AP CSA Practice Test: Boolean Logic

AP CSA Practice Test: Boolean Logic

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

Question 1
Which expression is equivalent to !(a && b)?
A!a && !b
B!a || !b
Ca || b
D!(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.

Question 2
What is the output?
int x = 0;
if (x != 0 && 10 / x > 1) {
    System.out.println("Yes");
} else {
    System.out.println("No");
}
A"Yes"
B"No"
CArithmeticException (divide by zero)
DCompile error

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.

Question 3
Given int x = 5, which expression evaluates to true?

I. x > 3 && x < 10
II. x < 3 || x > 4
III. !(x == 5)
AI only
BI and II only
CI and III only
DI, II, and III

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.

Question 4
Which is equivalent to !(x >= 1 && x <= 10)?
Ax < 1 && x > 10
Bx < 1 || x > 10
Cx >= 1 || x <= 10
Dx != 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.

Question 5
What is the output?
boolean p = true;
boolean q = false;
System.out.println(!p || q);
System.out.println(!(p || q));
Afalse then false
Bfalse then true
Ctrue then false
Dtrue then true

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 ||.

Question 6
Consider the expression: (a || b) && !(a && b). This is equivalent to which logic gate?
AAND
BOR
CXOR (exclusive or)
DNAND

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.

Question 7
What does this method return for check(7)?
public static boolean check(int n) {
    return n > 5 && n < 10 || n > 20;
}
Atrue
Bfalse
CCompile error
DDepends on operator precedence

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.

Question 8
Which values of x make !(x > 3 || x < -3) evaluate to true?
Ax > 3 or x < -3
B-3 <= x <= 3
Cx == 0 only
DNo value of x

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.

Question 9
What is the output?
int a = 3, b = 5;
boolean result = (a > b) && (++a > 0);
System.out.println(a + " " + result);
A4 false
B3 false
C4 true
D3 true

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.

Question 10
Which expression is NOT equivalent to a && (!b || c)?
Aa && !b || a && c
B(a && !b) || (a && c)
Ca || !b && c
D!(!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 ||.

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]