AP CSA Practice Test: Loops
AP CSA Practice Test: Loops
Unit 2: Selection and Iteration | 10 Questions | AP Computer Science A
for (int i = 1; i <= 5; i++) {
if (i % 2 != 0)
System.out.print(i + " ");
}
Explanation: The loop runs for i = 1, 2, 3, 4, 5. The condition i % 2 != 0 is true for odd numbers: 1, 3, 5.
Common Mistake: Forgetting i=5. The condition is i <= 5, which includes 5.
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
}
Explanation: i=0: j goes 0 (1 star). i=1: j goes 0,1 (2 stars). i=2: j goes 0,1,2 (3 stars). i=3: j goes 0,1,2,3 (4 stars). Total: 1+2+3+4 = 10.
Common Mistake: Assuming the inner loop runs the same number of times each iteration.
sum after this loop?int sum = 0;
for (int i = 10; i > 0; i -= 3) {
sum += i;
}
Explanation: i starts at 10, decreases by 3 each iteration: i=10 (sum=10), i=7 (sum=17), i=4 (sum=21), i=1 (sum=22). Next i=-2 fails i > 0. Sum = 22.
Common Mistake: Stopping too early at i=4 or miscounting the iterations.
int x = 1;
while (x < 100) {
x *= 2;
}
System.out.println(x);
Explanation: x doubles each iteration: 1, 2, 4, 8, 16, 32, 64, 128. At x=128, the condition 128 < 100 is false, so the loop stops. x is now 128.
Common Mistake: Choosing 64. The loop body executes when x=64 (since 64 < 100), setting x to 128.
for (int i = 0; i < 3; i++) {
for (int j = 3; j > i; j--) {
System.out.print("#");
}
System.out.println();
}
Explanation: i=0: j goes 3,2,1 (3 hashes). i=1: j goes 3,2 (2 hashes). i=2: j goes 3 (1 hash). Result: ###, ##, #.
Common Mistake: Miscounting the inner loop bounds. j starts at 3 and goes down to i+1.
I.
for (int i = 0; i < 10; i++)II.
for (int i = 0; i <= 10; i++)III.
for (int i = 1; i <= 10; i++)
Explanation: I: iterates 0-9 (correct). II: iterates 0-10 (11 values, includes 10). III: iterates 1-10 (misses 0, includes 10). Both II and III have off-by-one errors.
Common Mistake: Only catching one of the two errors.
int n = 12345;
int count = 0;
while (n > 0) {
if (n % 10 % 2 == 0)
count++;
n /= 10;
}
Explanation: Extracts digits right-to-left: 5 (odd), 4 (even, count=1), 3 (odd), 2 (even, count=2), 1 (odd). Two even digits.
Common Mistake: Including 0 as an even digit or miscounting. The digits are 1,2,3,4,5.
for (int i = 0; i < 5; i++) {
if (i == 3)
continue;
System.out.print(i + " ");
}
Explanation: continue skips the rest of the current iteration when i=3, but the loop continues with i=4. Output: 0 1 2 4.
Common Mistake: Confusing continue with break. break exits the loop; continue skips to the next iteration.
int i = 1;
while (i <= 1000) {
i *= 3;
}
Explanation: i values: 1, 3, 9, 27, 81, 243, 729, 2187. The loop runs while i <= 1000: iterations at i=1,3,9,27,81,243,729 (7 iterations). When i becomes 2187, the condition fails.
Common Mistake: Miscounting by including or excluding the boundary iteration.
String s = "abcabc";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.substring(i, i+1).equals("a"))
count++;
}
System.out.println(count);
Explanation: The string "abcabc" has 'a' at index 0 and index 3. count increments twice.
Common Mistake: Using == instead of .equals() for string comparison. This code correctly uses .equals().
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]