Counting loops are an important Java iteration structure for AP Computer Science A questions involving repetition, counting, traversal, and algorithmic reasoning. Topic 2.8 focuses on for loops, including initialization, Boolean condition checks, and update expressions.
In this interactive practice set, we cover Topic 2.8: For Loops. These 10 multiple-choice questions test your ability to trace counting sequences, manage loop control variable scope, and identify boundary conditions without an IDE.
Key Concepts to Remember Before You Start:
-
Header Anatomy:
A
forloop header consists of three parts: initialization (runs once), Boolean condition (checked before each iteration), and update expression (runs after the loop body). -
Variable Scope:
A loop control variable declared inside the
forheader (for example,for (int i = 0; ...)) is local to the loop and cannot be accessed outside of it. -
Step Sizes and Countdown Loops:
Iteration step values can increment by custom amounts
(
i += 2) or decrement backward (i--). -
Off-By-One Traps:
Carefully verify whether boundaries use strict inequalities
(
<) or inclusive inequalities (<=) to determine exact iteration counts.
Test your knowledge with the interactive quiz below. Review the explanations for any questions you miss to strengthen your mental tracing accuracy.
AP CSA 2.8: For Loops Practice
"Java" printed to the console?System.out.print(“Java “);
}
i takes values 1, 3, 5, and 7.
Each of these satisfies i < 8, printing
“Java ” 4 times. When i becomes 9,
9 < 8 is false, terminating the loop.total after the loop finishes executing?for (int i = 5; i > 0; i–) {
if (i % 2 == 0) {
total += i;
}
}
System.out.println(total);
i from 5 down to 1.
The even values of i in this range are 4 and 2.
Adding these together gives 4 + 2 = 6.System.out.print(i + ” “);
}
System.out.println(i);
i is declared inside the for
loop header, its scope is restricted to the loop.
Attempting to reference i in
System.out.println(i); outside the loop causes
a compile-time error.for (int i = 1; i <= 6; i += 2) {
sum += i;
}
System.out.println(sum);
i takes odd values:
1, 3, and 5. When i becomes 7,
7 <= 6 is false.
Summing these values gives 1 + 3 + 5 = 9.for (int i = 10; i > 0; i /= 2) {
count++;
}
System.out.println(count);
i = 10 → count = 1, i becomes 5.i = 5 → count = 2, i becomes 2.i = 2 → count = 3, i becomes 1.i = 1 → count = 4, i becomes 0.i = 0 → condition is false and the loop ends.Therefore, the loop executes 4 times.
for loop headers will result
in an infinite loop?i starts at 0 and decreases on every
iteration. It becomes -1, -2, -3, and so on.
Because every negative value is still less than 10,
the condition i < 10 remains true and the loop
never terminates.String result = “”;
for (int i = 0; i < s.length(); i += 2) {
result += s.substring(i, i + 1);
}
System.out.println(result);
i by 2, accessing characters
at indices 0, 2, 4, and 6:Index 0:
'c'Index 2:
'm'Index 4:
'u'Index 6:
'e'Combining these characters gives
"cmue".for (int i = 1; i <= 4; i++) {
p *= i;
}
System.out.println(p);
i = 1: p = 1 × 1 = 1i = 2: p = 1 × 2 = 2i = 3: p = 2 × 3 = 6i = 4: p = 6 × 4 = 24Therefore, the program prints
24.for (int i = 0; i < 3; i++) {
val += i;
}
System.out.println(val);
Start:
val = 5i = 0: val = 5 + 0 = 5i = 1: val = 5 + 1 = 6i = 2: val = 6 + 2 = 8The loop terminates when
i reaches 3,
so the program prints 8.for loops in Java is FALSE?for loop is:1) Initialization
2) Boolean condition check
3) Loop body execution
4) Update expression
The update expression executes after the loop body. The condition is then checked again before the next iteration.
Unit 2.8 For Loops Practice Complete!
Why For Loop Practice Matters for AP CSA
For loops are an important Java iteration structure for AP CSA questions involving repetition, counting, traversal, and algorithmic reasoning. Understanding loop boundaries, update expressions, variable scope, and trace patterns can help you solve code-tracing questions accurately.
Continue Your AP CSA Practice
Now that you have practiced for loops, continue with
loop-based algorithms involving accumulators, string processing,
and other common iteration patterns.
Continue your AP CSA review with Topics 2.9–2.10: Loop Algorithms Practice , where you can practice implementing selection and iteration algorithms and string algorithms.