AP CSA Practice Test: Conditionals
AP CSA Practice Test: Conditionals
Unit 2: Selection and Iteration | 10 Questions | AP Computer Science A
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");
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.
int a = 5, b = 10;
if (a > 3)
if (b < 8)
System.out.print("X");
else
System.out.print("Y");
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.
int x = 7;
if (x > 5) {
System.out.print("A");
}
if (x > 3) {
System.out.print("B");
}
if (x > 1) {
System.out.print("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.
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";
}
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.
int n = 0;
if (n > 0)
System.out.print("positive ");
System.out.print("done");
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.
String s = null;
if (s != null && s.length() > 0) {
System.out.print("valid");
} else {
System.out.print("invalid");
}
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.
result?int x = 4;
int result;
if (x % 2 == 0)
if (x % 4 == 0)
result = 1;
else
result = 2;
else
result = 3;
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.
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");
}
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').
int x = 10;
if (x > 5)
x -= 3;
x *= 2;
System.out.println(x);
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.
x is between 1 and 100, exclusive?if (1 < x < 100)
if (x > 1 && x < 100)
if (x > 1 || x < 100)
if (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.
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]