Logical Operators Examples: 47 Problems with Answers

Worked examples of Logical Operators to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

medium
Evaluate A AND NOT B OR C for A=True, B=True, C=False, using standard precedence.
Show solution

Answer

False

First step

1.
NOT B = NOT True = False.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

medium
Use De Morgan to rewrite: NOT(x>0 OR y>0).

Example 3

medium
Identify the bug: `if (x = 0 OR 1 OR 2)` is meant to test whether x is 0, 1, or 2.

Example 4

hard
Simplify A OR (A AND B) using boolean algebra.

Example 5

hard
Simplify the condition: `NOT (age < 13 OR age > 65)`.

Example 6

challenge
A condition `(NOT a) OR (NOT b)` is being optimized. Show two equivalent simpler forms.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

easy
Evaluate: True AND False.

Example 2

easy
Evaluate: True OR False.

Example 3

easy
Evaluate: NOT True.

Example 4

easy
Evaluate: True XOR True (XOR is true when operands differ).

Example 5

easy
Evaluate: False OR False.

Example 6

easy
Evaluate: True AND True.

Example 7

easy
Evaluate: NOT (False).

Example 8

easy
In `x == 5 OR 10`, why is this a logic bug when checking if x is 5 or 10?

Example 9

medium
Evaluate NOT True AND False using precedence (NOT before AND).

Example 10

medium
Evaluate True OR False AND False using precedence (AND before OR).

Example 11

medium
Using De Morgan's law, rewrite NOT (A AND B) without the outer NOT.

Example 12

medium
Using De Morgan's law, rewrite NOT (A OR B) without the outer NOT.

Example 13

medium
A short-circuit AND `A AND B` skips evaluating B when A is false. If A is `False`, is B evaluated?

Example 14

medium
Evaluate (True AND False) OR (NOT False).

Example 15

medium
For a discount, a customer needs (age over 65) OR (student AND member). If age=70, student=False, member=False, does the customer qualify?

Example 16

challenge
Simplify NOT(NOT A OR NOT B) using De Morgan and double negation.

Example 17

challenge
XOR can be built from AND, OR, NOT. Verify that A XOR B=(A OR B) AND NOT(A AND B) for A=True,B=True.

Example 18

challenge
A condition `NOT (x > 0 AND x < 10)` should reject values inside (0,10). Rewrite it with De Morgan into an OR of comparisons.

Example 19

medium
Evaluate NOT (True OR False) AND True using precedence.

Example 20

medium
Access is granted if `(isAdmin OR isOwner) AND NOT isBanned`. For isAdmin=False, isOwner=True, isBanned=False, is access granted?

Example 21

easy
Evaluate: False AND True.

Example 22

easy
Evaluate: False OR True.

Example 23

easy
Evaluate: True XOR False (XOR is true when operands differ).

Example 24

easy
How many rows does the truth table for A AND B have?

Example 25

easy
Which operator binds TIGHTEST (highest precedence): AND, OR, or NOT?

Example 26

easy
Evaluate: True AND True AND False.

Example 27

medium
Evaluate NOT(True AND False) OR False using precedence.

Example 28

medium
Rewrite NOT(x=5) without NOT, using a comparison.

Example 29

medium
A discount is given if (member AND age >= 18) OR (employee). Are member=False, age=25, employee=True eligible?

Example 30

medium
Short-circuit OR: in `A OR B`, B is skipped if A is True. If A=True and B is `1/0`, does the expression crash?

Example 31

medium
Evaluate (A AND B) OR (A AND NOT B) for A=True, B=False.

Example 32

medium
The expression A AND NOT A is what kind of formula?

Example 33

medium
Access if `loggedIn AND (isAdmin OR isOwner)`. For loggedIn=True, isAdmin=False, isOwner=False, is access granted?

Example 34

medium
If short-circuit AND is used and `A AND B` evaluates only A when A is False, what design rule does this enable?

Example 35

hard
Simplify A AND (A OR B) using boolean algebra.

Example 36

hard
Show that A XOR B≡(A AND NOT B) OR (NOT A AND B) by testing (A,B)=(T,F).

Example 37

hard
Three variables A,B,C. Build a truth-table expression that is True only when EXACTLY one of them is true.

Example 38

hard
Why is `{NAND}` (NOT AND) considered functionally complete on its own?

Example 39

hard
Simplify: (A AND B) OR (A AND NOT B).

Example 40

challenge
A short-circuit AND in `expensiveCheck() AND cheapCheck()` runs the expensive one first. What is the smell, and the fix?

Example 41

challenge
Write the negation of 'every student passed' using logical operators (formally).

What to focus on

Core idea: Logical operators let you build complex conditions from simple boolean tests.

Common stuck point: AND requires ALL conditions true; OR only requires ONE — easy to confuse.

Ask yourself: Is one condition built by joining true/false tests with AND or OR, or by flipping one with NOT?

Concept recap

Operators that combine or modify boolean expressions: AND (true only when both operands are true), OR (true when at least one operand is true), and NOT (reverses a boolean value from true to false or vice versa).

Two light switches wired one after the other light the bulb only when both are on: that is AND. Wired side by side, either switch alone lights it: that is OR. NOT is a switch mounted upside down. The picture predicts every two-input case, but it says nothing about the order a program follows in a long mix of operators.

Read the full lesson on Logical Operators

Background knowledge

These ideas may be useful before you work through the harder examples.

boolean logicselection