PrepGo

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

Which of the following best describes the primary purpose of an iterative statement like a while loop?

All Questions (14)

Which of the following best describes the primary purpose of an iterative statement like a while loop?

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.

What is the value of `count` after the following code segment is executed? ```java int count = 0; while (count < 5) { count++; } ```

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.

According to the definition of a while loop, when is the Boolean expression evaluated?

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.

What is the result of executing the following code segment? ```java int x = 10; while (x > 10) { x = x - 2; } System.out.println(x); ```

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.

Which of the following scenarios best describes the cause of an infinite loop?

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.

Consider the following code segment. What is the most likely outcome? ```java int i = 1; while (i <= 5) { System.out.println("Hello"); } ```

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.

What is the final value of `total` after this code runs? ```java int total = 15; int step = 4; while (total > step) { total = total - step; } ```

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.

An iterative process is required when a programmer needs to...

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.

What is the value of `k` when the following loop terminates? ```java int k = 1; int result = 1; while (result < 50) { k = k * 2; result = result + k; } ```

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 programmer wants to write code that repeatedly subtracts 5 from a variable `num` until `num` is less than or equal to 0. If `num` starts at 23, which code segment correctly represents this process?

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.

An iteration statement, such as a while loop, will repeat its body of code how many times?

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).

What is printed by the following code segment? ```java int val = 20; while (val % 3 != 0) { val--; } System.out.println(val); ```

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.

Consider the following code. What is the final value of `power`? ```java int base = 2; int power = 1; while (power * base <= 30) { power = power * base; } ```

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.

Which of the following code segments will result in an infinite loop?

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.