ANSWERS C Sc 227 Practice Test 1, Fall 11

0. Anyanswer will do. I'll ask aon the real test that you cannot get wrong either. This is supposed to be a tension-reducing question (a free 2 points)

1. Write a checkmark √ to the right of any assertion if it would pass. Leave the line in the comment

assertEquals(2, j / k); // a. ______

assertEquals(1, j % k); // b. ______

assertEquals(-0.5, x + 1, 1e-12); // c. __√___

assertEquals('U', s.charAt(1)); // d. ______

assertEquals(5, s.indexOf("Arizona")); // e. __√___

assertEquals(0, s.indexOf("U of A")); // f. __√___

assertTrue(s.compareTo("ASU") > 0); // g. __√___

assertEquals(6, s.compareTo("U of A")); // h. __√___

2a. Complete the unit test for method grade (fill in the blanks) so it executes every branch of code in the
assertEquals( "A", grade(90.0) ); // or some argument > 90.

assertEquals( "B", grade(80.0) ); // or some argument > 80.0 and < 90.0

assertEquals( "C", grade(70.0) );

assertEquals( "D", grade(60.0) );

assertEquals( "E", grade(50.0) );

2b. You could use ifs, but remember to have one return that is NOT in an if statement (all could be false, compiler complains)

public String grade(double percentage) {

String result = "?"; // or an empty string

if (percentage >= 90.0)

result = "A";

else if (percentage >= 80.0)

result = "B";

else if (percentage >= 70.0)

result = "C";

else if (percentage >= 60.0)

result = "D";

else

result = "E";

return result;

3.How many times will the following code print "Hello"? 0, Unknown, and Infinite are legitimate

Infinite / 4
Zero / 1

4 public int occurencesOf(int target, Scanner scanner) {

int result = 0;

while(scanner.hasNextInt() ) {

// We don't really need an extra int here, however on other Scanner

// problems if nextInt() is used twice in the loop, you will likely lose poits

int next = scanner.nextInt();

if (next.equals(target))

result++;

}

return result;

}

5 public String mirrorEnds(String str) {

String result = "";

int left = 0;

int right = str.length() - 1;

while ( left < str.length() & right >= 0 & str.charAt(left) == str.charAt(right)) {

result = result + str.charAt(left);

left++;

right--;

}

return result;

}

6 public boolean exists(String target, String[] a) {

for (int i = 0; i < a.length; i++) {

// Could also use the equals method instead of compareTo, but do NOT use ==

if (target.compareTo(names[i]) == 0)

return true;

}

return false;

}

7 Assertions that check the returned array completely:

public int[] post4(int[] nums) {
int spot = -1;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 4)
spot = i;

}
int[] temp = new int[nums.length - spot - 1];
spot++;
for (int i = 0; i < temp.length; i++) {
temp[i] = nums[spot];
spot++;
}
return temp;
}

8. In class IntegerSet, complete method union to return a new IntegerSet that is the union of this IntegerSet

public IntegerSet union(IntegerSet other) {

IntegerSet result = new IntegerSet();

for (int i = 0; i < this.size(); i++)

result.add(this.elements[i]);

for (int i = 0; i < other.size(); i++)

result.add(other.elements[i]);

return result;

}

9. As part of the Matrix class, complete method transpose()to transpose any Matrix object. The transpose

public void transpose() {

int[][] t = newint[nCols][nRows];

for(int i = 0 ; i < nRows; i++) {

for(int j = 0 ; j < nCols; j++) {

t[j][i] = table[i][j];
}

}

table = t; // 2pts

nCols = t[0].length; // 1pt

nRows = t.length; // 1pt

}