Question 981
What is printed?
int a = 0, b = 5, c = 3;
boolean p = (a != 0) && (b / a > 1);
boolean q = (a == 0) || (b / a > 1);
boolean r = (c > b) || (a != 0) && (b > c);
System.out.println(p + " " + q + " " + r);Main Solution
Let's trace through the code with a = 0, b = 5, c = 3.
Evaluating p:
boolean p = (a != 0) && (b / a > 1);
The left operand of && is (a != 0) → (0 != 0) → false.
Because Java uses short-circuit evaluation with &&, if the left side is false, the right side is never evaluated. So b / a (which would be division by zero) is never executed.
Therefore, p = false.
Evaluating q:
boolean q = (a == 0) || (b / a > 1);
The left operand of || is (a == 0) → (0 == 0) → true.
Because Java uses short-circuit evaluation with ||, if the left side is true, the right side is never evaluated. So again, b / a is never executed.
Therefore, q = true.
Evaluating r:
boolean r = (c > b) || (a != 0) && (b > c);
Operator precedence: && has higher precedence than ||, so this is parsed as:
(c > b) || ((a != 0) && (b > c))
(c > b)→(3 > 5)→ false- Since left of
||is false, evaluate right side:(a != 0) && (b > c) (a != 0)→(0 != 0)→ false- Short-circuit: right side of
&¬ evaluated. - So
(a != 0) && (b > c)→ false false || false→ false
Therefore, r = false.
Output:
System.out.println(p + " " + q + " " + r); prints: false true false
The correct answer is C.
Teacher Explanation
This question tests two key Java concepts: short-circuit evaluation and operator precedence. Students must recognize that && short-circuits on a false left operand and || short-circuits on a true left operand, preventing the division-by-zero runtime error. Additionally, students must know that && has higher precedence than ||, so the expression for r groups as (c > b) || ((a != 0) && (b > c)).
Education Coach Advice
On the AP CSA exam, always check for short-circuit evaluation when you see && or || — especially when one operand could cause a runtime error (like division by zero). Also remember: && binds more tightly than ||, just like multiplication before addition. When in doubt, add parentheses mentally to clarify grouping before tracing the logic.
Traps
Option D (Runtime error) is the most tempting wrong answer. Students who do not know about short-circuit evaluation will assume b / a (i.e., 5 / 0) is always evaluated, causing an ArithmeticException. However, Java's && and || operators short-circuit, so the division is never reached in either p or q.
Option A (false true true) is a trap for students who misapply operator precedence in r, treating it as ((c > b) || (a != 0)) && (b > c) or making an error in the boolean logic.
Answer images

