CB MCQ
2015 CB Practice Exam MCQ and Corrections
Corrections
-
#17 (D) : The for loop starts at 1 and increments by 2 for each iteration until it is greater than num. The values of loop that are being added together are 1, 3, 5, …, num. When loop has the value num + 2, the loop stops preventing num + 2 from being included in the sum, since it is not less than or equal to num.
-
#25 (C) : The outer loop iterates four times (for x = 0, 1, 2, 3). When x is assigned 4, the loop terminates. The inner loop will iterate from the value of x to 4, not including 4. In the first iteration of the outer loop, the inner loop iterates four times (for y = 0, 1, 2, 3) and count will increase by 1 each time and will equal to 4. In the second iteration of the outer loop, the inner loop iterates three times (for y = 1, 2, 3) and count will now be 7. In the third iteration of the outer loop, the inner loop iterates two times (for y = 2, 3) and count will now be 9. In the fourth and final iteration of the outer loop, the inner loop will iterate one time (for y = 3) and count will be 10.
-
#33 (E) : Choice I sets max to Integer.MIN_VALUE, which is the smallest possible integer value. Then it accesses each element in arr and assigns them value. If value is greater than max, max is assigned value since it is now the largest value so far. Choice II uses an if statement inside the for loop to check and see if value is the first element in arr or not. Once the first element is identified, max is initialized to the first element and first is set to false. For all subsequent elements in arr, if value is greater than max, max is assigned value since it is now the largest value so far. Choice III sets max to the first value in arr. Then it accesses each subsequent value in arr checking to see if the value is greater than max, if it is max is assigned this element since it is now the largest value so far.
-
#35 (C) : In the first iteration of the binary search, it will check the value at index (0 + 7) / 2 which is index 3. Since 8 is greater than data[3], start is assigned mid + 1 which is 4 and the process will repeat. In the second iteration of the while loop, it will check the value at index (4 + 7) / 2 which is index 5. Since data[5] is 8, 5 is returned.
-
#36 (D) : The first iteration would check the middle element of 2000 and eliminate approximately 1000 elements. The second iteration would check the middle element of 1000 and eliminate approximately 500 elements. The third would eliminate approximately 250 elements. The fourth would eliminate approximately 125 elements. The fifth would eliminate approximately 62 elements. The sixth would eliminate approximately 31 elements. The seventh would eliminate approximately 15 elements. The eighth would eliminate approximately 7 elements. The nineth would eliminate approximately 3 elements. The tenth would eliminate approximately 1 element. The eleventh iteration would be the final iteration to determine if the element was found or not.
-
#37 (E) : Choice I iterates from startIndex to the end of the array words as expected, but when it adds elements to result it adds the current word followed by a second word starting at the end of the array words. This will result in duplicate words being added to result. Choice II starts at the end of the array words and adds each word to result working right to left until it reaches the startIndex. By starting at the end of the array, the elements are concatenated in reverse order. Choice III starts by adding the elements of words to temp in reverse order. The second loop starts at the beginning of temp (which was the end of words) and adds each subsequent element to result until it reaches the element that was at startIndex and is now at temp.length – startIndex resulting in result containing the required elements in reverse order.
-
#38 (C) : This recursive method starts at the end of the array numbers by setting numVals to numbers.length and checks to see if this element is the same as v, which is equal to the actual parameter val. If it is, it sets k to 1. The method recursively calls mystery, decrementing numVals by 1 each time. Once, numVals is equal to 1 the method checks to see if element 0 is equal to v and then the recursion is complete and k is returned. In each iteration, k will either be 1 or 0, based on whether the element is equal to v or not. The sum of all the values of k will be the return value of the original call to mystery.