Skip to main content

Unit 2 · Selection and Iteration

Accumulators and Counters

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What accumulators and counters covers

An accumulator builds a running total across a loop, while a counter tracks how many times a condition was met. Both share the same structure: declare and initialize before the loop, update conditionally inside it, and read the result after the loop ends. Sum accumulators start at 0, product accumulators start at 1, and counters start at 0.

Where students lose points

Initializing inside the loop resets the value every iteration, which is the single most common defect. Students also update the counter outside the if statement, counting every element rather than only the matching ones, and they compute averages using the loop bound instead of the counter, which is wrong whenever the loop skips values.

How it shows up on the AP exam

Unit 2 material that carries directly into Unit 4 array and ArrayList traversals. Accumulator and counter logic underpins a large share of FRQ part A and part B tasks.

For practice use only.

Accumulators and Counters MCQ Practice

AP CSA accumulator and counter practice: summing, counting matches, tracking running values, and placing initialization and updates correctly in a loop.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Which option matches the complete output produced by this segment?

public class AccumulatorsAndCountersContext
{
    public static void runExample()
    {
        int sum = 0;
        for (int i = 1; i <= 4; i++)
        {
            sum += i;
        }
        System.out.print(sum);
    }

    public static void main(String[] args)
    {
        runExample();
    }
}
Choose one answer.