AP CSA Unit 2 Day 12: Debugging Loop Condition
Share
Unit 2, Selection & Iteration • Cycle 2
Day 12 Advanced Practice • Harder Difficulty
Focus: Loop Bounds
Hard
Debugging (Loop condition)
Advanced Practice Question
Format: Debugging (Loop condition)
A programmer wants to print all integers from 1 to 5 inclusive. Which change fixes the code?
A programmer wants to print all integers from 1 to 5 inclusive. Which change fixes the code?
int i = 0;
while (i <= 5) {
System.out.print(i + " ");
i++;
}
Difficulty: Hard |
Topic: Debugging (Loop condition) |
Cycle: 2 (Advanced)
Why This Answer?
The current loop stops when i becomes 5, so it prints 1–4. Using `i <= 5` includes 5.
Common Mistake
Watch Out!
Changing the loop type without fixing the condition doesn't guarantee correctness.
AP Exam Strategy
For "inclusive" loops, check whether your condition should use `<` or `<=`.
Master This Topic
This Cycle 2 HARD question tests loop bounds. Review Unit 2 concepts to build mastery of selection and iteration.
- Understanding loop bounds
- 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