AP Computer Science A · Topic 4.1

while Loops Practice

Part of Iteration.

Practice questions

30

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 30 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 2/5

    int n = 1;
    while (n < 16) {
      n *= 2;
    }
    System.out.println(n);

    What is printed?

    • A

      16

      check_circle
    • B

      8

    • C

      32

    • D

      15

    Why

    n doubles each time: 1, 2, 4, 8, 16. When n becomes 16 the condition n < 16 is false and the loop exits. n is 16.

  2. Sample 2difficulty 2/5

    int i = 10;
    while (i < 5) {
      System.out.println(i);
      i++;
    }
    System.out.println("done");

    What is printed?

    • A

      done

      check_circle
    • B

      10 done

    • C

      nothing

    • D

      10 11 12 done

    Why

    The while condition (10 < 5) is false at the start, so the loop body never executes. Only "done" prints.

  3. Sample 3difficulty 2/5

    What is printed?

    <code>int i = 0; while (i < 3) { System.out.print(i + " "); i++; } </code></pre>

    • A

      1 2 3

    • B

      Infinite loop

    • C

      0 1 2 3

    • D

      0 1 2

      check_circle

    Why

    Loops while i < 3: prints 0, 1, 2.

  4. Sample 4difficulty 3/5

    <code>int i = 1; while (i < 16) i *= 2;</code> — final i and iterations

    • A

      i=16, 5 iterations

    • B

      i=32, 5 iterations

    • C

      i=16, 4 iterations

      check_circle
    • D

      i=8, 3 iterations

    Why

    1 → 2 → 4 → 8 → 16 (4 multiplications); condition fails at 16.

  5. Sample 5difficulty 3/5

    int sum = 0;
    for (int i = 1; i <= 5; i++) {
      if (i % 2 == 0) {
        continue;
      }
      sum += i;
    }
    System.out.println(sum);

    What is printed?

    • A

      15

    • B

      9

      check_circle
    • C

      6

    • D

      5

    Why

    continue skips the rest of the iteration when i is even. Only odd values 1, 3, 5 are added: 1+3+5 = 9.

AP Computer Science A · 4.1 while Loops — Practice Questions | Acemy