AP CSP 3.8 Exercise 2: Iteration Applied Challenge

Big Idea 3: Algorithms and Programming · Topic 3.8 · Exercise 2

Iteration: Applied Challenge

Six loops. Count the iterations before you count anything else.

How this one is different

Off by one is the single most common loop error on the exam. Every question here can be settled by counting passes carefully.

Applied Practice

6 questions · scenario driven · every answer is recorded for your teacher

Question 1 of 6Trace
What does this segment display?
i <- 1 total <- 0 REPEAT UNTIL (i > 4) { total <- total + i i <- i + 1 } DISPLAY total
Incorrect. 15 would include 5, but the loop exits before adding it.
Incorrect. 6 stops one iteration early, at 1 plus 2 plus 3.
Incorrect. 4 is the last value added, not the running total.
Correct. The loop adds 1, 2, 3, and 4 before i reaches 5 and the condition stops it, giving 10.
Question 2 of 6Trace
How many times does the body of this loop execute?
count <- 0 REPEAT 5 TIMES { count <- count + 1 }
Incorrect. The body changing count has no effect on how many times a REPEAT n TIMES loop runs.
Incorrect. 4 undercounts. REPEAT n TIMES runs exactly n passes.
Correct. REPEAT 5 TIMES is an exact count, independent of anything in the body.
Incorrect. 6 overcounts by one, which is the usual result of counting the check that ends the loop as a pass.
Question 3 of 6Debug
A loop never ends.
Which change fixes it?
i <- 1 REPEAT UNTIL (i > 3) { DISPLAY i }
Incorrect. The condition is fine. Nothing in the body ever changes i, so no comparison would ever become true.
Correct. A condition based loop needs the body to make progress toward the exit condition. Incrementing i is that progress.
Incorrect. Removing the display leaves an empty loop that still never ends.
Incorrect. This does terminate, but it is a different loop rather than a fix to the described bug, and it still never advances i.
Question 4 of 6Apply
Which loop displays exactly the values 1, 2, and 3?
Correct. Starting at 1 and stopping once i passes 3 displays exactly 1, 2, and 3.
Incorrect. The loop exits as soon as i reaches 3, so 3 is never displayed.
Incorrect. This continues through 4.
Incorrect. Starting at 0 displays 0, 1, 2, and 3.
Question 5 of 6Trace
What does this segment display?
n <- 16 steps <- 0 REPEAT UNTIL (n = 1) { n <- n / 2 steps <- steps + 1 } DISPLAY steps
Incorrect. 8 is the value after one halving, not the step count.
Incorrect. 16 is the starting value.
Incorrect. 2 stops after reaching 4.
Correct. 16 halves to 8, 4, 2, and 1, which is four steps.
Question 6 of 6Analyze
A loop processes every element of a list whose length changes at runtime.
Which loop form is appropriate, and why?
Incorrect. Loops handle unknown counts routinely, which is much of what they are for.
Incorrect. A fixed count either runs past the end of a short list or misses part of a long one.
Correct. FOR EACH is defined in terms of the list itself, so the iteration count follows the data rather than a constant written in the code.
Incorrect. A loop with no exit condition never terminates.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]