Boolean expressions are fundamental to selection and iteration in Java. In the current AP Computer Science A framework, this practice set covers Topic 2.2: Boolean Expressions, Topic 2.5: Compound Boolean Expressions, and Topic 2.6: Comparing Boolean Expressions.
Practice evaluating relational and logical operators, tracing compound Boolean expressions, applying De Morgan’s Laws, recognizing equivalent expressions, and understanding short-circuit evaluation. Work through the 12 questions below and review the explanation after each answer.
Quick Review: Expand the sections below for a step-by-step refresher before starting the quiz.
1. The Short-Circuit Evaluation Rule
Java evaluates logical expressions from left to right and can skip the remaining part of an expression when its result is already determined. This is called short-circuit evaluation.
-
AND (
&&): If the left side isfalse, the entire expression must be false, so Java skips the right side. -
OR (
||): If the left side istrue, the entire expression must be true, so Java skips the right side.
Practice Tip: Short-circuit evaluation can prevent an expression on the right side from being evaluated. This is especially useful when the right side depends on a condition established by the left side.
2. De Morgan’s Laws
When a NOT operator (!) is applied to a compound Boolean
expression, the logical operator changes and each condition is negated.
- Negate the first condition.
- Negate the second condition.
-
Flip the logical operator
(
&&becomes||, and||becomes&&).
Example: Simplify
!(x > 5 && y == 10)
-
x > 5becomesx <= 5 -
&&becomes|| -
y == 10becomesy != 10
Result:
x <= 5 || y != 10
Boolean Expressions & Logic
boolean isValid = (word != null) && (word.length() > 5);
System.out.println(isValid);
word != null evaluates to false,
the entire && expression must be false.
Java therefore skips the right side of the expression.As a result,
word.length() > 5 is never evaluated,
and no NullPointerException occurs.1. Negate
x > 10 → x <= 102. Change
|| to &&3. Negate
y <= 5 → y > 5Therefore:
!(x > 10 || y <= 5)is equivalent to:x <= 10 && y > 5.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) → true2.
(true && true) → true3.
!(true) → false4.
true && false → falseTherefore,
result is 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 && !q2.
p is false and q is true:
!p && qThe
|| combines these two cases.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 this is an AND expression, Java short-circuits and does not call
check(a).In the
else if, a < 10 is true.
Because this is an OR expression, Java again short-circuits and does not
call check(a).Therefore, only
PathB is printed.if (att >= 95) {
return true;
}
}
return false;
return true
statement is reached.Therefore, the equivalent Boolean expression is:
(score >= 90) && (att >= 95).A and B are Boolean variables. Which of the following expressions is always logically equivalent to (A && B) || A?A.If
A is true:(true && B) || trueis true regardless of B.If
A is false:(false && B) || falseis false.Therefore, the entire expression always has the same value as
A.x is an initialized integer variable. Which of the following expressions will always evaluate to false regardless of the value of x?&&, so the expression always evaluates to
false.str:1. Negate
str.equals("quit") →
!str.equals("quit")2. Change
|| to &&3. Negate
str.length() < 3 →
str.length() >= 3Therefore, the equivalent expression is:
!str.equals("quit") && str.length() >= 3.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) → trueTherefore:
true && true && trueevaluates to true.result?5 + 3 > 7 →
8 > 7 →
true4 * 2 == 8 →
8 == 8 →
true!true →
falseThe expression becomes:
true && true || falseThe AND operation evaluates to
true, and then:true || false → true.result?String s2 = new String(“Java”);
boolean result = (s1 == s2) || s1.equals(s2);
s1 == s2 evaluates to false because the two
new String("Java") expressions create two distinct String
objects, so the references are different.s1.equals(s2) evaluates to true because the two
String objects contain the same sequence of characters.Therefore:
false || true → true.Boolean Expressions Practice Complete!
Why Boolean Logic Matters for AP CSA
Boolean expressions are used throughout Java selection and iteration. Understanding relational operators, logical operators, compound conditions, and equivalent Boolean expressions helps you trace code and determine how conditional statements and loops behave.
The concepts practiced here are especially useful as you move from Boolean
expressions into if statements and loops in Unit 2. Pay close
attention to how &&, ||, and !
affect the evaluation of compound expressions.
Continue Your AP CSA Practice
You have completed the Boolean Expressions & Logic practice set covering Topics 2.2, 2.5, and 2.6. Continue with the next available Unit 2 practice page on while loops.