AP Computer Science A Unit 2 practice questions
Try 10 multiple-choice questions from Unit 2 here. Pick an answer to see why it's right or wrong, then take the timed test above for a full AP-style set.
Question 1
Source:Fiveable fictional Java practice code aligned to the AP Computer Science A Fall 2025 CED
Consider the following Java code.
</>Javapublic class CountDown { public static int compute(int n) { int total = 0; int k = n; while (k > 0) { total += k; k -= 2; } return total; } public static void main(String[] args) { int result = compute(7); System.out.println(result); int x = 0; int limit = 4; while (x < limit) { System.out.print(x + " "); x++; } } }What is printed to the console as a result of executing the main method in the code shown?
16 0 1 2 3
16 0 1 2 3 4
12 0 1 2 3
16 1 2 3 4
Show answer
Answer: (A)
Tracing compute(7): iteration 1 k=7, total=7, k becomes 5; iteration 2 k=5, total=12, k becomes 3; iteration 3 k=3, total=15, k becomes 1; iteration 4 k=1, total=16, k becomes -1; condition -1>0 is false, loop exits, returns 16. println(16) prints '16' then newline. Second loop: x starts at 0, limit=4. Prints '0 ', '1 ', '2 ', '3 ', then x=4 fails x<4, loop ends. Full output is '16' on line 1 and '0 1 2 3 ' on line 2.
Question 2
Consider the following code segment.
</>Javaint count = 0; for (int i = 0; i < 4; i++) { for (int j = i; j < 4; j++) { if ((i + j) % 2 == 0) { count++; } } } System.out.println(count);What is printed as a result of executing this code segment?
681016Show answer
Answer: (A)
Tracing the nested loops shows the inner loop's starting value depends on the outer loop variable
i. The condition(i + j) % 2 == 0is true wheniandjare both even or both odd. The matching(i, j)pairs are (0,0), (0,2), (1,1), (1,3), (2,2), and (3,3), which incrementscounta total of 6 times.Question 3
Consider the following code segment.
</>JavaString result = ""; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { result += (i * j) + " "; } } System.out.println(result);What is printed as a result of executing this code segment?
"1 2 4 3 6 9 ""1 2 2 4 3 6 ""1 2 2 3 3 3 ""1 1 2 1 2 3 "Show answer
Answer: (A)
The outer loop runs from 1 to 3, and the inner loop runs from 1 to the current value of
i. The code concatenates the product ofiandj. Fori=1, it appends 11. Fori=2, it appends 21 and 22. Fori=3, it appends 31, 32, and 33.Question 4
Consider the following code segment.
</>Javaint val = 20; for (int i = 1; i < 4; i++) { int j = i; while (j > 0) { val -= j; j--; } } System.out.println(val);What is printed as a result of executing this code segment?
1014017Show answer
Answer: (A)
The outer
forloop runs forivalues 1, 2, and 3. The innerwhileloop subtracts all integers fromidown to 1 fromval. It subtracts 1 (when i=1), then 2+1=3 (when i=2), then 3+2+1=6 (when i=3). The total subtracted is 10, leavingvalas 10.Question 5
Consider the following code segment.
</>Javaint total = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { if (j > i) { break; } total += j; } } System.out.println(total);What is printed as a result of executing this code segment?
20103040Show answer
Answer: (A)
The
breakstatement causes the inner loop to terminate early whenj > i. This meansjonly takes on values from 1 up toi. The code sums these values: 1 (for i=1), 1+2 (for i=2), 1+2+3 (for i=3), and 1+2+3+4 (for i=4), resulting in 1 + 3 + 6 + 10 = 20.Question 6
Consider the following code segment.
</>Javadouble val = 0; int divisor = 2; for (int i = 0; i < 2; i++) { val += 11 / divisor; divisor += 3; } System.out.println(val);What is printed as a result of executing this code segment?
7.0
7.7
8.0
5.0
Show answer
Answer: (A)
Tracing a loop containing integer division and automatic widening. In the first iteration,
11 / 2evaluates to5(integer division), which widens to5.0. In the second iteration,11 / 5evaluates to2, widening to2.0. The sum is7.0.Question 7
Consider the following code segment.
</>Javaint total = 0; int count = 5; while (count > 2) { total += count; count--; } total *= count; System.out.println(total);What is printed as a result of executing this code segment?
24
14
27
60
Show answer
Answer: (A)
Analyzing loop execution with decrement operators requires tracking the loop control variable and the accumulator. The loop runs for
countvalues of 5, 4, and 3, adding them tototalto reach 12. The loop terminates whencountbecomes 2, and the final statement multipliestotal(12) by the currentcount(2) to yield 24.Question 8
The following method is intended to return a new string containing the characters of
wordin reverse order.</>Javapublic static String reverse(String word) { String result = ""; /* missing code */ return result; }Which of the following can replace
/* missing code */to correctly reverse the string?for (int i = 0; i < word.length(); i++) { result = word.charAt(i) + result; }for (int i = 0; i < word.length(); i++) { result = result + word.charAt(i); }for (int i = 1; i < word.length(); i++) { result = word.charAt(i) + result; }for (int i = 1; i <= word.length(); i++) { result = result + word.charAt(i); }Show answer
Answer: (A)
Evaluating string concatenation order within a forward-iterating loop. By iterating from the beginning of the string and prepending each character (
word.charAt(i) + result), the characters are accumulated in reverse order.Question 9
The following method is intended to return a new string with all lowercase vowels (
"a","e","i","o","u") removed from the parameterword.</>Javapublic static String removeVowels(String word) { String result = ""; for (int i = 0; i < word.length(); i++) { String letter = word.substring(i, i + 1); if (/* missing code */) { result += letter; } } return result; }Which of the following can replace
/* missing code */so the method works as intended?"aeiou".indexOf(letter) == -1"aeiou".indexOf(letter) >= 0"aeiou".indexOf(letter) != -1"aeiou".indexOf(letter) == 1Show answer
Answer: (A)
Utilizing the
indexOfmethod to filter characters. TheindexOfmethod returns-1when a substring is not found within the target string. Checking for-1ensures the current letter is not a vowel before appending it to the result.Question 10
A meteorologist has two datasets: Dataset 1 contains
cityIDandaverageTemp(adouble). Dataset 2 containscityIDandcurrentTemp(adouble). The meteorologist evaluates the Boolean expressioncurrentTemp <= averageTemp - 10.0for a specific city. What knowledge is extracted when this expression evaluates totrue?The city's current temperature is at least ten degrees below its historical average.
The city's current temperature is exactly ten degrees below its historical average.
The city's current temperature is at most ten degrees above its historical average.
The city's current temperature is more than ten degrees above its historical average.
Show answer
Answer: (A)
Evaluating the
<=operator requires recognizing that it checks for values less than or equal to a threshold. Since the threshold is the average temperature minus 10.0, a true result indicates the current temperature is at least ten degrees colder than the historical average.
What's on the AP Computer Science A Unit 2 practice test
The standard version takes about 66 minutes and follows the timing and directions of the AP exam, sized to one unit:
- 20 Multiple Choice Questions (43 minutes)
- 1 FRQ – FRQ 1 – Methods and Control Structures (23 minutes)
Questions are drawn from 987 multiple-choice questions and 34 FRQs written for Unit 2, Selection and Iteration.
Practice AP Computer Science A Unit 2 FRQs
Answer the unit's free-response questions one at a time and get a score on every part.
- FRQ 1 – Methods and Control Structures (34 questions)
Review Unit 2 before you start
- 2.1 Algorithms with Selection and Repetition
- 2.2 Boolean Expressions
- 2.3 If Statements and Control Flow
- 2.4 If-Else Statements
- 2.5 Compound Boolean Expressions
- 2.6 Equivalent Boolean Expressions
- 2.7 While Loops
- 2.8 For Loops
- 2.9 Implementing Selection and Iteration Algorithms
- 2.10 Developing Algorithms Using Strings
- 2.11 Nested Iteration
- 2.12 Informal Code Analysis