AP Computer Science A • Unit 2.5

Compound Boolean Expressions

Detailed answer explanations for the Unit 2.5 multiple-choice practice questions.

Q1A
Q2B
Q3A
Q4A
Q5A
Q6B
Q7C
Q8A
Q9B
Q10D
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

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

boolean valid = false;
boolean wait = true;
boolean pass = true;
System.out.println(pass || valid && wait);
Atrue
Bfalse
Ctrue false
DThe code does not compile.
Correct answer: A. Without parentheses Java groups valid && wait first, and pass is true, so the whole expression is true.
Explanation

Operator precedence is the main rule Java uses to group a compound condition. Without parentheses, && is evaluated before ||, so valid && wait is grouped first. An expression using || is true when at least one side is true.

  • B is not correct: the left side of the || is pass, which holds true, and one true side is already enough for the whole expression.
  • C is not correct: the compound expression produces a single Boolean result, and one System.out.println statement prints that one value rather than two.
  • D is not correct: the code compiles, because each name is declared as a boolean and && and || combine Boolean values.
Q2

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

String s = "";
if (s.length() > 0 && s.substring(0,1).equals("A")) {
    System.out.println("A code");
} else {
    System.out.println("Empty");
}
AA code
BEmpty
CNothing is printed.
DA run-time error occurs before anything is printed.
Correct answer: B. The left side s.length() > 0 is false, so the whole && is false and the else branch runs.
Explanation

Java uses short-circuit evaluation to reduce the number of operations: once the left side of && determines the result, the right side is skipped. A guard condition placed on the left can also prevent Java from reaching an expression that would cause a run-time error.

  • A is not correct: the compound condition is false, so the first branch never runs; the empty string has no first character to compare with "A".
  • C is not correct: the statement always runs exactly one of its two branches, and here the else branch prints a line.
  • D is not correct: the guard on the left of && is false, so Java skips s.substring(0,1) and never reaches an operation that could fail.
Q3

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

boolean sent = true;
boolean paid = false;
boolean free = true;
boolean held = false;
if (sent && !held && (paid || free)) {
    System.out.println("Process");
} else {
    System.out.println("Hold");
}
AProcess
BHold
CBoth Process and Hold
DNothing is printed.
Correct answer: A. Every part joined by && holds: sent is true, !held is true, and free makes the parenthesized group true.
Explanation

A compound condition can combine requirements involving different variables. Each part joined by && must hold, and the parentheses keep the two alternatives together, so at least one of paid and free is enough.

  • B is not correct: the else branch runs only when the compound condition is false, and here every part joined by && is true.
  • C is not correct: a statement with an else branch runs exactly one of its two paths, so only one of the two lines is printed.
  • D is not correct: one of the two branches always runs, and the compound condition selects the first one.
Q4

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

boolean locked = false;
boolean staff = true;
System.out.println(!locked && staff);
Atrue
Bfalse
Cready
DThe code does not compile.
Correct answer: A. ! reverses locked from false to true, and staff is true, so both sides hold.
Explanation

! is the logical NOT operator, and it reverses a Boolean value. It applies only to the value beside it, so !locked is evaluated first and then combined with staff. An expression using && is true only when both sides are true.

  • B is not correct: a result of false would need at least one side of the && to be false, but both sides evaluate to true.
  • C is not correct: the compound expression produces a Boolean value, so the printed result can only be true or false.
  • D is not correct: the code compiles, because ! reverses a boolean value and && combines two Boolean values.
Q5

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

String s = "Hi";
if (s.length() < 3 || s.substring(0,3).equals("VIP")) {
    System.out.println("Short");
} else {
    System.out.println("Long");
}
AShort
BLong
CNothing is printed.
DA run-time error occurs before anything is printed.
Correct answer: A. The left side s.length() < 3 is true, so the || is true and the first branch runs.
Explanation

An expression using || is true when at least one side is true. A true left side already determines that result, so Java skips the right side and never evaluates s.substring(0,3).equals("VIP").

  • B is not correct: the else branch runs only when both sides of the || are false, and the left side is true here.
  • C is not correct: the statement always runs exactly one of its two branches, so one of the two lines is printed.
  • D is not correct: the left side of the || is true, so Java skips s.substring(0,3) and never reaches an operation that could fail.
Q6

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

boolean off = true;
boolean alert = false;
System.out.println(!(off || alert));
Atrue
Bfalse
Ca
DThe code does not compile.
Correct answer: B. off is true, so off || alert is true, and ! reverses that grouped result to false.
Explanation

To reverse a compound expression, that expression is grouped first. The parentheses make off || alert a single value, and ! then reverses it. Without the parentheses, ! would apply only to the value beside it.

  • A is not correct: a result of true would need off || alert to be false, but off is true and one true side is enough.
  • C is not correct: the expression produces a Boolean value, so the printed result is true or false and never a letter.
  • D is not correct: the code compiles, because the parentheses group two boolean values joined by || and ! reverses that result.
Q7

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

boolean local = true;
boolean fast = false;
boolean safe = false;

System.out.println(local || fast && safe);
System.out.println((local || fast) && safe);
Atrue followed by true
Bfalse followed by true
Ctrue followed by false
Dfalse followed by false
Correct answer: C. Precedence groups fast && safe first, giving true; the parentheses group local || fast first, giving false.
Explanation

Without parentheses, && is evaluated before ||. Parentheses override that order and make the intended grouping explicit, which can change the printed result. When a condition mixes && and ||, parentheses show the grouping that is intended.

  • A is not correct: the second line groups local || fast first and then applies && safe, and safe is false, so that result is false.
  • B is not correct: the first line is an || whose left side local is true, so its printed result is true.
  • D is not correct: only one side of an || needs to hold, and local is true, so the first line prints true.
Q8

Which expression is true when humidity is inside the inclusive range from 30 through 60?

Ahumidity >= 30 && humidity <= 60
Bhumidity >= 30 || humidity <= 60
Chumidity < 30 && humidity > 60
D!(humidity >= 30) && !(humidity <= 60)
Correct answer: A. A value inside an inclusive range satisfies both limits, and && is true only when both sides are true.
Explanation

Two common patterns test whether a value is inside or outside a range. A value is inside an inclusive range only when both limits hold, which is the shape shown by x >= 1 && x <= 10 with different limits.

  • B is not correct: || is true when at least one side is true, so a value far below 30 still satisfies the second comparison.
  • C is not correct: no value can be below 30 and above 60 at the same time, so this compound expression is never true.
  • D is not correct: reversing both comparisons and joining them with && again asks for a value below 30 and above 60 at once.
Q9

Which expression is true when temp is outside the inclusive operating range from 5 through 40?

Atemp >= 5 && temp <= 40
Btemp < 5 || temp > 40
Ctemp < 5 && temp > 40
D!(temp < 5 || temp > 40)
Correct answer: B. A value is outside the inclusive range when either limit is crossed, so the two comparisons are joined with ||.
Explanation

The inside and outside conditions describe opposite cases. Being inside requires both limits to hold, so being outside holds when either limit is crossed, and || is true when at least one side is true.

  • A is not correct: this is the inside condition, which is true only for values that satisfy both limits, so it describes the opposite case.
  • C is not correct: no value can be below 5 and above 40 at the same time, so joining these comparisons with && never gives true.
  • D is not correct: ! applied to the outside condition reverses it, so this expression is true exactly for values inside the range.
Q10

In the absence of parentheses, in which order are Java's logical operators evaluated?

A&&, then ||, then !
B!, then ||, then &&
CStrictly from left to right
D!, then &&, then ||
Correct answer: D. Operator precedence applies ! first, then &&, then ||, unless parentheses override that order.
Explanation

Operator precedence is the main rule Java uses to group a compound condition during evaluation. Parentheses override that order and make the intended grouping explicit, which is why they help whenever compound logic could be misread.

  • A is not correct: this reverses the actual order, since ! is applied before && and && is applied before ||.
  • B is not correct: ! is indeed applied first, but && is grouped before || rather than after it.
  • C is not correct: precedence rather than position decides the grouping, so && is grouped before || wherever the two appear.