AP Computer Science A Iteration — Worked Answer Explanations
Unit 4 · 12 questions explained
Below is a complete answer key for our AP Computer Science A Iteration practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.
Prefer to test yourself first? Take the timed Iteration practice test and come back here to review, or head back to the Iteration unit overview.
- Question 1 · Easy
How many times does the following loop print "hello"?
for (int i = 0; i < 5; i++) { System.out.println("hello"); }- A4Why not A: The loop runs while i < 5, starting from i=0. That gives i = 0,1,2,3,4 — five iterations, not four.
- B5Correct
- C6Why not C: The condition is
i < 5(strict less than). When i=5, the condition is false and the loop ends. i=0 through i=4 is 5 iterations. - DInfinite loopWhy not D:
i++increments i each iteration. Eventually i reaches 5, the conditioni < 5becomes false, and the loop terminates.
ExplanationThe loop variable
itakes values 0, 1, 2, 3, 4 — five values total. When i = 5, the conditioni < 5is false and the loop exits. So "hello" is printed 5 times.Key takeawayA `for (int i = 0; i < n; i++)` loop executes exactly n times with i going from 0 to n-1.
- A
- Question 2 · Easy
What is the output of the following code?
int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; } System.out.println(sum);- A10Why not A: The loop sums 1+2+3+4+5. That is 15, not 10 (which would be 1+2+3+4).
- B15Correct
- C14Why not C: The condition is
i <= 5(inclusive), so i=5 is included. 1+2+3+4+5 = 15, not 14. - D20Why not D: The loop stops at i=5 (i <= 5). 1+2+3+4+5 = 15. The sum of 1 through 6 would be 21.
ExplanationThe loop adds i to sum for i = 1, 2, 3, 4, 5 (inclusive of 5 because of
<=). Sum = 1+2+3+4+5 = 15.Key takeawayUse `<=` to include the upper bound. The sum of integers 1 through n is n*(n+1)/2.
- A
- Question 3 · Easy
What is the output of the following code?
int i = 1; while (i < 16) { i *= 2; } System.out.println(i);- A8Why not A: After i=8, the loop checks 8 < 16 (true) and doubles again to 16. The loop doesn't stop at 8.
- B16Correct
- C15Why not C: i doubles each iteration: 1→2→4→8→16. When i=16, the condition 16 < 16 is false and the loop exits. i is 16, not 15.
- D32Why not D: When i=16, the condition 16 < 16 is false. The loop exits before doubling again. i stays at 16.
ExplanationTrace: i=1 (1<16→double→2), i=2 (2<16→4), i=4 (4<16→8), i=8 (8<16→16), i=16 (16<16 is false→exit). i = 16.
Key takeawayTrace while loops by checking the condition BEFORE each iteration. The loop exits as soon as the condition is false.
- A
- Question 4 · Easy
What is the value of
countafter the following code executes?int count = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { count++; } }- A7Why not A: count++ runs 4*3=12 times (outer loop * inner loop), not 4+3=7 times.
- B12Correct
- C16Why not C: The inner loop runs 3 times (j = 0,1,2), not 4 times. Total = 4 * 3 = 12.
- D10Why not D: Nested loops multiply their iteration counts. 4 outer iterations × 3 inner iterations = 12 total increments.
ExplanationFor each of the 4 outer iterations (i = 0,1,2,3), the inner loop runs 3 times (j = 0,1,2). Total count = 4 × 3 = 12.
Key takeawayNested loop iteration count = outer iterations × inner iterations.
- A
- Question 5 · Easy
What is the output of the following code?
for (int i = 1; i <= 10; i += 3) { System.out.print(i + " "); }- A1 4 7 10Correct
- B1 4 7Why not B: i = 10 satisfies i <= 10, so 10 is included. Check: i = 1, 4, 7, 10 (next would be 13 > 10 → stop).
- C1 3 6 9Why not C: The increment is
+= 3, not cumulative sums. Starting at 1: 1, 1+3=4, 4+3=7, 7+3=10. - D1 4 7 10 13Why not D: 13 > 10, so the condition
i <= 10is false when i = 13. The loop terminates before printing 13.
ExplanationStarting at i=1, each iteration adds 3: 1 → 4 → 7 → 10 → 13. When i=13, the condition
i <= 10is false, so the loop exits. Output: 1 4 7 10.Key takeawayThe `for` loop update can use any step size (not just `i++`). Trace: start, check condition, execute body, update.
- A
- Question 6 · Easy
What is the output of the following code?
String s = "coding"; int count = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'i' || s.charAt(i) == 'o') { count++; } } System.out.println(count);- A1Why not A: Both 'o' (index 1) and 'i' (index 3) are in "coding". Count should be 2.
- B2Correct
- C3Why not C: "coding" has characters: c-o-d-i-n-g. 'o' appears once and 'i' appears once. Total = 2.
- D6Why not D: The count increments only when the character is 'i' or 'o', not for every character.
ExplanationIterate through "coding": c(no), o(yes→count=1), d(no), i(yes→count=2), n(no), g(no). Final count = 2.
Key takeaway`charAt(i)` returns the character at index i. Use single quotes for char literals: `'a'` not `"a"`.
- A
- Question 7 · Easy
What is the output of the following code?
int x = 1; do { System.out.print(x + " "); x *= 3; } while (x < 30);- A1 3 9 27Correct
- B3 9 27Why not B: A do-while loop always executes the body at least once before checking the condition. x=1 is printed first.
- C1 3 9 27 81Why not C: After printing 27, x becomes 81. The condition 81 < 30 is false, so the loop exits. 81 is not printed.
- D1 3 9Why not D: After x=9, x*=3 gives 27. The condition 27 < 30 is true, so the body runs again, printing 27.
Explanationdo-while always executes the body first. Trace: x=1→print 1, x=3 (3<30→print 3, x=9), (9<30→print 9, x=27), (27<30→print 27, x=81), (81<30 false→exit). Output: 1 3 9 27.
Key takeawayA do-while loop always executes the body at least once; the condition is checked AFTER the body runs.
- A
- Question 8 · Easy
What is wrong with the following code that is supposed to print numbers 1 through 5?
for (int i = 1; i <= 5; i++) { System.out.println(i); if (i == 3) { i = 5; } }- AIt prints 1 2 3 4 5 as intended.Why not A: Modifying the loop variable inside the loop body causes unexpected behavior. When i=3, i is set to 5, then i++ makes i=6, skipping 4 and 5.
- BIt prints 1 2 3 5 (skips 4) then exits.Why not B: When i=3, i is set to 5. The body prints 5 (because i is 5 when the condition check passes). Then i++ makes i=6, and 6 <= 5 is false.
- CIt prints 1 2 3 5, skipping 4, due to modifying i inside the loop.Correct
- DIt causes an infinite loop.Why not D: Setting i=5 and then i++ makes i=6, which fails the i<=5 condition. The loop terminates, just with wrong output.
Explanationi goes 1(print 1), 2(print 2), 3(print 3, then set i=5), i++=6 — wait. After setting i=5 at end of iteration, i++ runs making i=6. Wait: body runs, prints 3, sets i=5; then the update i++ runs making i=6. Loop exits. Actually prints 1 2 3. But when i=5 is set inside body, the body already printed 3. Then i++ gives 6 > 5. Output is 1 2 3.
Key takeawayNever modify the loop control variable inside the loop body — it leads to confusing, hard-to-debug behavior.
- A
- Question 9 · Easy
What is the output of the following code?
for (int i = 0; i < 5; i++) { if (i % 2 == 0) continue; System.out.print(i + " "); }- A0 2 4Why not A:
continueskips the rest of the loop body when the condition is true (i is even). Even numbers are skipped, odd numbers are printed. - B1 3Correct
- C0 1 2 3 4Why not C:
continueskipsprintlnfor even values of i. Even numbers (0, 2, 4) are not printed. - D1 2 3 4Why not D: 0 is even (0 % 2 == 0), so it is skipped. Only odd numbers 1 and 3 are printed (4 is even and skipped too).
Explanationcontinueskips the remaining body of the loop for the current iteration and moves to the update (i++). When i is even (0, 2, 4),continueskips the print. When i is odd (1, 3), the print executes. Output: 1 3.Key takeaway`continue` skips the rest of the current loop iteration; `break` exits the loop entirely.
- A
- Question 10 · Medium
What is the output of the following code?
for (int i = 0; i < 3; i++) { for (int j = i; j < 3; j++) { System.out.print("* "); } System.out.println(); }- A3 stars, then 3 stars, then 3 stars (3 rows of 3)Why not A: The inner loop starts at j=i, not j=0. When i=1, inner runs 2 times (j=1,2). When i=2, inner runs 1 time (j=2).
- BRow 1: 3 stars, Row 2: 2 stars, Row 3: 1 starCorrect
- CRow 1: 1 star, Row 2: 2 stars, Row 3: 3 starsWhy not C: The inner loop starts at j=i. When i=0, j goes 0,1,2 (3 stars). When i=1, j goes 1,2 (2 stars). The pattern decreases.
- DRow 1: 3 stars, Row 2: 3 stars, Row 3: 3 stars (always 3)Why not D: The inner loop starts at j=i, so its iteration count decreases as i increases. Not always 3 stars.
Explanationi=0: j goes from 0 to 2 → 3 stars; i=1: j goes from 1 to 2 → 2 stars; i=2: j goes from 2 to 2 → 1 star. The pattern is decreasing because the inner loop starts at j=i.
Key takeawayWhen the inner loop variable starts at the outer loop variable (j=i), each row has fewer iterations — creating a triangular pattern.
- A
- Question 11 · Medium
What is the output of the following code?
int n = 100; int count = 0; while (n > 1) { n /= 2; count++; } System.out.println(count);- A5Why not A: Trace: 100→50→25→12→6→3→1. That is 6 divisions (count increments 6 times before n reaches 1).
- B6Correct
- C7Why not C: After n=1, the condition n > 1 is false and the loop exits without another increment. count stays at 6.
- D100Why not D: The loop halves n each iteration (logarithmic, not linear). With n=100 and halving each time, it takes about log2(100) ≈ 6 iterations.
ExplanationTrace: n=100(100>1 → n=50, count=1), (50>1 → n=25, count=2), (25>1 → n=12, count=3), (12>1 → n=6, count=4), (6>1 → n=3, count=5), (3>1 → n=1, count=6), (1>1 false → exit). count = 6.
Key takeawayHalving in a loop runs approximately log₂(n) times — a key pattern showing logarithmic iteration count.
- A
- Question 12 · Medium
What is the output of the following code?
int x = 5; while (true) { if (x <= 0) break; System.out.print(x + " "); x -= 2; }- A5 3 1Correct
- B5 3 1 -1Why not B: After printing 1, x becomes -1. The loop checks the break condition: -1 <= 0 is true, so break exits before printing -1.
- CInfinite loopWhy not C: The
breakstatement exits the loop when x <= 0. x decreases by 2 each iteration, so it eventually reaches a value <= 0. - D5 3Why not D: After x=3, x-=2 gives x=1. The check 1 <= 0 is false, so 1 is printed. Then x becomes -1 and the loop breaks.
Explanationx=5 (5>0 → print 5, x=3), (3>0 → print 3, x=1), (1>0 → print 1, x=-1), (-1<=0 → break). Output: 5 3 1. The break executes BEFORE the print in the next iteration, so -1 is never printed.
Key takeaway`break` immediately exits the loop. `while(true)` with a `break` is a valid pattern; trace carefully to determine what's printed before the break.
- A