Score: 35/40

Question 9

I put D when the correct answer was B because the elements of array key have indices 0 to key.length – 1. Since the for loop control variable i is initialized to 1 and will increase by 1 until it is equal to key.length, the access to key should be adjusted by 1, otherwise the value at index 0 will not be included in the sum and when i is key.length an ArrayIndexOutOfBoundsException will be thrown.

Question 16

I put C when the correct answer was D because in the first for loop, all the values from a1 are copied over to result at corresponding indices. At this point, the elements at index 0 through a1.length – 1 are full. The first index where the first element of a2 should be copied into is a1.length. We can use the loop control variable k to access all the elements in a2 and k + a1.length to access the corresponding elements in result.

Question 21

I put B when the correct answer was D because the algorithm uses nested enhanced for loops to iterate across all the elements in mat. The variable num is assigned the value of each element. If the positive difference between num and val is less than minDiff, num is the element of mat that is closest to val so far.

Question 34

I put D when the correct answer was B because Choice I uses the no parameter Point constructor to assign center a new Point with x and y both equal to 0, instead of x assigned the value a and y assigned the value b. Choice II correctly uses the two parameter Point constructor to create a new Point with x assigned the value a and y assigned the value b. Choice III uses the no parameter Point constructor to assign center a new Point with x and y both equal to 0. It attempts to update x and y, however since they are private instance variables in Point, they are not able to be accessed directly in Circle. This code will cause a compile time error.

Question 40

I put B when the correct answer was C because the call whatsItDo(“WATCH”) assigns to temp a substring of “WATCH” starting at 0 and ending at 4 – 1 or 3, which is “WATC”. Next the call whatsItDo(“WATC”) is made. The call whatsItDo(“WATC”), sets its local temp to “WAT” and calls whatsItDo(“WAT”). The call whatsItDo(“WAT”), sets its local temp to “WA” and calls whatsItDo(“WA”). The call whatsItDo(“WA”), sets its local temp to “W” and calls whatsItDo(“W”). The call whatsItDo(“W”) reaches the base case and doesn’t do anything since the length of “W” is 1. Then we need to finish the call to whatsItDo(“WA”), which prints the value of its local temp, “W”. Then we need to finish the call to whatsItDo(“WAT”), which prints the value of its local temp, “WA”. Then we need to finish the call to whatsItDo(“WATC”), which prints the value of its local temp, “WAT”. Then we need to finish the call to whatsItDo(“WATCH”), which prints the value of its local temp, “WATC”. And the recursive calls are complete.