AP CSA Practice Test: Loops

AP CSA Practice Test: Loops

Unit 2: Selection and Iteration | 10 Questions | AP Computer Science A

Question 1
What is the output?
for (int i = 1; i <= 5; i++) {
    if (i % 2 != 0)
        System.out.print(i + " ");
}
A"1 3 5 "
B"2 4 "
C"1 2 3 4 5 "
D"1 3 "

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.

Question 2
How many times does "*" print?
for (int i = 0; i < 4; i++) {
    for (int j = 0; j <= i; j++) {
        System.out.print("*");
    }
}
A4
B10
C6
D16

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.

Question 3
What is the value of sum after this loop?
int sum = 0;
for (int i = 10; i > 0; i -= 3) {
    sum += i;
}
A22
B18
C25
D15

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.

Question 4
What is the output?
int x = 1;
while (x < 100) {
    x *= 2;
}
System.out.println(x);
A64
B128
C100
D256

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.

Question 5
What does this code print?
for (int i = 0; i < 3; i++) {
    for (int j = 3; j > i; j--) {
        System.out.print("#");
    }
    System.out.println();
}
A3 rows: ###, ##, #
B3 rows: #, ##, ###
C3 rows: ###, ###, ###
D4 rows: ####, ###, ##, #

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.

Question 6
Which loop has an off-by-one error if the goal is to print indices 0 through 9?

I. for (int i = 0; i < 10; i++)
II. for (int i = 0; i <= 10; i++)
III. for (int i = 1; i <= 10; i++)
AI only
BII and III only
CII only
DI and III only

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.

Question 7
What is the output?
int n = 12345;
int count = 0;
while (n > 0) {
    if (n % 10 % 2 == 0)
        count++;
    n /= 10;
}
A2
B3
C1
D5

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.

Question 8
What is printed?
for (int i = 0; i < 5; i++) {
    if (i == 3)
        continue;
    System.out.print(i + " ");
}
A"0 1 2 4 "
B"0 1 2 3 "
C"0 1 2 "
D"3 "

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.

Question 9
How many iterations does this loop execute?
int i = 1;
while (i <= 1000) {
    i *= 3;
}
A6
B7
C8
D1000

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.

Question 10
What is the output?
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);
A1
B2
C3
D6

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().

0% 0/10

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]