Corrections

  • #11 (D) : The original code segment prints “YES” for odd integers and “NO” for even integers. Code segment I produces the same output as the original code segment because the result of evaluating the expression x % 2 has only two possible values: 0 if x is even and 1 if x is odd. Code segment II produces the same output as the original code segment because the second else clause that prints “NONE” is never reached. Code segment III does not produce the same output as the original code segment. It creates a local boolean variable and assigns it the result of x % 2 == 0, but when using it in the if statement, the wrong branch is taken so that “YES” is printed for even integers and “NO” is printed for odd integers.

  • #20 (E) : The given code segment starts when k is 1 and prints every other value as long as k is less than or equal to 7. This code segment starts when k is 1 and prints every other value as long as k is less than or equal to 8. Both code segments print 1357.

  • #26 (A) : The length of arr is 6. When k takes on the value 6 in the last iteration of the loop, referencing arr[6] causes a runtime error because the last element of the array is arr[5]. Changing the Boolean expression in the for loop header to k < arr.length fixes the error by making arr[5] the last array element referenced.

  • #27 (B) : One issue with the code is that when the body of the if statement executes, the loop variable j is sometimes incremented by 1 twice, once inside the body of the if statement and once at the end of the loop iteration. As a consequence, the for loop iterates through some but not all of the elements in nums. Specifically, the method does not work as intended when the input list has two consecutive pairs of duplicate integers. For the list in this option, at the beginning of the first loop iteration, the value of j is 0 and the value of nums is {1, 2, 2, 3, 3, 4, 5}. At the beginning of the second loop iteration, the value of j is 1 and the value of nums is {1, 2, 2, 3, 3, 4, 5}. At the beginning of the third loop iteration, the value of j is 3 and the value of nums is {1, 2, 3, 3, 4, 5}. At the beginning of the fourth (and last) loop iteration, the value of j is 4 and the value of nums is {1, 2, 3, 3, 4, 5}. At the end of the method, the value of nums is {1, 2, 3, 3, 4, 5} and is different than the expected value of nums, which is {1, 2, 3, 4, 5}.

  • #37 (E) : In the body of the first if clause in the code segment, b1 retains the value true if num is between 0 and 100, exclusive. In the body of the else clause, b1 retains the value true if num is less than -100. The statement assigns true to b2 if num is less than -100 or between 0 and 100, exclusive.

  • #38 (B!) : Choice 2 correctly creates a new point with the two paramerter Point constructor that assigns a to x and b to y. Choice 1 and 3 use the no paramater point constructor which incorrectly assigns 0 to both x and y. Choice 3 attempts to update x and y individually but they are variables in Point that cannot be accessed direclty from circle. This code will cause a compile runtime error.

  • #50 (D) : The for loop iterates from i = 0 to i = 19. The expression i % num2 == 0 evaluates to true when i is divisible by 5 and the expression i % 2 == 0 evaluates to true when i is even. The only values in the range 0 to 19, inclusive, that are both divisible by 5 and even are 0 and 10, so the statement prints “0 10 “.

  • #59 (B) : Choice I will return the correct value if the element in the array with one element was target. In this case, with the first call to seqSearchRecHelper the value of data[0] would be target and 0 would be returned. Choice II will eventually cause an ArrayIndexOutOfBoundsException to be thrown when the recursive call is made with target and -1. This will happen after every valid index in data has been examined. During this call, data[-1] is out of bounds. Choice III will correctly return the index of the element closest to the end of the array with the value target, since data[last] == target will be true at some valid index value of last.