AP Computer Science A • Unit 2.6

Comparing Boolean Expressions

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

Q1A
Q2B
Q3C
Q4A
Q5A
Q6B
Q7A
Q8C
Q9A
Q10C
Q11B
Q12B
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 p = true;
boolean q = false;
boolean left = !(p && q);
boolean right = !p || !q;

System.out.print(left == right);
Atrue
Bfalse
Ca
DThe code does not compile.
Correct answer: A. De Morgan's Laws make the two expressions equivalent, so left and right hold the same value and true is printed.
Explanation

!(p && q) and !p || !q form a De Morgan pair: the negation moves inward and the operator changes from && to ||. Equivalent expressions agree for every combination of values, so both variables hold true here.

  • B is not correct: false would require the two expressions to disagree, but De Morgan's Laws make them equivalent, so they agree for every combination of p and q.
  • C is not correct: the letter a names an operand in the statement of De Morgan's Laws; it is not a value that a comparison of two boolean variables can produce.
  • D is not correct: nothing in the segment breaks a rule: each of the first four lines declares a boolean variable, and the last line prints the result of a comparison.
Q2

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

String badge = new String("E7");
String saved = badge;
badge = null;

System.out.println(badge == null);
System.out.println(saved == null);
Atrue, followed by true
Btrue, followed by false
Cfalse, followed by true
Dfalse, followed by false
Correct answer: B. saved copies the reference before badge is assigned null, so the first comparison is true and the second is false.
Explanation

Comparing a reference with null uses == or != and does not call a method. Assigning null to badge changes only badge; saved still holds the reference it received earlier.

  • A is not correct: saved received the reference to the String object before the assignment, so it still refers to that object and the second comparison is false.
  • C is not correct: badge holds null when the first comparison runs, so that comparison is true; this choice reverses the order of the two results.
  • D is not correct: the first comparison is true, because badge was assigned null on the line just above the two output statements.
Q3

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

String r1 = new String("NORTH");
String r2 = r1;
String r3 = new String("NORTH");

System.out.println(r1 == r2);
System.out.println(r2 == r3);
System.out.println(r1.equals(r3));
Atrue, true, true
Bfalse, false, true
Ctrue, false, true
Dfalse, true, false
Correct answer: C. r2 is an alias for r1, r3 refers to a separate object, and equals compares the characters.
Explanation

== checks reference identity, while equals applies the class's rule for content equality. r2 receives an existing reference and becomes an alias, and the second new expression creates a different object.

  • A is not correct: r2 == r3 is false, because r3 comes from its own new expression and therefore refers to a different object.
  • B is not correct: r1 == r2 is true, since r2 receives the reference held by r1 and both names then refer to one object.
  • D is not correct: r1 == r2 is true for the alias, and r1.equals(r3) is true because both strings contain the same characters.
Q4

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

String p = new String("OPEN");
String copy = p;
p = null;

System.out.println(p != copy);
Atrue
Bfalse
Cnull
DA NullPointerException occurs.
Correct answer: A. p holds null while copy still refers to the object, so the two references differ and true is printed.
Explanation

The operators == and != compare object references. copy received the reference before p was assigned null, so the two variables no longer hold the same reference.

  • B is not correct: false would mean both variables hold the same reference, but only p was changed by the assignment of null.
  • C is not correct: null is the value stored in p; the comparison itself produces a boolean result rather than that stored value.
  • D is not correct: comparing a reference with == or != does not call a method, so holding null causes no exception here.
Q5

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

int n = 14;

boolean a = !(n >= 5 && n <= 12);
boolean b = n < 5 || n > 12;

System.out.println(a + " " + b);
Atrue true
Btrue false
Cfalse true
Dfalse false
Correct answer: A. The two expressions form a De Morgan pair, and 14 lies outside the range, so both variables are true.
Explanation

Moving the negation inward changes && to || and negates each comparison, so n >= 5 becomes n < 5 and n <= 12 becomes n > 12. Both forms then describe the same values.

  • B is not correct: the second variable is also true, because 14 is greater than 12, which makes one operand of the || expression true.
  • C is not correct: the first variable is true as well: n <= 12 is false, so the && expression is false and its negation is true.
  • D is not correct: both expressions describe the same condition, and 14 lies outside the range, so neither of them evaluates to false.
Q6

Which expression is equivalent to !(p && q)?

A!p && !q
B!p || !q
Cp || q
Dp && q
Correct answer: B. De Morgan's Laws negate each operand and change the && to ||, which produces !p || !q.
Explanation

!(a && b) is equivalent to !a || !b, and a truth table confirms it: the final two columns match in every row. The operator itself changes, not only the placement of the !.

  • A is not correct: that is the pattern for negating an || expression; the original operator here is &&, so the negated operands must be joined by ||.
  • C is not correct: both ! operators have been dropped, so this expression is false when p and q are both false, where the original is true.
  • D is not correct: this is the expression inside the parentheses without its negation, so it gives the opposite result in every row of a truth table.
Q7

Which expression is equivalent to !(p || q)?

A!p && !q
B!p || !q
Cp && q
Dp || q
Correct answer: A. De Morgan's Laws negate each operand and change the || to &&, which produces !p and !q joined by &&.
Explanation

!(a || b) is equivalent to !a && !b, as the truth table shows: the final two columns match in every row. Moving the negation inward changes the operator as well as the operands.

  • B is not correct: the operator has not changed here; negating an || expression must join the negated operands with && rather than with ||.
  • C is not correct: both ! operators are missing, so this expression is true only when p and q are both true, where the original is false.
  • D is not correct: this is the expression inside the parentheses with the negation dropped, so it gives the opposite result in every row of a truth table.
Q8

Which expression is equivalent to this expression? !(x <= 18)

Ax >= 18
Bx < 18
Cx > 18
Dx == 18
Correct answer: C. The negation of x <= y is x > y, so the given expression is equivalent to x > 18.
Explanation

A negated relational expression can often be rewritten by flipping the comparison. Each relational operator has a negated form, and the negated form of x <= y is x > y.

  • A is not correct: x >= 18 is true when x equals 18, but the original expression is false there, so the two are not equivalent.
  • B is not correct: x < 18 is the negation of x >= y rather than of x <= y; it is true exactly where the original expression is false.
  • D is not correct: x == 18 tests a single value, while the original expression is true for every value greater than 18.
Q9

Which expression is always true?

Ap || q || !(p && q)
Bp && !p
Cp && q && !(p || q)
Dp && q
Correct answer: A. Moving the negation inward leaves both p and !p among the operands, and a || !a is true.
Explanation

De Morgan turns !(p && q) into !p || !q, so the whole condition becomes an || expression with four operands. Whatever value p has, one of p and !p is true.

  • B is not correct: p && !p is always false, because p and !p cannot both be true at the same time.
  • C is not correct: after the negation moves inward this expression contains both p and !p joined by &&, which makes it always false.
  • D is not correct: p && q is false whenever either operand is false, so it is not true for every combination of values.
Q10

Which expression is always false?

Ap || !p
Bp || q || !(p && q)
Cp && q && !(p || q)
D!(p && q) || p
Correct answer: C. After the negation moves inward the expression contains both p and !p joined by &&, and a && !a is false.
Explanation

!(p || q) becomes !p && !q, so the whole condition is an && expression with four operands, among them p and !p. They cannot both be true.

  • A is not correct: p || !p is always true, because one of those two operands is true whatever value p has.
  • B is not correct: this is the || pattern that is always true, since moving the negation inward leaves both p and !p among its operands.
  • D is not correct: when p is true the second operand makes the whole || expression true, so it is not always false.
Q11

If Boolean expressions p and q are equivalent, what must be true?

AThey use exactly the same symbols.
BThey always produce the same result for the same inputs.
CThey have the same number of characters.
DThey contain no relational operators.
Correct answer: B. Equivalent expressions evaluate to the same value for every possible combination of their variables, however differently they are written.
Explanation

Equivalence is about results rather than appearance. buildingOpen && !atCapacity and !(!buildingOpen || atCapacity) look nothing alike, yet a truth table shows that they agree in every row.

  • A is not correct: two expressions written with different symbols can still agree on every input, and agreement on every input is all that equivalence requires.
  • C is not correct: the number of characters says nothing about the values produced; equivalence is decided row by row, by comparing results for every possible input.
  • D is not correct: relational operators are allowed. !(score < 60 || score > 100) and score >= 60 && score <= 100 both use them and are equivalent.
Q12

Which expression is equivalent to this expression? !(!p || (p && q))

A!p && q
Bp && !q
C!p || q
Dp || !q
Correct answer: B. Moving the negation inward pairs p with !p || !q, and since p && !p is false, p && !q remains.
Explanation

In De Morgan's Laws each operand may be an entire expression. Negating the outer || changes it to &&; the negation of !p is p, and the negation of p && q is !p || !q.

  • A is not correct: !p && q requires p to be false, but moving the negation inward leaves p itself as one of the operands.
  • C is not correct: the outer negation turns the || into &&, so the equivalent expression cannot keep || as its main operator.
  • D is not correct: this keeps || where De Morgan's Laws require &&, and it is true when p and q are both false, where the original is false.