AP CSA Unit-Level MCQ

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.

Code Tracing 01 - Accumulators and Counters: track the final printed value.

int[] data = {4, 9, 2, 7, 5, 1};
int sum = 0;
int count = 0;
for (int v : data) {
  if (v > 2) {
    sum += v;
    count++;
  }
}
System.out.print(sum + ":" + count);

All 15 accumulators and counters questions

Work through the interactive quiz above first. This is the full accumulators and counters question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - Accumulators and Counters: track the final printed value.

    int[] data = {4, 9, 2, 7, 5, 1};
    int sum = 0;
    int count = 0;
    for (int v : data) {
      if (v > 2) {
        sum += v;
        count++;
      }
    }
    System.out.print(sum + ":" + count);
    • 28:4
    • 27:4
    • 25:4
    • 25:6
    Show worked solution

    Correct answer: 25:4

    Only values passing the test are added, and the counter advances on exactly the same passes as the sum.

  2. 2.Code Tracing 12 - Accumulators and Counters: follow the variable updates.

    int[] data = {6, 7, 4, 9, 7, 3};
    int max = data[0];
    for (int i = 1; i < data.length; i++) {
      if (data[i] > max) max = data[i];
    }
    System.out.print(max);
    • 6
    • 3
    • 8
    • 9
    Show worked solution

    Correct answer: 9

    Seeding the running maximum with the first element (not 0) is what makes this work for any set of values.

  3. 3.Code Tracing 28 - Accumulators and Counters: trace the branch and loop path.

    int[] data = {9, 7, 2, 8, 10, 6};
    int minIndex = 0;
    for (int i = 1; i < data.length; i++) {
      if (data[i] < data[minIndex]) minIndex = i;
    }
    System.out.print(minIndex + ":" + data[minIndex]);
    • 0:9
    • 3:2
    • The code does not compile.
    • 2:2
    Show worked solution

    Correct answer: 2:2

    The accumulator here tracks an index rather than a value, so the reported number is a position and the lookup happens at the end.

  4. 4.Code Tracing 39 - Accumulators and Counters: watch the index changes.

    int[] data = {11, 8, 4, 10, 6, 8};
    int evens = 0;
    int odds = 0;
    for (int v : data) {
      if (v % 2 == 0) evens += v;
      else odds += v;
    }
    System.out.print(evens + "/" + odds);
    • 36/11
    • 11/36
    • 47/0
    • 36/36
    Show worked solution

    Correct answer: 36/11

    Two accumulators run side by side, and every element updates exactly one of them.

  5. 5.Code Tracing 50 - Accumulators and Counters: evaluate the state change step by step.

    int[] data = {13, 9, 6, 8, 8, 10};
    int total = 0;
    for (int v : data) total += v;
    int avg = total / data.length;
    System.out.print(total + ":" + avg);
    • 9:54
    • 54:9
    • 54:9.0
    • 54:10
    Show worked solution

    Correct answer: 54:9

    Because both operands are int, the average uses integer division and any fractional part is discarded.

  6. 6.What is printed by the following code segment?

    int[] a = {4, -2, 7, -5, 3};
    int pos = 0;
    int neg = 0;
    for (int v : a)
    {
        if (v > 0) { pos += v; }
        else { neg += v; }
    }
    System.out.print(pos + " " + neg);
    • 7 -7
    • 14 7
    • 7 -14
    • 14 -7
    Show worked solution

    Correct answer: 14 -7

    Two accumulators run side by side, one collecting the positives and one the negatives.

  7. 7.What is printed by the following code segment?

    int[] a = {3, 3, 5, 5, 5, 2};
    int best = 1;
    int run = 1;
    for (int i = 1; i < a.length; i++)
    {
        run = a[i] == a[i - 1] ? run + 1 : 1;
        if (run > best) { best = run; }
    }
    System.out.print(best);
    • 3
    • 2
    • 6
    • 1
    Show worked solution

    Correct answer: 3

    The longest run of equal adjacent values is the three fives.

  8. 8.What is printed by the following code segment?

    String s = "a1b22c333";
    int d = 0;
    int l = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (Character.isDigit(s.charAt(i))) { d++; }
        else { l++; }
    }
    System.out.print(l + ":" + d);
    • 6:3
    • 3:6
    • 9:0
    • 3:3
    Show worked solution

    Correct answer: 3:6

    Three letters and six digit characters appear in the string.

  9. 9.What is printed by the following code segment?

    int[] a = {5, 1, 4, 2, 3};
    int c = 0;
    for (int i = 1; i < a.length; i++)
    {
        if (a[i] > a[i - 1]) { c++; }
    }
    System.out.print(c);
    • 3
    • 4
    • 2
    • 1
    Show worked solution

    Correct answer: 2

    The rises occur from 1 to 4 and from 2 to 3.

  10. 10.What is printed by the following code segment?

    int t = 0;
    int c = 0;
    for (int i = 1; i <= 10; i++)
    {
        if (i % 3 == 0) { t += i; c++; }
    }
    System.out.print(t / c);
    • 18
    • 3
    • 5
    • 6
    Show worked solution

    Correct answer: 6

    The multiples of 3 are 3, 6, and 9, whose total is 18 and whose average is exactly 6.

  11. 11.What is printed by the following code segment?

    int[] a = {2, 8, 4, 6};
    int max = a[0];
    int min = a[0];
    for (int v : a)
    {
        if (v > max) { max = v; }
        if (v < min) { min = v; }
    }
    System.out.print(max - min);
    • 6
    • 8
    • 2
    • 10
    Show worked solution

    Correct answer: 6

    The largest is 8 and the smallest is 2, so the range is 6.

  12. 12.What is printed by the following code segment?

    int p = 1;
    int c = 0;
    for (int i = 1; i <= 6; i++)
    {
        if (i % 2 == 1) { p *= i; c++; }
    }
    System.out.print(p + " " + c);
    • 15 6
    • 15 3
    • 48 3
    • 720 6
    Show worked solution

    Correct answer: 15 3

    Only the odd values 1, 3, and 5 are multiplied, and the counter records how many were used.

  13. 13.What is printed by the following code segment?

    String s = "hello world";
    int w = 0;
    boolean inWord = false;
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) != ' ')
        {
            if (!inWord) { w++; }
            inWord = true;
        }
        else { inWord = false; }
    }
    System.out.print(w);
    • 1
    • 11
    • 2
    • 3
    Show worked solution

    Correct answer: 2

    A flag records whether the traversal is inside a word, so each new word is counted once.

  14. 14.What is printed by the following code segment?

    int[] a = {1, 2, 3, 4, 5, 6};
    int even = 0;
    int odd = 0;
    for (int v : a)
    {
        if (v % 2 == 0) { even += v; }
        else { odd += v; }
    }
    System.out.print(even - odd);
    • -3
    • 21
    • 9
    • 3
    Show worked solution

    Correct answer: 3

    The even total is 12 and the odd total is 9.

  15. 15.What is printed by the following code segment?

    int run = 0;
    int best = 0;
    int[] a = {1, 0, 1, 1, 0, 1, 1, 1};
    for (int v : a)
    {
        run = v == 1 ? run + 1 : 0;
        if (run > best) { best = run; }
    }
    System.out.print(best);
    • 3
    • 2
    • 5
    • 8
    Show worked solution

    Correct answer: 3

    The longest run of ones is the final group of three.