AP Computer Science A Practice Quiz: while Loops
Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026
Test your understanding with short quizzes. This quiz has 14 questions to check your progress.
Question 1 of 14
All Questions (14)
A) To make a single decision based on a Boolean expression.
B) To store a collection of related data elements.
C) To repeat a segment of code as long as a condition is true.
D) To define a new data type for an object.
Correct Answer: C
Based on the provided content, 'Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.' This directly corresponds to repeating code based on a condition.
A) 0
B) 4
C) 5
D) 6
Correct Answer: C
The loop continues as long as `count` is less than 5. The loop will execute for `count` values of 0, 1, 2, 3, and 4. In the last iteration, `count` is 4, the condition `4 < 5` is true, and `count` is incremented to 5. The loop condition `5 < 5` then becomes false, and the loop terminates. The final value of `count` is 5.
A) Only once, before the first iteration.
B) After each iteration of the loop body.
C) Before each iteration, including the first one.
D) Only when a `break` statement is encountered.
Correct Answer: C
The provided content states: 'In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first.' This means the condition is checked prior to every potential execution of the loop's code.
A) The code will result in an infinite loop.
B) The number 10 will be printed.
C) The number 8 will be printed.
D) The code will not compile.
Correct Answer: B
The Boolean expression `x > 10` is evaluated before the first iteration. Since `x` is initialized to 10, the condition `10 > 10` is false. Therefore, the loop body is never executed. The program proceeds to the `println` statement and prints the current value of `x`, which is 10.
A) A loop where the Boolean expression is initially false.
B) A loop where the controlling Boolean expression never becomes false.
C) A loop that executes exactly one time.
D) A loop that contains a nested conditional statement.
Correct Answer: B
The provided content defines an infinite loop as occurring 'when the Boolean expression in an iterative statement always evaluates to true.' If the expression can never become false, the loop will never terminate.
A) "Hello" is printed 5 times.
B) "Hello" is printed 4 times.
C) The code results in an infinite loop.
D) The code does not print anything.
Correct Answer: C
The variable `i` is initialized to 1, and the loop condition `i <= 5` is true. However, the value of `i` is never changed inside the loop body. As a result, the condition `i <= 5` will always be true, causing the loop to execute indefinitely. This is an infinite loop.
A) 15
B) 4
C) 3
D) 0
Correct Answer: C
1. Start: `total` is 15. `15 > 4` is true. `total` becomes `15 - 4 = 11`. 2. Iteration 2: `total` is 11. `11 > 4` is true. `total` becomes `11 - 4 = 7`. 3. Iteration 3: `total` is 7. `7 > 4` is true. `total` becomes `7 - 4 = 3`. 4. Iteration 4: `total` is 3. `3 > 4` is false. The loop terminates. The final value of `total` is 3.
A) achieve a result by performing a task a repeated number of times.
B) select one outcome from two or more possibilities.
C) call a method from a different class.
D) declare a variable that will not change its value.
Correct Answer: A
The content states that an iterative process is used to 'achieve a desired result' by 'repeating a segment of code'. This directly aligns with performing a task a repeated number of times.
A) 8
B) 16
C) 32
D) 64
Correct Answer: C
Let's trace the values of `k` and `result`: - Before loop: `k=1`, `result=1`. - Iteration 1: `result < 50` (1<50) is true. `k` becomes 2. `result` becomes `1+2=3`. - Iteration 2: `result < 50` (3<50) is true. `k` becomes 4. `result` becomes `3+4=7`. - Iteration 3: `result < 50` (7<50) is true. `k` becomes 8. `result` becomes `7+8=15`. - Iteration 4: `result < 50` (15<50) is true. `k` becomes 16. `result` becomes `15+16=31`. - Iteration 5: `result < 50` (31<50) is true. `k` becomes 32. `result` becomes `31+32=63`. - After Iteration 5: `result < 50` (63<50) is false. The loop terminates. The final value of `k` is 32.
A) ```java while (num < 0) { num = num - 5; } ```
B) ```java while (num >= 0) { num = num - 5; } ```
C) ```java while (num > 0) { num = num - 5; } ```
D) ```java while (num <= 23) { num = num - 5; } ```
Correct Answer: C
The process should continue as long as `num` is greater than 0. The loop should stop when `num` is less than or equal to 0. The condition `while (num > 0)` correctly captures this. Option A's loop would never run. Option B would result in an extra subtraction when num is 0. Option D would lead to an infinite loop as `num` would always be less than 23.
A) Exactly once
B) One or more times
C) Zero or more times
D) At least twice
Correct Answer: C
The content explicitly states that an iteration statement repeats a segment of code 'zero or more times'. This is because the Boolean condition is checked before the first iteration, and if it's initially false, the loop body will not execute at all (zero times).
A) 20
B) 19
C) 18
D) 0
Correct Answer: C
The loop continues as long as `val` is not divisible by 3. - Start: `val` is 20. `20 % 3` is 2 (not 0), so the loop runs. `val` becomes 19. - Next: `val` is 19. `19 % 3` is 1 (not 0), so the loop runs. `val` becomes 18. - Next: `val` is 18. `18 % 3` is 0. The condition `0 != 0` is false. The loop terminates. The final value of `val`, which is 18, is printed.
A) 8
B) 16
C) 32
D) 64
Correct Answer: B
Let's trace the value of `power`: - Before loop: `power` is 1. - Iteration 1: `1 * 2 <= 30` is true. `power` becomes `1 * 2 = 2`. - Iteration 2: `2 * 2 <= 30` is true. `power` becomes `2 * 2 = 4`. - Iteration 3: `4 * 2 <= 30` is true. `power` becomes `4 * 2 = 8`. - Iteration 4: `8 * 2 <= 30` is true. `power` becomes `8 * 2 = 16`. - Iteration 5: `16 * 2 <= 30` (i.e., `32 <= 30`) is false. The loop terminates. The final value of `power` is 16.
A) ```java int x = 100; while (x > 0) { x = x / 2; } ```
B) ```java int y = 0; while (y < 10) { y = y + 2; } ```
C) ```java boolean done = false; while (!done) { done = true; } ```
D) ```java int z = 5; while (z > 0) { z = z + 1; } ```
Correct Answer: D
An infinite loop occurs when the loop's Boolean expression always evaluates to true. In option D, `z` starts at 5 and is always greater than 0. Inside the loop, `z` is incremented, so it will become 6, 7, 8, and so on, always remaining greater than 0. The condition will never become false. The other options have conditions that will eventually become false, terminating the loop.