Score - 34/40
Q4 - Selection with variables x and y
- answer picked - Answer B
- This would be the result if the division used was floating point division, instead of integer division. This would be the case if either x or y were of type double instead of type int or if either value was typecast as a double in the expression.
- correct answer - Answer C
- When we evaluate the express(x < 10) && (y < 0) for x having the value 7 and y having the value 3, x < 10 evaluates to true, since 7 is less than 10, and y < 0 evaluates to false, since 3 is not less than 0. The logic operator && evaluates to true when both conditions are true and evaluates to false otherwise. Since the second condition is false, the boolean expression is false. As a result, the compiler will skip the first output statement and execute the statement in the else. The expression x / y is integer division for 7 / 3, which is 2.
Q23 - manipulate method and animals List
- answer picked - Answer C
- List is an interface, which an ArrayList implements. Please note that List is no longer tested as part of the AP CSA exam and ArrayList will be used instead. This would be the correct answer if the remove occurred before the size was calculated in the statement animals.add(animals.size()-k, animals.remove(k)); and only one iteration of the loop occurred.
- correct answer - Answer B
- List is an interface, which an ArrayList implements. In the fifth iteration, when k is 1, the element of animals at 1 (“baboon”) starts with a “b”. It is removed from the list and inserted at index 5. The list would then be {“bear”, “zebra”, “bass”, “cat”, “koala”, “baboon”}. Finally, k decrements to 0 which is not greater than 0 so the loop terminates.
Q28 mystery method with int parameter description
- answer picked - Answer B
- If the value of n is 2 or less in the original call to the method mystery, the loop will be skipped and x will be 1 at //Point C.
- correct answer - Answer E
- The while loop only iterates while n is greater than 2 and //Point B is in the body of the while loop prior to any change to the value of n. At this point, n will always be greater than 2.
Q30 scramble method with String and int parameters
- answer picked - Answer D
- This would be the value if the second call to substring was word.substring(0, howFar + 1).
- correct answer - Answer C
- The two parameter substring method returns the substring beginning at the first parameter and ending at the second parameter – 1. When word is assigned “compiler” and howFar is assigned 3, the value of word.substring(howFar + 1, word.length()) is “iler”. This is the substring of “compiler” beginning at 3 + 1 or 4 and ending at 8 – 1 or 7. The value of word.substring(0, howFar) is “com”. This is the substring of “compiler” beginning at 0 and ending at 2. The method returns “ilercom”.
Q33 Print sum after while loop
- answer picked - Answer B
- This would be correct if k was incremented by 1 during each iteration of the loop and the loop condition was changed to (sum < 12 || k <= 4). Since k is never incremented, the or (||) will always be true since k will always be less than 4 and an infinite loop will occur.
- correct answer - Answer E
- Since k is never changed in the body of the while loop, it will always be 1 and less than 4. In a boolean expression with or (||) if one of the two expressions is true, the expression is true. This will cause an infinite loop.
Q39 recur method with int parameter
- answer picked - Answer B
- This is the value that is passed in the first recursive call to recur.
- correct answer - Answer D
- The call recur(27) returns the value of recur(recur(9)) since 27 is greater than 10. The call recur(9) returns 18, since 9 is less than or equal to 10. Therefore, recur(recur(9)) is recur(18). The call recur(18) returns recur(recur(6)) since 18 is greater than 10. The call recur(6) returns 12, since 6 is less than or equal to 10. Therefore, recur(recur(6)) is recur(12). The call recur(12) returns recur(recur(4)) since 12 is greater than 10. The call recur(4) returns 8, since 4 is less than or equal to 10. Therefore, recur(recur(4)) is recur(8). The call recur(8) returns 16, since 8 is less than or equal to 10. Therefore, recur(27)returns the value of 16.