ANSWERS C Sc 227 Practice Test 1Fall 2013

0. First question will be correct with any answer(this will be an attempt to reduce tension with no knowledge of anything, just grab any answer without thinking)

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

assertEquals(2, j / k); // a. ______1, not 2

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

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

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

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

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

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

assertEquals(2.0, Math.sqrt(j), 1e-12); // h. ______sqrt(5), not 4

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

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

assertEquals(3, s.compareTo("U of D")); // k. ______returns -3

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;

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

n++;
Infinite / j = j + 3;
} 4
System.out.print("Hello");
Zero / System.out.print("Hello"); 1

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

int result = 0;

while(scanner.hasNext() ) {

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

// Scanner problems when the nextInt is used twice, we will

int current = scanner.nextInt();

if (current == search)

result++;;

}

return result;

}

5public 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;

}

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

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

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

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

return true;

}

return false;

}

7public 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.public String227substring(int beginIndex, int endIndex) {

n = endIndex - beginIndex;// Change instance variable so length() and toString() work

char[] temp = new char[n];

int otherIndex = beginIndex;

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

temp[i] = theChars[otherIndex];

otherIndex++;

}

return new String227(temp);

}

9. putSumInDiagonal, other solutions are possible for this or any other solution on a test

for(int r = 0; r < lastRow; r++) {

int sum = 0;

for (int c = 0; c < lastCol; c++ ) {

sum += m[r][c];

m[r][c] = 0;

}

m[r][r] = sum;
}