Unit 2 Cycle 2 Day 9: Iteration (while-loop tracing)
Share
Unit 2, Selection & Iteration • Cycle 2
Day 9 Advanced Practice • Harder Difficulty
Focus: While Loop Tracing
Hard
Iteration (while-loop tracing)
Advanced Practice Question
Format: Iteration (while-loop tracing)
What is printed?
What is printed?
"apcs-keyword">int x = "apcs-number">20;
"apcs-keyword">int y = "apcs-number">0;
"apcs-keyword">while (x > "apcs-number">0)
{
x -= "apcs-number">6;
y++;
}
System.out.println(y);
Difficulty: Hard |
Topic: Iteration (while-loop tracing) |
Cycle: 2 (Advanced)
Why This Answer?
x goes 20 → 14 → 8 → 2 → -4. The loop ran 4 times, so y is 4.
Common Mistake
Watch Out!
Assuming the loop stops when x becomes 2 (it stops only after the next subtraction makes x ≤ 0).
AP Exam Strategy
Track x after each update; the condition is checked at the top each time.
Master This Topic
This Cycle 2 HARD question tests while loop tracing. Review Unit 2 concepts to build mastery of selection and iteration.
- Understanding while loop tracing
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs