Lesson 2.6: Comparing Boolean Expressions

Unit 2 · Lesson 2.6 · Code Mechanics

Lesson 2.6: Comparing Boolean Expressions

🕑 20–25 min·8 Practice Questions·4 Mastery Questions·Output Predictor + Bug Hunt

Key Vocabulary

Term Definition
equivalent expressions Two boolean expressions that evaluate to the same value for every possible combination of input values. (EK 2.6.A.1)
truth table A table showing the output of a boolean expression for all possible combinations of input values. Used to prove or disprove equivalence. (EK 2.6.A.1)
De Morgan's Law A pair of transformation rules: !(a && b)!a || !b and !(a || b)!a && !b. When NOT is distributed, AND becomes OR and OR becomes AND. (EK 2.6.A.2)
object reference A variable that stores a memory address pointing to an object. Two references can point to the same object or different objects with identical content. (EK 2.6.B)
.equals() A method that compares object content rather than memory addresses. The correct way to compare String values. Returns a boolean.
null A special value indicating a reference variable does not point to any object. Calling a method on a null reference throws a NullPointerException at runtime.

Equivalent Boolean Expressions (EK 2.6.A.1)

Two boolean expressions are equivalent if and only if they produce the same result for every possible combination of input values — not just the inputs you happen to test. Proving equivalence requires checking all cases, which is exactly what a truth table does. Finding a single case where two expressions differ is sufficient to prove they are NOT equivalent.

CED Connection (EK 2.6.A.1)

"Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent." The AP exam tests this with code questions asking which expression is equivalent to a given one, and with truth table fill-in questions.

Worked Example — Proving Equivalence with Truth Table

Are a > b and !(a <= b) equivalent?

a b a > b a <= b !(a <= b) Match?
3 5 false true false
5 5 false true false
7 5 true false true

All cases match → a > b and !(a <= b) are equivalent. This makes sense logically: "greater than" is exactly the negation of "less than or equal to."

De Morgan's Law (EK 2.6.A.2)

De Morgan's Law is one of the highest-frequency topics on the AP CSA exam. It provides two transformation rules for distributing NOT across AND and OR. The CED states both rules explicitly:

De Morgan's Law — Both Rules

!(a && b)  is equivalent to  !a || !b    // AND becomes OR
!(a || b)  is equivalent to  !a && !b    // OR becomes AND

Memory rule: push NOT inside the parentheses, flip every operand, and flip the operator (AND ↔ OR).

Worked Example — Proving De Morgan's with Truth Table

Verify !(a && b)!a || !b:

a b a && b !(a && b) !a || !b Match?
T T T F F
T F F T T
F T F T T
F F F T T

All four rows match → the equivalence is proven.

Worked Example — Applying De Morgan's in Code

// Original condition: not (raining AND cold)
if (!(isRaining && isCold)) { /* go outside */ }

// De Morgan's applied: !AND becomes OR with flipped operands
if (!isRaining || !isCold) { /* go outside */ }  // equivalent

// Second law: not (hungry OR tired)
if (!(isHungry || isTired)) { /* keep working */ }

// De Morgan's applied: !OR becomes AND with flipped operands
if (!isHungry && !isTired) { /* keep working */ }  // equivalent

De Morgan's lets you rewrite complex negations into forms that are easier to read and reason about. On AP FRQs, either form is accepted as correct when both are equivalent.

AP Trap: Forgetting to Flip the Operator

The single most common De Morgan's mistake: !(a && b) becomes !a && !b. WRONG — the AND must become OR. Correct: !a || !b. Similarly !(a || b) becomes !a || !b is WRONG — the OR must become AND. Correct: !a && !b. The operator always flips when you distribute NOT.

Comparing Object References (EK 2.6.B)

With reference types (objects), == and != compare memory addresses — whether two variables point to the same object in memory, not whether their content is identical. A reference variable can also hold the special value null, meaning it does not point to any object. Calling a method on a null reference throws a NullPointerException at runtime.

Worked Example — == vs. .equals() on Strings

String a = new String("hello");
String b = new String("hello");
String c = a;
String d = null;

System.out.println(a == b);        // false — different objects
System.out.println(a == c);        // true  — same object (c points to a)
System.out.println(a.equals(b));   // true  — same content
System.out.println(a.equals(c));   // true  — same content

System.out.println(d == null);     // true  — d points to nothing
System.out.println(d != null);     // false

// Safe null check before calling .equals():
if (a != null && a.equals(b)) {   // short-circuits if a is null
    System.out.println("Match");
}
Method / Operator What it compares
== (objects) Memory addresses. true only if both variables point to the exact same object.
⚠ Use only for null checks: obj == null
.equals() (objects) Object content. true if the objects have equal content, even if stored at different addresses.
⚠ Always null-check before calling: if (s != null && s.equals(t))
== (primitives) Values directly. Safe and correct for int, double, boolean, char.

AP Trap: Calling .equals() on null

null.equals("hello") throws a NullPointerException because null has no methods. Always place the non-null object on the left: "hello".equals(userInput) is safe even if userInput is null, because .equals() returns false for a null argument. Or null-check first: userInput != null && userInput.equals("hello").

Real-World Connection

Equivalent boolean expressions are important in software maintenance: two developers might write logically identical conditions in different forms, and a code reviewer must recognize they are the same. De Morgan's Law is used constantly to simplify complex guard conditions. Object reference comparison matters in every system that manages collections of objects: a database query returns new object instances even for the same data row, so == will return false even when the records are identical. Java's entire .equals() convention exists to solve this problem.

Summary

  • Equivalent expressions produce the same boolean result for every possible input. Prove with a truth table. (EK 2.6.A.1)
  • De Morgan's Law: !(a && b)!a || !b and !(a || b)!a && !b. Distribute NOT, flip operands, flip operator. (EK 2.6.A.2)
  • Common De Morgan's mistake: forgetting to flip AND to OR (or vice versa).
  • == on objects compares memory addresses, not content. (EK 2.6.B)
  • .equals() compares object content. Use for Strings and other objects.
  • null check: obj == null or obj != null. Never call .equals() on a reference that may be null without checking first.
  • Safe pattern: knownNonNull.equals(possiblyNull) or obj != null && obj.equals(other).

Equivalent Boolean Expressions

Two boolean expressions are equivalent if they evaluate to the same value for all possible inputs. Truth tables are a systematic way to prove equivalence.

Example: Proving Equivalence

// Are x > 5 and !(x <= 5) equivalent?
x = 3:  x>5 = false,  !(x<=5) = !(true)  = false  ✓
x = 5:  x>5 = false,  !(x<=5) = !(true)  = false  ✓
x = 7:  x>5 = true,   !(x<=5) = !(false) = true   ✓
// Same result for all inputs → EQUIVALENT

De Morgan’s Law

De Morgan’s Law provides a rule for distributing NOT across AND/OR:

De Morgan’s Law

!(a && b)  is equivalent to  !a || !b
!(a || b)  is equivalent to  !a && !b

Memory trick: when you push NOT inside, flip AND to OR (and vice versa).

De Morgan’s in Practice

// Original: not (raining AND cold)
!(isRaining && isCold)

// Equivalent via De Morgan's:
!isRaining || !isCold
// "not raining, or not cold"

AP Trap: Common De Morgan’s Mistake

Students often write !(a && b) as !a && !b. This is WRONG. The AND must become OR when you distribute the NOT. Always flip the operator.

Comparing Object References

With reference types (objects), == and != compare references (memory addresses), not content. A reference can be compared to null to check whether it points to any object.

Reference Comparison

String s = null;
System.out.println(s == null);   // true: no object

// Two variables can reference the same object
String a = "hello";
String b = a;         // b and a reference the SAME object
System.out.println(a == b);      // true: same reference

AP Trap: == on Strings

Two String variables with identical content may NOT be == if they reference different objects. Use .equals() to compare string content. You will practice this more in Lesson 2.10.

Tier 2 · AP Practice

Practice Questions

MCQ 1
Which expression is equivalent to !(x > 5 || y < 3) by De Morgan’s Law?
A !(x > 5) || !(y < 3)
B x <= 5 || y >= 3
C x <= 5 && y >= 3
D x > 5 && y < 3
De Morgan’s: !(A || B) = !A && !B. !(x>5) = x≤5. !(y<3) = y≥3. Result: x≤5 && y≥3. The OR flips to AND.
MCQ 2
Which expression is equivalent to !(a && b)?
A !a && !b
B a || b
C !a && b
D !a || !b
De Morgan’s: !(A && B) = !A || !B. The AND flips to OR and both operands are negated. A is the most common wrong answer — the operator must flip.
MCQ 3
Are x >= 5 and !(x < 5) equivalent?
Test with x=4, x=5, x=6 before choosing.
A Yes, they evaluate to the same value for all x
B No, they differ when x = 5
C No, they differ when x < 5
D No, one is a compile error
x=4: x≥5=false, !(x<5)=!(true)=false ✓. x=5: x≥5=true, !(x<5)=!(false)=true ✓. x=6: both true ✓. They are equivalent for all integers.
MCQ 4
What is printed?

String s = null;
System.out.println(s == null);
A false
B true
C NullPointerException
D Compile error
s is null (no object). Comparing a null reference with == null is safe and evaluates to true. No NullPointerException occurs here.
MCQ 5
Which of the following correctly applies De Morgan’s Law?

I. !(p && q)!p || !q
II. !(p || q)!p && !q
III. !(p && q)!p && !q
A I only
B III only
C I and II only
D I, II, and III
I and II are correct De Morgan’s applications. III is the classic mistake — it keeps AND instead of flipping to OR. Only I and II are correct.
MCQ 6
Two variables a and b both hold references to the same object. What does a == b evaluate to?
A true, because they reference the same object in memory
B false, because == always compares content
C Depends on the object type
D Compile error: cannot use == on object references
When two reference variables point to the same object, == returns true because they hold the same memory address. == on objects compares references, not content.
MCQ 7
Which expression is NOT equivalent to x < 3 || x > 7?
Test each with x=5 (inside range) to find the odd one out.
A !(x >= 3 && x <= 7)
B !(3 <= x && x <= 7)
C x > 7 || x < 3
D !(x < 3) && !(x > 7)
D is NOT equivalent. !(x<3) = x≥3 and !(x>7) = x≤7. Connected with &&: x is between 3 and 7 — the OPPOSITE of what we want. A and B use De Morgan’s correctly. C is just the original reordered.
MCQ 8
What is the result of !(true || false) && true?
A true
B false
C true || false
D Compile error
Inside parens first: true || false = true. Then !(true) = false. Then false && true = false.
PRACTICE WITH A GAME — CHOOSE ONE:
Concept Match — Boolean Equivalence
Click a term on the left, then its equivalent on the right.
Round 1 of 3Score: 0
Term / Code
Match
🎉 Done!
You got 0 of 3 correct.
Truth Table — De Morgan’s Law
Fill in the Result column to prove De Morgan's Law.
Table 1 of 2Score: 0
🎉 Done!
You got 0 of 2 correct.
Spot the Difference — Boolean Expressions
Which version behaves differently? Or are they the same?
Question 1 of 4Score: 0

Version A

Version B

A behaves differently
B behaves differently
Same behavior
🎉 Done!
You got 0 of 4 correct.
Tier 3 · AP Mastery

Mastery: Boolean Equivalence

A student claims that a && b is equivalent to !(! a || !b). Is this correct?
Apply De Morgan’s Law to the right side.
A No, because double negation changes the result
B No, because De Morgan’s Law cannot be applied twice
C No, the expressions differ when both a and b are false
D Yes, they are equivalent for all values of a and b
D. By De Morgan’s: !(!a || !b) = !!a && !!b = a && b. Double negation cancels. The student is correct — the expressions are equivalent.
Which truth table row proves that p || q and p && q are NOT equivalent?
Find a row where they give different results.
A p = true, q = false
B p = true, q = true
C p = false, q = false
D They are equivalent
When p=true, q=false: p||q = true but p&&q = false. Different results — not equivalent. Row B: both true for both. Row C: both false for both. Row A is the counterexample.
What is printed?

int x = 4;
boolean p = x >= 5;
boolean q = !(x < 5);
System.out.println(p == q);
Evaluate p and q separately for x=4.
A false
B Compile error
C true
D 4
x=4: p = x≥5 = false. q = !(x<5) = !(true) = false. p==q = false==false = true. These expressions are equivalent.
A reference variable obj is passed to a method. Inside the method, the code runs if (obj != null) before calling methods on it. Why is this good practice?
Predict the answer before reading options.
A It checks whether the object has any methods defined
B It prevents a NullPointerException if no object was passed in
C It compares the content of the object to null
D It is redundant because null references cannot be passed to methods
B. If obj is null, calling any method on it throws a NullPointerException. The null check guards against this. Null references can absolutely be passed to methods — that is exactly what the guard prevents.
Extension

Truth Tables as Proof

A truth table lists every possible combination of true/false inputs and shows the output of an expression. For two variables (p, q), there are exactly 4 rows. If two expressions produce the same column of outputs, they are equivalent. Build a truth table for !(p && q) and !p || !q and verify De Morgan’s Law holds in all 4 cases.

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]