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.

In-content ad
  1. Question 1 · Easy

    How many times does the following loop print "hello"?

    for (int i = 0; i < 5; i++) {
        System.out.println("hello");
    }
    
    • A
      4
      Why not A: The loop runs while i < 5, starting from i=0. That gives i = 0,1,2,3,4 — five iterations, not four.
    • B
      5Correct
    • C
      6
      Why 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.
    • D
      Infinite loop
      Why not D: i++ increments i each iteration. Eventually i reaches 5, the condition i < 5 becomes false, and the loop terminates.
    Explanation

    The loop variable i takes values 0, 1, 2, 3, 4 — five values total. When i = 5, the condition i < 5 is false and the loop exits. So "hello" is printed 5 times.

    Key takeaway

    A `for (int i = 0; i < n; i++)` loop executes exactly n times with i going from 0 to n-1.

  2. 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);
    
    • A
      10
      Why not A: The loop sums 1+2+3+4+5. That is 15, not 10 (which would be 1+2+3+4).
    • B
      15Correct
    • C
      14
      Why not C: The condition is i <= 5 (inclusive), so i=5 is included. 1+2+3+4+5 = 15, not 14.
    • D
      20
      Why 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.
    Explanation

    The 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 takeaway

    Use `<=` to include the upper bound. The sum of integers 1 through n is n*(n+1)/2.

  3. Question 3 · Easy

    What is the output of the following code?

    int i = 1;
    while (i < 16) {
        i *= 2;
    }
    System.out.println(i);
    
    • A
      8
      Why not A: After i=8, the loop checks 8 < 16 (true) and doubles again to 16. The loop doesn't stop at 8.
    • B
      16Correct
    • C
      15
      Why 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.
    • D
      32
      Why not D: When i=16, the condition 16 < 16 is false. The loop exits before doubling again. i stays at 16.
    Explanation

    Trace: 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 takeaway

    Trace while loops by checking the condition BEFORE each iteration. The loop exits as soon as the condition is false.

  4. Question 4 · Easy

    What is the value of count after the following code executes?

    int count = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            count++;
        }
    }
    
    • A
      7
      Why not A: count++ runs 4*3=12 times (outer loop * inner loop), not 4+3=7 times.
    • B
      12Correct
    • C
      16
      Why not C: The inner loop runs 3 times (j = 0,1,2), not 4 times. Total = 4 * 3 = 12.
    • D
      10
      Why not D: Nested loops multiply their iteration counts. 4 outer iterations × 3 inner iterations = 12 total increments.
    Explanation

    For 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 takeaway

    Nested loop iteration count = outer iterations × inner iterations.

  5. Question 5 · Easy

    What is the output of the following code?

    for (int i = 1; i <= 10; i += 3) {
        System.out.print(i + " ");
    }
    
    • A
      1 4 7 10Correct
    • B
      1 4 7
      Why not B: i = 10 satisfies i <= 10, so 10 is included. Check: i = 1, 4, 7, 10 (next would be 13 > 10 → stop).
    • C
      1 3 6 9
      Why not C: The increment is += 3, not cumulative sums. Starting at 1: 1, 1+3=4, 4+3=7, 7+3=10.
    • D
      1 4 7 10 13
      Why not D: 13 > 10, so the condition i <= 10 is false when i = 13. The loop terminates before printing 13.
    Explanation

    Starting at i=1, each iteration adds 3: 1 → 4 → 7 → 10 → 13. When i=13, the condition i <= 10 is false, so the loop exits. Output: 1 4 7 10.

    Key takeaway

    The `for` loop update can use any step size (not just `i++`). Trace: start, check condition, execute body, update.

  6. 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);
    
    • A
      1
      Why not A: Both 'o' (index 1) and 'i' (index 3) are in "coding". Count should be 2.
    • B
      2Correct
    • C
      3
      Why not C: "coding" has characters: c-o-d-i-n-g. 'o' appears once and 'i' appears once. Total = 2.
    • D
      6
      Why not D: The count increments only when the character is 'i' or 'o', not for every character.
    Explanation

    Iterate 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"`.

  7. Question 7 · Easy

    What is the output of the following code?

    int x = 1;
    do {
        System.out.print(x + " ");
        x *= 3;
    } while (x < 30);
    
    • A
      1 3 9 27Correct
    • B
      3 9 27
      Why not B: A do-while loop always executes the body at least once before checking the condition. x=1 is printed first.
    • C
      1 3 9 27 81
      Why not C: After printing 27, x becomes 81. The condition 81 < 30 is false, so the loop exits. 81 is not printed.
    • D
      1 3 9
      Why not D: After x=9, x*=3 gives 27. The condition 27 < 30 is true, so the body runs again, printing 27.
    Explanation

    do-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 takeaway

    A do-while loop always executes the body at least once; the condition is checked AFTER the body runs.

  8. 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;
        }
    }
    
    • A
      It 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.
    • B
      It 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.
    • C
      It prints 1 2 3 5, skipping 4, due to modifying i inside the loop.Correct
    • D
      It 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.
    Explanation

    i 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 takeaway

    Never modify the loop control variable inside the loop body — it leads to confusing, hard-to-debug behavior.

  9. 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 + " ");
    }
    
    • A
      0 2 4
      Why not A: continue skips the rest of the loop body when the condition is true (i is even). Even numbers are skipped, odd numbers are printed.
    • B
      1 3Correct
    • C
      0 1 2 3 4
      Why not C: continue skips println for even values of i. Even numbers (0, 2, 4) are not printed.
    • D
      1 2 3 4
      Why 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).
    Explanation

    continue skips the remaining body of the loop for the current iteration and moves to the update (i++). When i is even (0, 2, 4), continue skips 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.

  10. 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();
    }
    
    • A
      3 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).
    • B
      Row 1: 3 stars, Row 2: 2 stars, Row 3: 1 starCorrect
    • C
      Row 1: 1 star, Row 2: 2 stars, Row 3: 3 stars
      Why 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.
    • D
      Row 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.
    Explanation

    i=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 takeaway

    When the inner loop variable starts at the outer loop variable (j=i), each row has fewer iterations — creating a triangular pattern.

  11. 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);
    
    • A
      5
      Why not A: Trace: 100→50→25→12→6→3→1. That is 6 divisions (count increments 6 times before n reaches 1).
    • B
      6Correct
    • C
      7
      Why not C: After n=1, the condition n > 1 is false and the loop exits without another increment. count stays at 6.
    • D
      100
      Why 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.
    Explanation

    Trace: 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 takeaway

    Halving in a loop runs approximately log₂(n) times — a key pattern showing logarithmic iteration count.

  12. 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;
    }
    
    • A
      5 3 1Correct
    • B
      5 3 1 -1
      Why not B: After printing 1, x becomes -1. The loop checks the break condition: -1 <= 0 is true, so break exits before printing -1.
    • C
      Infinite loop
      Why not C: The break statement exits the loop when x <= 0. x decreases by 2 each iteration, so it eventually reaches a value <= 0.
    • D
      5 3
      Why 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.
    Explanation

    x=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.