Unit 2 Practice Exam: Selection and Iteration | AP CSA

AP CSA Practice Exam

Unit 2 Practice Exam: Selection and Iteration

50 Questions • 2025-2026 Curriculum • Unit 2: Boolean Expressions, if/else, for and while Loops (25-35% of exam)
0Correct
0Incorrect
0/50Progress
Exam Overview: Unit 2 covers 25-35% of the AP CSA exam — the single largest unit weight. The full AP CSA exam is 42 MCQ (55%) plus 4 FRQ (45%), delivered fully digitally via Bluebook on May 15, 2026 (afternoon session). Topics in this set: boolean expressions, short-circuit evaluation, if / if-else / if-else-if, for and while loops, and nested iteration. Formats: Error Detection, I/II/III multi-select, and Predict the Output — all at AP exam difficulty.
Section 1: Boolean Expressions and Comparisons
Predict the Output
Question 1
Before looking at the choices, trace the code and predict the output.
int p = 5;
int q = 10;
boolean r = (p > 3) && (q < 8);
System.out.println(r);
I/II/III Format
Question 2
Consider the following declarations:
int m = 7;
int n = 4;
Which of the following expressions evaluate to true?

I. (m > n) || (m == n)
II. !(m < n) && (n < 10)
III. (m + n == 11) && (m - n == 2)
Error Detection
Question 3
A student wants to check whether a value is between 1 and 10 inclusive. Which expression is WRITTEN INCORRECTLY?
Predict the Output
Question 4
Before looking at the choices, predict the output.
String w1 = new String("Code");
String w2 = new String("Code");
System.out.println(w1 == w2);
System.out.println(w1.equals(w2));
Error Detection
Question 5
Consider the following code segment. Will it run without exceptions, and why?
int numer = 10;
int denom = 0;
if (denom != 0 && numer / denom > 2) {
    System.out.println("Big");
} else {
    System.out.println("Small or zero");
}
I/II/III Format
Question 6
Which of the following expressions are equivalent to !(a && b) by De Morgan's Law?
I. !a && !b
II. !a || !b
III. (!a) || (!b)
Predict the Output
Question 7
Before looking at the choices, predict the output.
int j = 15;
boolean t = (j > 10) == (j < 20);
System.out.println(t);
Error Detection
Question 8
A student writes the following code intending to print Match when two Strings are equal. Will it work as intended?
String first = "yes";
String second = "Yes";
if (first == second) {
    System.out.println("Match");
} else if (first.equals(second)) {
    System.out.println("Case-match");
} else if (first.equalsIgnoreCase(second)) {
    System.out.println("Ignore-case match");
} else {
    System.out.println("No match");
}
Predict the Output
Question 9
Before looking at the choices, predict the output. Pay attention to evaluation order.
int k = 5;
if (k < 10 || ++k > 100) {
    System.out.println(k);
}
I/II/III Format
Question 10
Which of the following conditions are TRUE when a = 6 and b = 4?
I. a % 2 == 0 && b % 2 == 0
II. (a + b) % 2 != 0
III. a > b && a - b < 5
Section 2: if, if-else, and if-else-if Statements
Error Detection
Question 11
A student wants the code to print exactly one letter grade based on the score. Which version achieves this CORRECTLY?
Predict the Output
Question 12
Before looking at the choices, trace each branch carefully.
int s = 85;
if (s >= 90) {
    System.out.println("A");
}
if (s >= 80) {
    System.out.println("B");
}
if (s >= 70) {
    System.out.println("C");
}
Error Detection
Question 13
What does the following code print?
int v = 5;
if (v > 10)
    System.out.println("X");
    System.out.println("Y");
System.out.println("Z");
Predict the Output
Question 14
Before looking at the choices, trace the nested conditional.
int v = 15;
if (v > 10) {
    if (v > 20) {
        System.out.println("High");
    } else {
        System.out.println("Mid");
    }
} else {
    System.out.println("Low");
}
I/II/III Format
Question 15
Consider the following code with int p = 10, q = 20, r = 30. Which of the following will correctly identify the LARGEST value?
I. if (p > q && p > r) largest = p; else if (q > r) largest = q; else largest = r;
II. largest = p; if (q > largest) largest = q; if (r > largest) largest = r;
III. if (p > q) largest = p; if (r > largest) largest = r;
Predict the Output
Question 16
Before looking at the choices, predict the output.
int temp = 72;
boolean rain = false;
if (temp > 80 && !rain) {
    System.out.println("Beach");
} else if (temp > 60 || !rain) {
    System.out.println("Park");
} else {
    System.out.println("Home");
}
Error Detection
Question 17
A student writes the following code intending to print Positive when the value is strictly positive. What does it actually print when n = 0?
int n = 0;
if (n > 0) {
    System.out.println("Positive");
} else {
    System.out.println("Negative");
}
Predict the Output
Question 18
Before looking at the choices, trace the branch.
String letter = "B";
String msg;
if (letter.equals("A")) {
    msg = "Top";
} else if (letter.equals("B")) {
    msg = "Good";
} else if (letter.equals("C")) {
    msg = "Ok";
} else {
    msg = "Low";
}
System.out.println(msg);
Predict the Output
Question 19
Before looking at the choices, trace the leap-year logic carefully.
int yr = 2100;
boolean leap;
if (yr % 400 == 0) {
    leap = true;
} else if (yr % 100 == 0) {
    leap = false;
} else if (yr % 4 == 0) {
    leap = true;
} else {
    leap = false;
}
System.out.println(leap);
I/II/III Format
Question 20
Consider int x = 5. Which of the following code fragments print exactly YES (and nothing else)?
I. if (x > 0) System.out.println("YES");
II. if (x > 0) { System.out.println("YES"); } else { System.out.println("NO"); }
III. if (x > 0) System.out.println("YES"); System.out.println("NO");
Section 3: for Loops
Predict the Output
Question 21
Before looking at the choices, trace the loop.
int total = 0;
for (int i = 1; i <= 5; i++) {
    total += i;
}
System.out.println(total);
Error Detection
Question 22
A student writes this loop expecting it to print the numbers 1 through 10. What is WRONG?
for (int i = 1; i < 10; i++) {
    System.out.print(i + " ");
}
Predict the Output
Question 23
Before looking at the choices, predict the output.
int result = 1;
for (int i = 1; i <= 4; i++) {
    result *= i;
}
System.out.println(result);
I/II/III Format
Question 24
Which of the following loops print exactly 2 4 6 8 10 (separated by spaces)?
I. for (int i = 2; i <= 10; i += 2) System.out.print(i + " ");
II. for (int i = 1; i <= 5; i++) System.out.print((i * 2) + " ");
III. for (int i = 10; i >= 2; i -= 2) System.out.print(i + " ");
Predict the Output
Question 25
Before looking at the choices, trace the loop carefully.
int count = 0;
for (int i = 0; i < 10; i += 3) {
    count++;
}
System.out.println(count);
Error Detection
Question 26
A student wants to print the digits of a number from right to left. Which code CORRECTLY prints the digits of 1234 as 4 3 2 1?
Predict the Output
Question 27
Before looking at the choices, trace the loop.
int sum = 0;
for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) {
        sum += i;
    }
}
System.out.println(sum);
I/II/III Format
Question 28
Which of the following fragments print the numbers 1 2 3 4 5 in order (each separated by a space)?
I. for (int i = 1; i <= 5; i++) System.out.print(i + " ");
II. for (int i = 0; i < 5; i++) System.out.print((i + 1) + " ");
III. for (int i = 5; i >= 1; i--) System.out.print(i + " ");
Error Detection
Question 29
What is wrong with the following loop that intends to print 0 1 2 3 4?
for (int i = 0; i < 5; i++);
{
    System.out.print(i + " ");
}
Predict the Output
Question 30
Before looking at the choices, trace the nested loop.
int c = 0;
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 2; j++) {
        c++;
    }
}
System.out.println(c);
Section 4: while Loops
Predict the Output
Question 31
Before looking at the choices, trace the while loop.
int i = 1;
int prod = 1;
while (i <= 5) {
    prod *= i;
    i++;
}
System.out.println(prod);
Error Detection
Question 32
What is the MOST LIKELY problem with the following loop?
int i = 10;
while (i > 0) {
    System.out.println(i);
}
I/II/III Format
Question 33
Consider the loop shown. Which of the following claims are TRUE about it?
int i = 0;
while (i < 5) {
    System.out.print(i + " ");
    i++;
}
I. The loop prints 0 1 2 3 4.
II. When the loop ends, i equals 5.
III. The loop body executes exactly 5 times.
Predict the Output
Question 34
Before looking at the choices, trace the loop.
int n = 100;
int count = 0;
while (n > 1) {
    n /= 2;
    count++;
}
System.out.println(count);
Error Detection
Question 35
A student writes the following code intending to read numbers until a negative number is entered. What is wrong?
Scanner sc = new Scanner(System.in);
int num = 1;
while (num > 0);
{
    num = sc.nextInt();
    System.out.println(num);
}
Predict the Output
Question 36
Before looking at the choices, trace the Euclidean algorithm for the GCD.
int a = 48;
int b = 18;
while (b != 0) {
    int t = b;
    b = a % b;
    a = t;
}
System.out.println(a);
I/II/III Format
Question 37
Which of the following while-loop fragments print 1 2 3 (separated by spaces)?
I. int i = 1; while (i <= 3) { System.out.print(i + " "); i++; }
II. int i = 1; while (i < 4) { System.out.print(i + " "); i++; }
III. int i = 0; while (i < 3) { i++; System.out.print(i + " "); }
Predict the Output
Question 38
Before looking at the choices, trace the loop.
int x = 2;
int steps = 0;
while (x < 100) {
    x *= x;
    steps++;
}
System.out.println(steps);
Error Detection
Question 39
A student wants to find the first index where a substring of length 1 equals "a" in a String. Which version is WRITTEN CORRECTLY and is safe on all strings?
Predict the Output
Question 40
Before looking at the choices, trace the loop that extracts digits.
int n = 307;
int sum = 0;
while (n > 0) {
    sum += n % 10;
    n /= 10;
}
System.out.println(sum);
Section 5: Nested Loops and Iteration Algorithms
Predict the Output
Question 41
Before looking at the choices, trace the nested loop.
int t = 0;
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= i; j++) {
        t += j;
    }
}
System.out.println(t);
Error Detection
Question 42
A student writes the following palindrome check. Will it correctly report whether the String is a palindrome?
String s = "racecar";
boolean pal = true;
for (int i = 0; i < s.length(); i++) {
    if (!s.substring(i, i+1).equals(s.substring(s.length()-1-i, s.length()-i))) {
        pal = false;
    }
}
System.out.println(pal);
I/II/III Format
Question 43
Consider nested loops that print pairs of values. Which of the following fragments would print exactly 9 lines of output?
I. for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) System.out.println(i + " " + j);
II. for (int i = 0; i < 9; i++) System.out.println(i);
III. for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) System.out.println(i + " " + j); }
Predict the Output
Question 44
Before looking at the choices, trace the nested loop.
int count = 0;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i == j) {
            count++;
        }
    }
}
System.out.println(count);
Error Detection
Question 45
A student writes the following loop to count vowels in a String. What does it count INSTEAD?
String s = "programming";
int k = 0;
for (int i = 0; i < s.length(); i++) {
    String ch = s.substring(i, i+1);
    if (ch.equals("a") || ch.equals("e") || ch.equals("i") || ch.equals("o") || ch.equals("u")) {
        k = 1;
    }
}
System.out.println(k);
Predict the Output
Question 46
Before looking at the choices, trace the loop that reverses a String.
String src = "ABCDE";
String rev = "";
for (int i = src.length() - 1; i >= 0; i--) {
    rev += src.substring(i, i+1);
}
System.out.println(rev);
I/II/III Format
Question 47
Which of the following fragments correctly compute the sum of the digits of int num = 125? (Assume num is positive.)
I. int s = 0; while (num > 0) { s += num % 10; num /= 10; } // result in s
II. int s = 0; for (int i = 0; i < 3; i++) { s += num % 10; num /= 10; } // result in s
III. int s = 0; while (num >= 10) { s += num % 10; num /= 10; } s += num; // result in s
Predict the Output
Question 48
Before looking at the choices, trace the count.
String data = "abcabc";
int c = 0;
for (int i = 0; i <= data.length() - 3; i++) {
    if (data.substring(i, i + 3).equals("abc")) {
        c++;
    }
}
System.out.println(c);
Predict the Output
Question 49
Before looking at the choices, trace the binary-search-style loop.
int target = 42;
int lo = 1;
int hi = 100;
int guesses = 0;
while (lo <= hi) {
    int mid = (lo + hi) / 2;
    guesses++;
    if (mid == target) break;
    if (mid < target) lo = mid + 1;
    else hi = mid - 1;
}
System.out.println(guesses);
I/II/III Format
Question 50
Consider the following loop. Which of the following claims are TRUE?
int p = 1;
int q = 1;
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i == j) p++;
        if (i + j == 4) q++;
    }
}
I. After the loops, p equals 6.
II. After the loops, q equals 6.
III. The inner body executes exactly 25 times.

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]