Unit 2 Cycle 2 Day 28: Iteration (nested loop accumulation)

Unit 2, Selection & Iteration • Cycle 2
Day 28 Advanced Practice • Harder Difficulty
Focus: Variable Inner Bound Hard Iteration (nested loop accumulation)

Advanced Practice Question

Format: Iteration (nested loop accumulation)

What is printed?
"apcs-keyword">int x = "apcs-number">0;

"apcs-keyword">for ("apcs-keyword">int i = "apcs-number">1; i <= "apcs-number">3; i++)
{
    "apcs-keyword">for ("apcs-keyword">int j = "apcs-number">1; j <= i; j++)
    {
        x++;
    }
}

System.out.println(x);
Difficulty: Hard  |  Topic: Iteration (nested loop accumulation)  |  Cycle: 2 (Advanced)
Why This Answer?

x increments 1 time when i=1, 2 times when i=2, and 3 times when i=3. Total = 1+2+3 = 6.

Common Mistake
Watch Out!

Assuming inner loop always runs 3 times instead of depending on i.

AP Exam Strategy

When the inner bound depends on i, compute iterations for each i separately.

Master This Topic

This Cycle 2 HARD question tests variable inner bound. Review Unit 2 concepts to build mastery of selection and iteration.

  • Understanding variable inner bound
  • Tracing code execution accurately
  • Avoiding common pitfalls
View Unit 2 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.