AP CSA Practice Test: Boolean Logic
AP CSA Practice Test: Boolean Logic
Unit 2: Selection and Iteration | 10 Questions | AP Computer Science A
!(a && b)?!a && !b
!a || !b
a || b
!(a || b)
Explanation: De Morgan's Law: !(a && b) = !a || !b. When you distribute NOT over AND, the AND flips to OR, and each operand is negated.
Common Mistake: Choosing !a && !b. That would be !(a || b) by De Morgan's second law.
int x = 0;
if (x != 0 && 10 / x > 1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
Explanation: Short-circuit evaluation: x != 0 is false, so Java does NOT evaluate 10 / x. The && operator stops at the first false. The else branch executes, printing "No".
Common Mistake: Expecting a divide-by-zero error. Short-circuit && prevents the second condition from being evaluated.
int x = 5, which expression evaluates to true?I.
x > 3 && x < 10II.
x < 3 || x > 4III.
!(x == 5)
Explanation: I: 5 > 3 && 5 < 10 = true && true = true. II: 5 < 3 || 5 > 4 = false || true = true. III: !(5 == 5) = !true = false.
Common Mistake: Including III. !(x == 5) is false when x is 5.
!(x >= 1 && x <= 10)?x < 1 && x > 10
x < 1 || x > 10
x >= 1 || x <= 10
x != 1 && x != 10
Explanation: Apply De Morgan's: negate each part and flip && to ||. !(x >= 1) becomes x < 1. !(x <= 10) becomes x > 10. Result: x < 1 || x > 10.
Common Mistake: Using && instead of ||. De Morgan's always flips the logical operator.
boolean p = true; boolean q = false; System.out.println(!p || q); System.out.println(!(p || q));
Explanation: Line 1: !true || false = false || false = false. Line 2: !(true || false) = !true = false. Both are false.
Common Mistake: Confusing operator precedence. ! has higher precedence than || so !p evaluates before the ||.
(a || b) && !(a && b). This is equivalent to which logic gate?Explanation: This expression is true when exactly one of a or b is true, but not both. That is the definition of XOR: (a || b) && !(a && b).
Common Mistake: Confusing with OR. OR is true when both are true; XOR is false when both are true.
check(7)?public static boolean check(int n) {
return n > 5 && n < 10 || n > 20;
}
Explanation: && has higher precedence than ||. So: (7 > 5 && 7 < 10) || 7 > 20 = (true && true) || false = true || false = true.
Common Mistake: Not knowing that && binds tighter than ||. This matters for the grouping.
x make !(x > 3 || x < -3) evaluate to true?Explanation: Apply De Morgan's: !(x > 3) && !(x < -3) = x <= 3 && x >= -3. This is the range -3 to 3, inclusive.
Common Mistake: Forgetting to flip > to <= and < to >= when negating.
int a = 3, b = 5; boolean result = (a > b) && (++a > 0); System.out.println(a + " " + result);
Explanation: Short-circuit evaluation: a > b is 3 > 5 = false. Since the left side of && is false, Java does NOT evaluate ++a. So a remains 3 and result is false.
Common Mistake: Thinking ++a executes regardless. Short-circuit && skips the right side when the left is false.
a && (!b || c)?a && !b || a && c
(a && !b) || (a && c)
a || !b && c
!(!a || b && !c)
Explanation: A and B are equivalent (distribution of && over ||). D uses De Morgan's and is equivalent. C is a || (!b && c) due to precedence (different meaning). C is NOT equivalent.
Common Mistake: Not tracking operator precedence in option C. && binds tighter than ||.
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]