AP CSA Practice Test: Conditionals

AP CSA Practice Test: Conditionals

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

Question 1
What is the output?
int x = 15;
if (x > 20)
    System.out.print("A");
else if (x > 10)
    System.out.print("B");
else if (x > 5)
    System.out.print("C");
else
    System.out.print("D");
A"A"
B"B"
C"BC"
D"BCD"

Explanation: If-else if chains execute only the FIRST true branch. x > 20 is false. x > 10 is true, so "B" prints and the chain ends. "C" is NOT printed even though x > 5 is also true.

Common Mistake: Thinking all true conditions execute. In an else-if chain, only the first match runs.

Question 2
What is the output?
int a = 5, b = 10;
if (a > 3)
    if (b < 8)
        System.out.print("X");
else
    System.out.print("Y");
A"X"
B"Y"
CNothing is printed
D"XY"

Explanation: The else matches the NEAREST if, which is if (b < 8) (the dangling else problem). Since a > 3 is true and b < 8 is false, the else prints "Y". Indentation is misleading here.

Common Mistake: Thinking the else matches the outer if. Java ignores indentation; else pairs with nearest if.

Question 3
What is the output?
int x = 7;
if (x > 5) {
    System.out.print("A");
}
if (x > 3) {
    System.out.print("B");
}
if (x > 1) {
    System.out.print("C");
}
A"A"
B"AB"
C"ABC"
D"C"

Explanation: These are three separate if statements (not if-else-if). Each is evaluated independently. All three conditions are true, so "A", "B", and "C" all print.

Common Mistake: Confusing separate if statements with an if-else-if chain. Separate ifs ALL execute when true.

Question 4
What does this method return for grade(85)?
public static String grade(int score) {
    if (score >= 90) {
        return "A";
    }
    if (score >= 80) {
        return "B";
    }
    if (score >= 70) {
        return "C";
    }
    return "F";
}
A"A"
B"B"
C"C"
D"F"

Explanation: 85 >= 90 is false. 85 >= 80 is true, so the method returns "B" immediately. The remaining conditions are never checked because return exits the method.

Common Mistake: Thinking all conditions are checked. return exits the method immediately.

Question 5
What is the output?
int n = 0;
if (n > 0)
    System.out.print("positive ");
    System.out.print("done");
A"positive done"
B"done"
CNothing is printed
DCompile error

Explanation: Without braces, the if controls only the FIRST statement. System.out.print("done") is NOT inside the if block despite the indentation. It always executes.

Common Mistake: Thinking indentation creates a block. Without {}, only the first line after if is conditional.

Question 6
What is the output?
String s = null;
if (s != null && s.length() > 0) {
    System.out.print("valid");
} else {
    System.out.print("invalid");
}
A"valid"
B"invalid"
CNullPointerException
DCompile error

Explanation: Short-circuit evaluation: s != null is false, so s.length() is NEVER called. This avoids the NullPointerException. The else branch prints "invalid".

Common Mistake: Expecting NullPointerException. Short-circuit && prevents calling methods on null.

Question 7
What is the value of result?
int x = 4;
int result;
if (x % 2 == 0)
    if (x % 4 == 0)
        result = 1;
    else
        result = 2;
else
    result = 3;
A1
B2
C3
DCompile error

Explanation: The else on result = 2 pairs with if (x % 4 == 0). The outer else pairs with if (x % 2 == 0). Since 4 % 2 == 0 is true and 4 % 4 == 0 is true, result = 1.

Common Mistake: Mismatching else with the wrong if. Read the nesting carefully.

Question 8
Consider the following code. For which input does it print "medium"?
public static void classify(int n) {
    if (n < 0 || n > 100)
        System.out.print("invalid");
    else if (n >= 50)
        System.out.print("high");
    else if (n >= 25)
        System.out.print("medium");
    else
        System.out.print("low");
}
A25
B50
C75
D10

Explanation: For n=25: not < 0 or > 100 (skip). Not >= 50 (skip). 25 >= 25 is true: prints "medium". n=50 prints "high", n=75 prints "high", n=10 prints "low".

Common Mistake: Choosing 50, which falls into the >= 50 branch (prints 'high').

Question 9
What is the output?
int x = 10;
if (x > 5)
    x -= 3;
    x *= 2;
System.out.println(x);
A14
B7
C20
D4

Explanation: Without braces, only x -= 3 is inside the if. x *= 2 always executes. x > 5 is true: x = 10 - 3 = 7. Then x = 7 * 2 = 14.

Common Mistake: Thinking x *= 2 is inside the if block. Without braces, only the first statement is conditional.

Question 10
Which code correctly checks if x is between 1 and 100, exclusive?
Aif (1 < x < 100)
Bif (x > 1 && x < 100)
Cif (x > 1 || x < 100)
Dif (x >= 1 && x <= 100)

Explanation: A: compile error, Java does not support chained comparisons. B: correct, both conditions must be true. C: this is true for ALL values (OR is too permissive). D: this is inclusive, not exclusive.

Common Mistake: Using || instead of &&. OR means either condition, but we need BOTH to be true.

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]