AP CS Java Basics MCQs Answers
AP CS Java Basics MCQs Answers
II.
III.
(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I and III only
Answer Key
1. B
2. E
3. D
4. E
5. E
6. C
7. B
8. D
9. D
10. A
11. C
12. D
13. A
14. C
15. A
16. D
17. D
18. B
19. C
20. C
21. B
22. E
23. D
24. D
Answer Explanations
1. (B) When x is converted to an integer, as in segment I, information is
lost. Java requires that an explicit cast to an int be made, as in
segment II. Note that segment II will cause x to be truncated: The
value stored in y is 14. By requiring the explicit cast, Java doesn’t let
you do this accidentally. In segment III, y will contain the value 14.0.
No explicit cast to a double is required since no information is lost.
2. (E) The string argument contains two escape sequences: ‘\\’, which
means print a backslash (\), and ‘\n’, which means go to a new line.
Choice E is the only choice that does both of these.
3. (D) Short-circuit evaluation of the boolean expression will occur. The
expression (n != 0) will evaluate to false, which makes the entire
boolean expression false. Therefore the expression (x / n > 100)
will not be evaluated. Hence no division by zero will occur, which
would have caused an ArithmeticException to be thrown. When the
boolean expression has a value of false, only the else part of the
statement, statement2, will be executed.
4. (E) For this choice, the integer division 13/5 will be evaluated to 2,
which will then be cast to 2.0. The output will be 13/5 = 2.0. The
compiler needs a way to recognize that real-valued division is
required. All the other options provide a way.
5. (E) The operators *, /, and % have equal precedence, all higher than
-, and must be performed first, from left to right.
This becomes 5 * 12 / 11 = 60 / 11 = 5.
10. (A) Since the first test (x >= 0) is true, the matching else part, y =
-x, will not be executed. Since (x <= 100) is true, the matching
else part, y = x * 2, will not be executed. The variable y will be set
to x * 3 (i.e., 90) and will now fail the test y < 50. Thus, x will
never be altered in this algorithm. Final values are x = 30 and y =
90.
The while loop terminates when x is 4 since the test while (x > 5)
fails.
18. (B) The algorithm finds the remainder when the sum of the first three
digits of n is divided by 7. If this remainder is equal to the fourth
digit, checkDigit, the method returns true, otherwise false. Note
that (6+1+4) % 7 equals 4. Thus, only choice B is a valid number.
19. (C) As n gets broken down into its digits, nRemaining is the part of n
that remains after each digit is stripped off. Thus, nRemaining is a
self-documenting name that helps describe what is happening.
Choice A is false because every digit can be stripped off using some
sequence of integer division and mod. Choice B is false because num
is passed by value and therefore will not be altered when the method
is exited (see p. 111). Eliminate choice D: When the method is
exited, all local variables are destroyed. Choice E is nonsense.
20. (C) Segment III works because if you enter an age of 90, say,
category will correctly be assigned "Senior", and none of the other
else pieces of code will be executed. Similarly, if you enter an age
corresponding to an adult or a child, only the correct assignment is
made. Segment I fails because if you enter an age of 90, category
will be assigned "Senior", but then will be changed to "Adult"
when the age passes the second test. Segment II uses incorrect
syntax. The segment will work if you change the second test to
if (age >= 18 && age <= 64)
21. (B) The outer loop produces five rows of output. Each pass through
the inner loop goes from i down to 1. Thus five odd numbers
starting at 9 are printed in the first row, four odd numbers starting at
7 in the second row, and so on. You can picture what is happening in
a chart.
i j Steps
5 5
4 Print 5 odd numbers starting at
3 9:
2 9 7 5 3 1
1
22. (E) All three algorithms produce the given output. The outer for
(int i …) loop produces six rows, and the inner for (int k …)
loops produce the symbols in each row.
23. (D) Statement I is false, since if 100 ≤ num ≤ 109, the body of the
while loop will be executed just once. (After this single pass through
the loop, the value of num will be 10, and the test if (num > 10) will
fail.) With just one pass, newNum will be a one-digit number, equal to
temp (which was the original num % 10). Note that statement II is
true: There cannot be an infinite loop since num /= 10 guarantees
termination of the loop. Statement III is true because if num ≤ 10,
the loop will be skipped, and newNum will keep its original value of 0.
24. (D) The algorithm works by stripping off the rightmost digit of n
(stored in rem), multiplying the current value of revNum by 10, and
adding that rightmost digit. When n has been stripped down to no
digits (i.e., n == 0 is true), revNum is complete. Both segments II
and III work. Segment I fails to produce the right output whenever
the input value n has a first digit less than (number of digits − 1). For
these cases, the output has the first digit of the original number
missing from the end of the returned number.