AP Computer Science a Quiz: Boolean Expressions
12 questions · exam conditions
0:00
Boolean ExpressionsQuestion 1 of 12

Consider the following Boolean expression: !(a && b) || (c && !d). Given that a = true, b = false, c = true, and d = false, what sequence of evaluations correctly shows how this expression is processed?

!(true && false) evaluates to !false which is true, then true || (true && true) evaluates to true || true which is true
!(true && false) evaluates to !true which is false, then false || (true && false) evaluates to false || false which is false
!(true && false) evaluates to !false which is true, then true || (true && false) evaluates to true || false which is true
!(true && false) evaluates to true, then true || (true && !false) evaluates to true || (true && true) which is false
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Boolean Expressions

Practice Boolean Expressions in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Boolean Expressions, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

Consider the following Boolean expression: !(a && b) || (c && !d). Given that a = true, b = false, c = true, and d = false, what sequence of evaluations correctly shows how this expression is processed?

  1. !(true && false) evaluates to !false which is true, then true || (true && true) evaluates to true || true which is true (correct answer)
  2. !(true && false) evaluates to !true which is false, then false || (true && false) evaluates to false || false which is false
  3. !(true && false) evaluates to !false which is true, then true || (true && false) evaluates to true || false which is true
  4. !(true && false) evaluates to true, then true || (true && !false) evaluates to true || (true && true) which is false

Explanation: First, a && b becomes true && false = false. Then !(false) = true. Next, !d becomes !false = true. Then c && !d becomes true && true = true. Finally, true || true = true. Choice A correctly shows this evaluation. Choice B incorrectly negates true instead of false. Choice C incorrectly evaluates !d as false. Choice D contains multiple errors in the logical operations.

Question 2

Consider the expression a && b || c && d. Due to operator precedence, this expression is evaluated as (a && b) || (c && d). If you wanted to force the evaluation to be a && (b || c) && d, which parentheses placement would be correct?

  1. a && (b || c && d) because the parentheses override precedence for the latter portion of the expression only
  2. (a && b || c) && d because the parentheses group the first three variables before applying the final conjunction
  3. a && (b || c) && d because the parentheses explicitly group only the disjunction between b and c (correct answer)
  4. (a && b) || (c && d) because this maintains the original precedence while adding explicit grouping for clarity

Explanation: To achieve a && (b || c) && d, we need parentheses around (b || c) to ensure the OR operation happens before the AND operations with a and d. Choice A would evaluate as a && ((b || c) && d) due to AND having higher precedence than OR. Choice B would evaluate as (a && (b || c)) && d, which is equivalent to the desired result but unnecessarily groups a with the parenthetical. Choice D shows the original precedence grouping, not the desired regrouping.

Question 3

In Java, short-circuit evaluation means that in the expression a && b, if a is false, then b is not evaluated. Consider the expression (x != 0) && (y / x > 5). Why is short-circuit evaluation particularly important in this case?

  1. It improves performance by avoiding unnecessary arithmetic operations when the first condition is sufficient to determine the result
  2. It prevents a runtime ArithmeticException that would occur from division by zero when x equals zero (correct answer)
  3. It ensures that the comparison operation is performed using integer arithmetic rather than floating-point arithmetic
  4. It guarantees that the expression will always evaluate to false when x is zero, maintaining logical consistency

Explanation: When x equals zero, the second part (y / x > 5) would cause a division by zero error, throwing an ArithmeticException. Short-circuit evaluation prevents this by not evaluating the second part when the first part (x != 0) is false. Choice A describes a benefit of short-circuit evaluation but not the critical safety aspect here. Choice C is incorrect about arithmetic types. Choice D is incorrect because the expression evaluates to false due to short-circuiting, not because of logical consistency.

Question 4

A programmer wants to check if a number n is outside the range [10, 50]. They write the condition n < 10 && n > 50. What is wrong with this logic, and what should the correct condition be?

  1. The condition uses the wrong variable comparison order; the correct condition is 10 > n && 50 < n to maintain consistent directional logic
  2. The condition has the wrong comparison operators; the correct condition is n <= 10 && n >= 50 to include the boundary values in the outside range
  3. The condition is backwards; the correct condition is n > 10 && n < 50 to check if the number is inside the range instead of outside
  4. The condition uses AND when it should use OR; the correct condition is n < 10 || n > 50 because a number cannot be both less than 10 and greater than 50 simultaneously (correct answer)

Explanation: When working with range conditions in programming, you need to carefully consider the logical operators AND (&&) and OR (||) based on what you're actually testing for. The programmer wants to check if a number is outside the range [10, 50]. For a number to be outside this range, it must be either less than 10 OR greater than 50. The key insight is that these are two separate, mutually exclusive conditions - a number cannot simultaneously be both less than 10 AND greater than 50. The correct condition should be n < 10 || n > 50 because OR allows either condition to make the entire expression true. The original condition n < 10 && n > 50 uses AND, which requires both conditions to be true simultaneously. Since no number can be both less than 10 and greater than 50 at the same time, this condition will always evaluate to false, regardless of the value of n. Looking at the wrong answers: A) incorrectly focuses on operator direction when the real issue is the logical operator choice. B) suggests changing the comparison operators to include boundaries, but this doesn't fix the fundamental AND/OR problem and would still always be false. C) completely misunderstands the goal by suggesting we check if the number is inside the range instead of outside. Answer D correctly identifies that OR should replace AND because we need either condition to be true, not both. Study tip: Remember that "outside a range" typically requires OR logic (either too small OR too large), while "inside a range" uses AND logic (both greater than minimum AND less than maximum).

Question 5

A library allows borrowing when isAvailable && !isReserved; what will happen if isReserved becomes true?

  1. Borrowing is allowed because isAvailable is checked
  2. Borrowing is denied even if the book is available (correct answer)
  3. Borrowing is allowed only for members
  4. Borrowing depends only on isAvailable

Explanation: This question tests AP Computer Science A skills in boolean expressions and their use in controlling program flow and iteration. Boolean expressions evaluate to true or false and are crucial in decision-making and loops. They use operators like && (and), || (or), and ! (not) to form conditions that guide program logic. Choice B is correct because when isReserved becomes true, the expression isAvailable && !isReserved will evaluate to false (since !true is false), denying borrowing even if the book is available. Choice A is incorrect because it ignores the impact of isReserved being true - the && operator requires both conditions to be true, and !isReserved becomes false when isReserved is true. To help students: Trace through the expression step by step - if isReserved is true, then !isReserved is false, and true && false equals false. Emphasize that && requires ALL conditions to be true for the overall result to be true.

Question 6

An access system runs while (!hasKeyCard && !isAdmin) to keep showing "Denied"; how does this affect loop iteration?

  1. It repeats until both become true
  2. It repeats while both are false (correct answer)
  3. It repeats while either is true
  4. It repeats only when hasKeyCard is true

Explanation: This question tests AP Computer Science A skills in boolean expressions and their use in controlling program flow and iteration. Boolean expressions evaluate to true or false and are crucial in decision-making and loops. They use operators like && (and), || (or), and ! (not) to form conditions that guide program logic. Choice B is correct because the expression !hasKeyCard && !isAdmin means the loop continues while the user does NOT have a key card AND is NOT an admin - essentially while both original conditions are false. Choice C is incorrect because it misinterprets the logic; the && operator with negations means both must be false, not that either being true continues the loop. To help students: Use De Morgan's Laws to understand negated compound expressions - !(A || B) equals !A && !B. Practice converting between different forms of boolean expressions and trace through examples with truth tables.

Question 7

A traffic-light controller uses if (isEmergency || timeOfDay.equals("NIGHT")); which condition needs to be true to switch to flashing mode?

  1. Only isEmergency must be true
  2. Either isEmergency is true or timeOfDay is NIGHT (correct answer)
  3. Both isEmergency is true and timeOfDay is NIGHT
  4. Neither isEmergency nor timeOfDay is NIGHT

Explanation: This question tests AP Computer Science A skills in boolean expressions and their use in controlling program flow and iteration. Boolean expressions evaluate to true or false and are crucial in decision-making and loops. They use operators like && (and), || (or), and ! (not) to form conditions that guide program logic. Choice B is correct because the || (OR) operator means the condition is true if either isEmergency is true OR timeOfDay equals "NIGHT" - only one condition needs to be met to switch to flashing mode. Choice C is incorrect because it describes an AND relationship, which would require both conditions to be true simultaneously, not what the || operator provides. To help students: Create scenarios and test them against the boolean expression - if it's an emergency during the day, does it flash? Yes. If it's night but no emergency? Also yes. Emphasize that || means "at least one" while && means "all must be true."

Question 8

An access system grants entry when hasKeyCard || isAdmin; what will happen if this condition evaluates to false?

  1. Access is granted because one check is assumed
  2. Access is denied because neither condition is true (correct answer)
  3. Access is granted only if isAdmin is false
  4. Access is granted only if hasKeyCard is false

Explanation: This question tests AP Computer Science A skills in boolean expressions and their use in controlling program flow and iteration. Boolean expressions evaluate to true or false and are crucial in decision-making and loops. They use operators like && (and), || (or), and ! (not) to form conditions that guide program logic. Choice B is correct because when the expression hasKeyCard || isAdmin evaluates to false, it means NEITHER condition is true - the person doesn't have a key card AND is not an admin, so access is denied. Choice A is incorrect because it misunderstands what happens when an OR expression is false - for || to be false, both operands must be false. To help students: Remember that || (OR) is only false when both conditions are false. Create a truth table showing that false || false = false, while any other combination with || results in true.

Question 9

In a game, if (hasPowerUp && !isEnemyNear) allows a speed boost; which condition must be true to execute the boost block?

  1. hasPowerUp is true and isEnemyNear is false (correct answer)
  2. hasPowerUp is true and isEnemyNear is true
  3. hasPowerUp is false and isEnemyNear is false
  4. hasPowerUp is true, regardless of isEnemyNear

Explanation: This question tests AP Computer Science A skills in boolean expressions and their use in controlling program flow and iteration. Boolean expressions evaluate to true or false and are crucial in decision-making and loops. They use operators like && (and), || (or), and ! (not) to form conditions that guide program logic. Choice A is correct because the expression hasPowerUp && !isEnemyNear requires the player to have a power-up AND there must NOT be an enemy nearby - the ! operator negates isEnemyNear, so it must be false. Choice B is incorrect because it misinterprets the negation operator - if isEnemyNear is true, then !isEnemyNear is false, making the entire expression false. To help students: Practice reading negations carefully - !true equals false and !false equals true. Use real-world analogies: "You can only use the speed boost if you have it AND the coast is clear."

Question 10

Which Boolean expression is equivalent to !(a || b) && !(c && d)?

  1. (!a && !b) && (!c || !d) because De Morgan's law applies to both parts of the conjunction independently (correct answer)
  2. (!a || !b) && (!c && !d) because negation distributes over the logical operators in each parenthetical group
  3. (!a && !b) || (!c || !d) because the outer conjunction becomes disjunction when the entire expression is negated
  4. (!a || !b) || (!c && !d) because De Morgan's law converts AND to OR and OR to AND throughout the expression

Explanation: Applying De Morgan's law: !(a || b) becomes (!a && !b), and !(c && d) becomes (!c || !d). The conjunction between these parts remains unchanged, so the result is (!a && !b) && (!c || !d). Choice B incorrectly applies De Morgan's law to the second part. Choice C incorrectly changes the middle conjunction to disjunction. Choice D incorrectly applies De Morgan's law to the first part and changes the conjunction to disjunction.

Question 11

A method uses the condition (temperature > 80 && humidity > 60) || (temperature > 90). Which statement best describes when this condition evaluates to true?

  1. When temperature exceeds 80 degrees, regardless of humidity levels, or when humidity exceeds 60 percent, regardless of temperature
  2. When temperature exceeds 90 degrees under any humidity condition, or when temperature exceeds 80 degrees and humidity exceeds 60 percent (correct answer)
  3. When both temperature exceeds 80 degrees and humidity exceeds 60 percent, but only if temperature does not exceed 90 degrees
  4. When temperature exceeds 80 degrees and humidity exceeds 60 percent, but never when temperature exceeds 90 degrees alone

Explanation: The condition (temperature > 80 && humidity > 60) || (temperature > 90) is true in two cases: (1) when temperature > 80 AND humidity > 60, or (2) when temperature > 90 (regardless of humidity). Choice A incorrectly suggests temperature > 80 is sufficient alone and that humidity > 60 is sufficient alone. Choice C incorrectly suggests the condition is false when temperature > 90. Choice D incorrectly suggests temperature > 90 alone is insufficient.

Question 12

Consider the expression a || b && c || d. Given Java's operator precedence rules, how is this expression grouped, and what would be an equivalent fully parenthesized version?

  1. ((a || b) && c) || d because OR operations are evaluated left to right, then AND operations are applied
  2. a || ((b && c) || d) because AND has higher precedence than OR, and OR operations associate right to left
  3. (a || b) && (c || d) because the expression is split into two OR operations connected by the AND operation
  4. a || (b && c) || d because AND has higher precedence than OR, so the AND operation is grouped first (correct answer)

Explanation: When you encounter complex boolean expressions in Java, operator precedence determines the order of evaluation, just like in mathematical expressions. The key principle here is that AND (&&) has higher precedence than OR (||), meaning AND operations are grouped and evaluated before OR operations. In the expression a || b && c || d, the AND operator takes precedence, so b && c is grouped first. This gives us the equivalent expression a || (b && c) || d. The OR operations then associate left to right, but since they're at the same precedence level, the grouping shown captures the essential precedence relationship. Looking at the wrong answers: Choice A incorrectly assumes OR has higher precedence and groups (a || b) first, which violates Java's precedence rules. Choice B makes two errors—it suggests OR associates right to left (it actually associates left to right) and creates an incorrect grouping that doesn't reflect how Java would actually parse this expression. Choice C completely misinterprets the precedence rules by suggesting the expression splits into two separate OR operations connected by AND, which would require explicit parentheses to achieve. Choice D correctly identifies that AND has higher precedence than OR, resulting in b && c being grouped first within the larger OR expression chain. Remember this precedence hierarchy: AND (&&) binds more tightly than OR (||), similar to how multiplication binds more tightly than addition in arithmetic. When in doubt about operator precedence, always add explicit parentheses to make your intentions clear in real code.