ANSWERS C Sc 227 Practice Midterm During Summer 2012

ANSWERS C Sc 227 Practice Midterm During Summer 2012


ANSWERS C Sc 227 Practice Midterm during Summer 2012

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 next = scanner.nextInt();

if (next == search)

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(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;

}

7 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.public boolean equals(String227 other) {

for (int i = 0; i < Math.min(length(), other.length()); i++) {

if (charAt(i) != other.charAt(i)) {

// Found 2 characters in the same index that are not equal

return false;

}

}

// Reach this point if all chars were equal up to the last char of the

// shorter one. If both have same length (number of chars), return true.

return length() == other.length();

}

9. substring

// Precondition: beginIndex <= endIndex

// If beginIndex == endIndex return an empty String227 object

public String227 substring(int beginIndex, int endIndex) {

int size = endIndex - beginIndex;

char[] temp = new char[size];

int index = beginIndex;

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

temp[i] = charAt(index);

index++;

}

return new String227(temp);

}

10. putSumInDiagonal

Other solutions are possible for this or any other solution that ask to write a method

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

int sum = 0;

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

sum += m[r][c];

m[r][c] = 0;

}

m[r][r] = sum;

}

11. Many other answers are possible including use of a temporary 2D array.

public void mirrored() {

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

for (int col = 0; col < nCols / 2; col++) { // -4 if you miss the /2

// reverse each row stopping at the middle

int temp = data[row][col];

data[row][col] = data[nRows - row - 1][nCols - col - 1];

data[nRows - row - 1][nCols - col - 1] = temp;

}

}

}

12

a. O(n)d. O(n2)

b. O(n) e. O(1)

c. O(log n) f. O(n log n)

13. Write an X in the comment to the right of all statements that compile. (4pts)

Object anObject = new Integer(3); // a. _X__

Object otherObject = "a string"; // b. _X__

Integer anInt = new Object(); // c. ____

Double d = new Object(); // d. ____

14. Write an X in the comment to the right of all statements that compile. (4pts)

Integer i1 = 3; // a. __X__

int i2 = new Integer(-1); // b. __X__

Object obj = 3; // c. __X__

Double d3 = 12; // d. _____ Tricky, 12.0 works, no on midterm!

15. Write an X in the comment to the right of all statements that compile. (4pts)

Object[] elements = new Object[5]; // a. _X___

elements[0] = 12; // b. _X___

elements[1] = "Second"; // c. _X___

String str = elements[1]; // d. ____ // can not cast down

16. Will this code compile, yes or no ___No___? (4pts)

Object obj = 3;

Integer anInt = obj; // can not cast down

17. Will this code generate a runtime error (an exception) when it is run, yes or no __ No __? (4pts)

Object obj = new String("abc");

String str = (String)obj; // as long as we tell the compiler obj is a string

18. Does this code compile, yes or no __ Yes ____? (4pts)

GenericBag name = new GenericArrayBag();

names.add(2);

names.add(new BankAccount("Pat", 2.00)); // Can store any type to object

names.add("abc");

19. Does this code compile, yes or no ___ No ___? (4pts)

GenericBag<Integer> name = new GenericArrayBag<Integer>();

names.add(2);

names.add(new BankAccount("Pat", 2.00)); // only store ints or Integers

names.add("abc");

20 write DS if it is a data structure, COLL if it is a collection class or ADT if it is abstract data type.

a.___ADT____ Bag b.__ ADT ___ PriorityList<E> c._ COLL __ ArrayPriorityList<E>

d.__ DS ___ Binary Treee.__ DS__ int[] f._ ADT _ Comparable<E>