PrepGo

AP Computer Science A Practice Quiz: Comparing Boolean Expressions

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 14 questions to check your progress.

Question 1 of 14

Under what condition are two Boolean expressions considered equivalent?

All Questions (14)

Under what condition are two Boolean expressions considered equivalent?

A) They use the same variables and operators.

B) They evaluate to the same value in all cases.

C) They have the same number of logical operators.

D) They both evaluate to true for at least one set of inputs.

Correct Answer: B

The definition of equivalent Boolean expressions is that they must produce the same result (true or false) for all possible combinations of input values, as stated in the provided content.

According to De Morgan's law, which of the following Boolean expressions is equivalent to `!(p && q)`?

A) !p && !q

B) !p || !q

C) p || q

D) p && !q

Correct Answer: B

De Morgan's law states that the negation of a conjunction (&&) is the disjunction (||) of the negations. Therefore, `!(p && q)` is equivalent to `!p || !q`.

According to De Morgan's law, which of the following Boolean expressions is equivalent to `!(x || y)`?

A) !x || !y

B) x && y

C) !x && !y

D) !x || y

Correct Answer: C

De Morgan's law states that the negation of a disjunction (||) is the conjunction (&&) of the negations. Therefore, `!(x || y)` is equivalent to `!x && !y`.

When the `==` operator is used to compare two object references, what is it actually comparing?

A) The contents or data stored within the objects.

B) Whether the objects are instances of the same class.

C) Whether the references point to the exact same object in memory.

D) The size of the objects in memory.

Correct Answer: C

The provided content states that object references can be compared using `==`. This operator checks for identity, not equality. It evaluates to true only if the two reference variables hold the same memory address, meaning they point to the identical object instance.

Which of the following expressions is guaranteed to be equivalent to `!(score < 60 && isLate)` for any boolean `isLate` and integer `score`?

A) score >= 60 && !isLate

B) score > 60 || !isLate

C) score >= 60 || !isLate

D) score < 60 || isLate

Correct Answer: C

This requires applying De Morgan's law, where `!(a && b)` is equivalent to `!a || !b`. In this case, `a` is `score < 60` and `b` is `isLate`. The negation of `score < 60` is `score >= 60`. The negation of `isLate` is `!isLate`. Applying the law results in the equivalent expression `score >= 60 || !isLate`.

Consider the following code segment, assuming `MyObject` is a valid class. What is the value of the boolean variable `areDifferent`? ```java MyObject obj1 = new MyObject(); MyObject obj2 = new MyObject(); boolean areDifferent = (obj1 != obj2); ```

A) true

B) false

C) The value is indeterminate.

D) The code will not compile.

Correct Answer: A

The `new` keyword creates a new, distinct object in memory. Therefore, `obj1` and `obj2` are references to two separate objects. The `!=` operator compares the object references (their memory addresses), which are different. Thus, `obj1 != obj2` evaluates to `true`.

What tool can be used to formally prove that two Boolean expressions are equivalent by checking all possible cases?

A) A compiler

B) De Morgan's law

C) A truth table

D) An object reference diagram

Correct Answer: C

The provided content explicitly states that 'Truth tables can be used to prove Boolean expressions are equivalent.' A truth table exhaustively checks every combination of input values to confirm the expressions produce the same output in all cases.

Which of the following Boolean expressions is equivalent to `! (a || !b)`?

A) !a || b

B) a && !b

C) !a && b

D) !a && !b

Correct Answer: C

To find an equivalent expression, we apply De Morgan's law, where `!(x || y)` is equivalent to `!x && !y`. In this expression, `x` is `a` and `y` is `!b`. The law transforms `!(a || !b)` into `!a && !(!b)`. The double negation `!(!b)` simplifies to `b`, resulting in the final equivalent expression `!a && b`.

What is the output of the following code segment? ```java String s1 = new String("A"); String s2 = new String("B"); s2 = s1; System.out.println(s1 == s2); ```

A) true

B) false

C) A

D) B

Correct Answer: A

Initially, `s1` and `s2` refer to two different String objects. The line `s2 = s1;` copies the reference (memory address) from `s1` into `s2`. After this assignment, both variables point to the exact same object. The `==` operator compares these references, finds them to be identical, and evaluates to `true`.

Three of the following Boolean expressions are equivalent. Which expression is NOT equivalent to the others?

A) !(x && y)

B) !x || !y

C) !x && !y

D) An expression that is true unless both x and y are true.

Correct Answer: C

According to De Morgan's law, `!(x && y)` is equivalent to `!x || !y`. Option D is a textual description of the same logic. Option C, `!x && !y`, is equivalent to `!(x || y)` by the other form of De Morgan's law, and is therefore the one not equivalent to the others.

Consider the code below. What is the final value of the `result` variable? ```java String strA = new String("Hi"); String strB = new String("Hi"); String strC = strA; boolean result = (strA != strB) && (strA == strC); ```

A) true

B) false

C) The code will cause a runtime error.

D) The code will not compile.

Correct Answer: A

The expression `(strA != strB)` evaluates to `true` because `strA` and `strB` are created as two separate objects with the `new` keyword, so their references are different. The expression `(strA == strC)` evaluates to `true` because `strC` is assigned the reference of `strA`, making them point to the same object. The overall expression becomes `true && true`, which evaluates to `true`.

Given that boolean variable `a` is `true` and boolean variable `b` is `false`, which of the following expressions evaluates to `false`?

A) a || b

B) !(a && b)

C) !b

D) !a || b

Correct Answer: D

Evaluating each option with `a = true` and `b = false`: A: `true || false` is `true`. B: `!(true && false)` is `!false`, which is `true`. C: `!false` is `true`. D: `!true || false` is `false || false`, which is `false`.

A program needs a condition that is true only when it is NOT the case that an item is on sale (`isOnSale`) OR it is out of stock (`isOutOfStock`). Which Boolean expression correctly represents this logic?

A) !isOnSale || !isOutOfStock

B) isOnSale && isOutOfStock

C) !isOnSale && !isOutOfStock

D) !isOnSale || isOutOfStock

Correct Answer: C

The initial condition is `isOnSale || isOutOfStock`. The requirement is to find the negation of this entire expression: `!(isOnSale || isOutOfStock)`. Applying De Morgan's law (`!(a || b)` is equivalent to `!a && !b`), we transform the expression into `!isOnSale && !isOutOfStock`.

Consider the following code segment where `obj1` and `obj2` are references to objects. Which expression is equivalent to `!(obj1 == obj2)`?

A) obj1 != obj2

B) !obj1 == !obj2

C) obj1 && obj2

D) The expression cannot be simplified.

Correct Answer: A

The `!=` operator is defined as the logical opposite of the `==` operator for both primitive types and object references. Therefore, the expression `!(obj1 == obj2)` is always equivalent to `obj1 != obj2`. They both check if the two references point to different objects.