Counting loops are the most frequently used iteration structure in Java programming and appear extensively on the AP Computer Science A exam. Topic 4.2 focuses on the for loop syntax, which consolidates initialization, condition checks, and increment/decrement updates into a single concise header.
In this interactive practice set, we cover Topic 4.2: 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 (e.g.,for (int i = 0; ...)) is local to the loop block 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 4.2: 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 solely to the loop block. 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 (10 > 0 true) -> count = 1, i becomes 5.–
i = 5 (5 > 0 true) -> count = 2, i becomes 2.–
i = 2 (2 > 0 true) -> count = 3, i becomes 1.–
i = 1 (1 > 0 true) -> count = 4, i becomes 0.–
i = 0 (0 > 0 false) -> loop ends, printing 4.i starts at 0 and decrements (i--) on each iteration. Because i becomes negative (-1, -2, etc.), it moves further away from 10, meaning 0 < 10 remains permanently true and creates an infinite loop.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 gives “cmue”.
for (int i = 1; i <= 4; i++) {
p *= i;
}
System.out.println(p);
– i=1: p = 1 * 1 = 1
– i=2: p = 1 * 2 = 2
– i=3: p = 2 * 3 = 6
– i=4: p = 6 * 4 = 24
Prints 24.
for (int i = 0; i < 3; i++) {
val += i;
}
System.out.println(val);
– Start: val = 5
– i = 0: val = 5 + 0 = 5
– i = 1: val = 5 + 1 = 6
– i = 2: val = 6 + 2 = 8
Loop terminates when i reaches 3, printing 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 always executes after the loop body, not before the condition check.Unit 4.2 For Loops Practice Complete!
Why For Loop Mastery Matters for the Exam
On the AP CSA exam, for loops form the backbone of both multiple-choice questions and algorithmic Free Response Questions (FRQs). Knowing how to manipulate loop boundaries, handle counting decrements, and recognize scope limitations will save you vital time and prevent careless compilation errors.
Ready for the Next Topic?
Now that you have mastered counting loops, it is time to look at how loops are applied to real-world tasks like string character processing and digit manipulation.
Continue your AP CSA review by proceeding to Topic 4.3: Loop Algorithms Practice, where we examine accumulator patterns and reverse character tracing.