PrepGo

AP Computer Science Principles Practice Quiz: Iteration

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 16 questions to check your progress.

Question 1 of 16

Which of the following best defines iteration within an algorithm?

All Questions (16)

Which of the following best defines iteration within an algorithm?

A) A single execution of a set of statements in order.

B) A decision-making step that chooses between two or more paths.

C) A repeating portion of an algorithm.

D) A statement that ends the algorithm's execution.

Correct Answer: C

Based on the provided content, 'Iteration is a repeating portion of an algorithm.' The other options describe sequence (A), selection (B), or termination.

An algorithm needs to perform a task a predetermined number of times. According to the exam reference sheet, which structure is most suitable?

A) REPEAT UNTIL(condition)

B) REPEAT n TIMES

C) IF (condition)

D) SEQUENTIAL

Correct Answer: B

The content states that 'The exam reference sheet provides the REPEAT n TIMES structure, in which a block of statements is executed n times.' This is designed for a fixed number of repetitions.

Consider the following code segment: value <- 10 REPEAT 3 TIMES { value <- value - 2 } What is the final value of `value` after the iteration completes?

A) 10

B) 8

C) 6

D) 4

Correct Answer: D

The loop executes 3 times. 1st iteration: value becomes 10 - 2 = 8. 2nd iteration: value becomes 8 - 2 = 6. 3rd iteration: value becomes 6 - 2 = 4. The final value is 4.

In a `REPEAT UNTIL(condition)` loop, when does the repetition of the statement block stop?

A) When the Boolean expression `condition` evaluates to false.

B) When the Boolean expression `condition` evaluates to true.

C) After a fixed number of iterations has been completed.

D) When an error occurs within the loop's body.

Correct Answer: B

The provided content specifies that for the `REPEAT UNTIL(condition)` structure, 'a block of statements is repeated until the Boolean expression condition evaluates to true.'

What is the primary cause of an infinite loop in a `REPEAT UNTIL(condition)` iteration?

A) The condition is true before the loop begins.

B) The loop body contains too many statements.

C) The ending condition will never evaluate to true.

D) The loop repeats more than a million times.

Correct Answer: C

The content explicitly states, 'In REPEAT UNTIL(condition) iteration, an infinite loop occurs when the ending condition will never evaluate to true.'

Consider the following algorithm: x <- 20 REPEAT UNTIL (x <= 20) { x <- x + 5 } What is the result of executing this code segment?

A) The final value of x is 20.

B) The final value of x is 25.

C) The code results in an infinite loop.

D) The loop body is not executed at all.

Correct Answer: D

The condition `x <= 20` is checked before the loop begins. Since `x` is initially 20, the condition `20 <= 20` is true. As stated in the content, 'if the conditional evaluates to true initially, the loop body is not executed at all.' Therefore, the statements inside the loop are skipped.

How do iteration statements affect the normal sequential flow of control in a program?

A) They guarantee that every statement is executed exactly once.

B) They repeat a set of statements, breaking the one-after-another execution.

C) They allow the program to skip a set of statements entirely and then terminate.

D) They have no effect on the sequential flow of control.

Correct Answer: B

The content states that 'Iteration statements change the sequential flow of control by repeating a set of statements zero or more times.' This repetition is a change from the standard top-to-bottom sequential flow.

Consider the following code segment: count <- 1 REPEAT UNTIL (count > 4) { count <- count * 2 } What is the final value of `count` after the loop terminates?

A) 2

B) 4

C) 8

D) 16

Correct Answer: C

1st check: `1 > 4` is false. Loop runs. count becomes 1 * 2 = 2. 2nd check: `2 > 4` is false. Loop runs. count becomes 2 * 2 = 4. 3rd check: `4 > 4` is false. Loop runs. count becomes 4 * 2 = 8. 4th check: `8 > 4` is true. The loop terminates. The final value of count is 8.

A task in an algorithm is to wash dishes until the sink is empty. Which of the following best expresses this task using iteration without using a programming language?

A) Repeat washing one dish 10 times.

B) If the sink is empty, stop washing.

C) Repeat the action of washing a dish until the condition 'sink is empty' is met.

D) First, wash all plates, then wash all cups.

Correct Answer: C

This question tests the ability to express an algorithm with iteration. The process continues until a condition is met, which is the core idea of a `REPEAT UNTIL` loop. Option C accurately describes this conditional repetition.

Which of the following code segments results in an infinite loop?

A) n <- 0 REPEAT UNTIL (n = 10) { n <- n + 1 }

B) n <- 10 REPEAT UNTIL (n < 0) { n <- n - 1 }

C) n <- 0 REPEAT UNTIL (n > 0) { n <- n - 1 }

D) n <- 0 REPEAT 10 TIMES { n <- n - 1 }

Correct Answer: C

In option C, `n` starts at 0. The condition to stop is `n > 0`. Inside the loop, `n` is decreased (`n - 1`), so it becomes -1, -2, etc. It will never become greater than 0, so the condition will never be true, causing an infinite loop.

The phrase 'repeating a set of statements zero or more times' implies which of the following is possible for an iteration statement?

A) The loop must always execute at least one time.

B) The statements inside the loop might never be executed.

C) All loops are guaranteed to eventually stop.

D) Iteration can only be used with numeric variables.

Correct Answer: B

The possibility of 'zero' repetitions means that under certain conditions (like the stopping condition being met initially), the body of the loop can be skipped entirely. This is a key feature of the `REPEAT UNTIL` loop described.

Consider the following algorithm: display_str <- "" REPEAT 4 TIMES { display_str <- CONCAT(display_str, "*") DISPLAY(display_str) } What is the side effect (i.e., what is displayed) of this code?

A) It displays "****" one time.

B) It displays "*", then "**", then "***", then "****".

C) It displays "*" four times.

D) It displays nothing.

Correct Answer: B

The `DISPLAY` command is inside the loop. 1st iteration: display_str becomes "*", displays "*". 2nd iteration: display_str becomes "**", displays "**". 3rd iteration: display_str becomes "***", displays "***". 4th iteration: display_str becomes "****", displays "****".

What is a key difference between the `REPEAT n TIMES` and `REPEAT UNTIL(condition)` structures as described?

A) `REPEAT n TIMES` can run zero times, but `REPEAT UNTIL` must run at least once.

B) The number of repetitions is known before a `REPEAT n TIMES` loop begins, whereas for `REPEAT UNTIL` it is often unknown.

C) `REPEAT n TIMES` changes the flow of control, but `REPEAT UNTIL` does not.

D) Only `REPEAT n TIMES` can result in an infinite loop.

Correct Answer: B

`REPEAT n TIMES` is defined by a specific number of iterations. `REPEAT UNTIL(condition)` runs as many times as it takes for the condition to become true, which may not be known in advance. Both can run zero times, both change the flow of control, and only `REPEAT UNTIL` can cause an infinite loop.

An algorithm is designed to find the first even number in a list of numbers, starting from the first element. Which iterative approach is most appropriate?

A) A `REPEAT n TIMES` loop, where n is the total number of elements in the list.

B) A `REPEAT UNTIL (current number is even OR end of list is reached)` loop.

C) A `REPEAT 100 TIMES` loop, assuming the list is not longer than 100.

D) An infinite loop that is manually stopped by the user.

Correct Answer: B

The goal is to stop as soon as the condition (finding an even number) is met. A `REPEAT UNTIL` loop is perfect for this, as it will stop iterating through the list once the desired element is found, which is more efficient than checking every single element with `REPEAT n TIMES` if the even number is found early.

According to the provided content, the condition in a `REPEAT UNTIL(condition)` loop is checked when?

A) After the loop body is executed at least once.

B) Before the loop body is executed.

C) In the middle of the loop body's execution.

D) Only after the loop has completed all iterations.

Correct Answer: B

The content explicitly states, 'if the conditional evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop.' This confirms a pre-execution check.

Consider the following code segment: x <- 10 y <- 0 REPEAT UNTIL (x = 0) { x <- x - 2 y <- y + 1 } What is the final value of `y`?

A) 0

B) 4

C) 5

D) 10

Correct Answer: C

The loop tracks how many times `x` is reduced by 2. 1. x=8, y=1 2. x=6, y=2 3. x=4, y=3 4. x=2, y=4 5. x=0, y=5 At this point, the condition `x = 0` is true, and the loop terminates. The final value of `y` is 5.