Unit 2 Cycle 1 Day 7: Compound Boolean with AND/OR

Unit 2 Foundation (Cycle 1) Day 7 of 28 Foundation

Compound Boolean with AND/OR

Section 2.5 — Compound Boolean

Key Concept

Compound boolean expressions use logical AND (&&) and OR (||) to combine multiple conditions. With &&, both conditions must be true for the overall expression to be true. With ||, at least one must be true. Operator precedence matters: ! has highest precedence, then &&, then ||. The expression a || b && c evaluates as a || (b && c), not (a || b) && c. Use parentheses to make intent clear.

Consider the following code segment.

int age = 16; boolean hasLicense = false; if (age >= 16 && hasLicense) { System.out.print("drive"); } else if (age >= 16 || hasLicense) { System.out.print("eligible"); } else { System.out.print("wait"); }

What is printed as a result of executing the code segment?

Answer: (B) eligible

age >= 16 && hasLicense: true && false = false. age >= 16 || hasLicense: true || false = true. Prints "eligible".

Why Not the Others?

(A) AND requires BOTH conditions to be true. hasLicense is false, so the AND fails.

(C) The OR condition is true because at least one operand (age >= 16) is true.

(D) This is an else-if chain, so only one branch executes.

Common Mistake

&& (AND) requires both sides to be true. || (OR) requires at least one side to be true. A common error is confusing which operator requires both vs. either.

AP Exam Tip

Evaluate each side of && and || independently first. Then apply: AND = both true; OR = at least one true.

Review this topic: Section 2.5 — Compound Boolean • Unit 2 Study Guide

More Practice

Related FRQs

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.