Boolean logic forms the backbone of all control flow in Java. On the AP CSA exam, questions testing your knowledge of if statements, logical operators (&&, ||, !), and Truth Tables account for up to 17.5% of your multiple-choice score.
In this module, you will practice tracing complex compound expressions, simplifying logic using De Morgan’s Laws, and identifying hidden NullPointerExceptions. Work through the 12 questions below to master Unit 3.
Crash Course: Expand the sections below for a quick step-by-step refresher before starting the quiz.
1. The Short-Circuit Evaluation Rule
Java optimizes logical expressions by skipping unnecessary evaluations. This is called short-circuiting.
- AND (
&&): If the left side isfalse, the entire expression must be false. Java skips the right side entirely. - OR (
||): If the left side istrue, the entire expression must be true. Java skips the right side entirely.
Exam Trap: The College Board loves to put code that would throw a runtime error on the right side of a short-circuited expression. If it gets skipped, the program survives!
2. De Morgan’s Laws
When you distribute a NOT operator (!) across a compound expression, three things must happen:
- Negate the first condition.
- Negate the second condition.
- Flip the logical operator (
&&becomes||, and vice versa).
Example: Simplify !(x > 5 && y == 10)
x > 5becomesx <= 5&&becomes||y == 10becomesy != 10
Result: x <= 5 || y != 10
Truth Tables & Booleans
boolean isValid = (word != null) && (word.length() > 5);
System.out.println(isValid);
word != null evaluates to false, the entire && statement is guaranteed to be false. Java optimizes this by short-circuiting (skipping) the right side. Therefore, word.length() > 5 is never executed, and the NullPointerException is avoided.1. Negate
(x > 10) → (x <= 10)2. Flip the OR (
||) to an AND (&&)3. Negate
(y <= 5) → (y > 5).The opposite of “greater than” is “less than OR equal to.”
a, b, and c are initialized boolean variables. If a = true, b = false, and c = true, what is the value of result?1.
(true || false) → true.2.
(true && true) → true.3. Apply the NOT operator to the second half:
!(true) → false.4. Combine them:
true && false results in false.p and q are boolean variables. Which of the following expressions evaluates to true if and only if exactly one of the variables is true?1:
p is true AND q is false (p && !q)2:
p is false AND q is true (!p && q).If either scenario is true, the OR ensures the statement returns true.
System.out.print(“Check “);
return n % 2 == 0;
}
public static void main(String[] args) {
int a = 5;
if (a > 10 && check(a)) {
System.out.print(“PathA”);
} else if (a < 10 || check(a)) {
System.out.print(“PathB”);
}
}
if, a > 10 is false. Because it’s an AND statement, Java short-circuits and skips the check(a) method. In the else if, a < 10 is true. Because it is an OR statement, Java short-circuits again, skipping the second check(a) call. Since the method is never executed, “Check” is never printed. Only “PathB” prints.if (att >= 95) {
return true;
}
}
return false;
if statement requires both the outer condition AND the inner condition to be true in order to execute the innermost block. This is the exact definition of the logical AND (&&) operator.A and B are boolean variables. Which of the following expressions is always logically equivalent to (A && B) || A?If
A is true: (true && B) || true evaluates to true regardless of B.If
A is false: (false && B) || false evaluates to false.Since the final result always matches the value of
A, variable B is irrelevant.x is an initialized integer variable. Which of the following expressions will always evaluate to false regardless of the value of x?str:1. Negate the first condition:
str.equals("quit") becomes !str.equals("quit").2. Flip the OR (
||) to an AND (&&).3. Negate the second condition:
< (strictly less than) becomes >= (greater than or equal to).boolean q = true;
boolean r = false;
(p || q) && (q || r) && !(p && r) evaluate to?1.
(p || q) → (false || true) → true2.
(q || r) → (true || false) → true3.
!(p && r) → !(false && false) → trueCombine them all:
true && true && true yields true.result?!), AND (&&), OR (||).1. Arithmetic:
8 > 7 && 8 == 8 || !true2. Relational & Equality:
true && true || !true3. Logical NOT:
true && true || false4. Logical AND:
true || false5. Logical OR:
trueresult?String s2 = new String(“Java”);
boolean result = (s1 == s2) || s1.equals(s2);
s1 == s2 evaluates to false because the new keyword creates two distinct objects in memory, so their memory addresses do not match.•
s1.equals(s2) evaluates to true because the .equals() method compares the actual character contents.The expression becomes
false || true, which evaluates to true.Truth Tables Practice Complete!
Why Boolean Logic Matters for AP CSA
While unit 3 focuses heavily on if/else statements, the logic you just practiced is the exact same logic required for Unit 4: Iteration. Every single while loop and for loop relies on a boolean expression to determine if the loop should continue executing or terminate.
If you do not fully understand how && and || interact, you will inevitably write code that results in infinite loops or skips essential array elements. Keep practicing De Morgan’s Laws until flipping the operators becomes second nature.
Ready for the Next Step?
Now that you have mastered control flow boundaries and condition evaluation, it is time to start repeating execution blocks using Iteration.
Continue your AP CSA review by heading over to the core arrays module or diving into standard looping algorithms.