Compound Boolean Expressions
Help Questions
AP Computer Science A › Compound Boolean Expressions
Consider the following compound Boolean expression: (x > 5 && y < 10) || (x <= 5 && y >= 10). For which of the following pairs of values would this expression evaluate to false?
x = 5, y = 10
x = 7, y = 12
x = 3, y = 8
x = 6, y = 9
Explanation
The expression is false only when both parts of the OR are false. For x = 3, y = 8: (3 > 5 && 8 < 10) = (false && true) = false, and (3 <= 5 && 8 >= 10) = (true && false) = false. Since both parts are false, the entire expression is false. All other options make at least one part of the OR true.
Given the expression !(a && b) && (c || !d), when will this expression evaluate to true? Assume all variables are boolean.
When at least one of a or b is false, and either c is true or d is false
When either a or b is true, and either c is false or d is true
When both a and b are false, and both c is true and d is false
When both a and b are true, and either c is true or d is false
Explanation
For the expression to be true, both parts must be true: !(a && b) is true when (a && b) is false, which happens when at least one of a or b is false. (c || !d) is true when either c is true OR d is false. Option A incorrectly states both a and b are true. Option C is too restrictive. Option D has the wrong conditions for the second part.
A method contains the condition: (score >= 90 && attempts <= 3) || (score >= 80 && bonus). If score = 85, attempts = 2, and bonus = false, what changes would make this expression evaluate to true?
Change score to 90 or change bonus to true
Change attempts to 4 or change bonus to true
Change score to 90 and change attempts to 1
Change attempts to 1 or change score to 75
Explanation
When you encounter compound boolean expressions with AND (&&) and OR (||) operators, break them down systematically by evaluating each part separately. This expression has two main parts connected by OR: (score >= 90 && attempts <= 3) OR (score >= 80 && bonus).
With the given values (score = 85, attempts = 2, bonus = false), let's evaluate each part. The first part (score >= 90 && attempts <= 3) becomes (85 >= 90 && 2 <= 3), which is (false && true) = false. The second part (score >= 80 && bonus) becomes (85 >= 80 && false), which is (true && false) = false. Since both parts are false, the entire expression is false OR false = false.
To make this true, you need at least one part to become true. Looking at option D, changing score to 90 makes the first part (90 >= 90 && 2 <= 3) = true, or changing bonus to true makes the second part (85 >= 80 && true) = true. Either change works.
Option A is wrong because changing attempts to 4 makes the first part false (4 > 3), though changing bonus to true would work. Option B fails because changing score to 75 makes both parts false (75 < 90 and doesn't help the second part), and changing attempts to 1 doesn't fix the score issue in the first part. Option C requires both changes together, but the question asks what changes would work, and either change in D works independently.
Remember: with OR expressions, you only need one side to be true, so look for changes that make at least one complete side evaluate to true.
A game awards bonus points when: (level > 5 && enemies == 0) || (level <= 5 && timeLeft > 60). A player is at level 3 with 45 seconds left and no enemies remaining. To earn bonus points, what is the minimum change needed?
Increase level to at least 6 and ensure enemies remain 0
Increase level to 6 or increase timeLeft to 61
Either increase timeLeft to 61 or advance to level 6
Increase timeLeft to at least 61 seconds
Explanation
When you encounter boolean logic problems with multiple conditions, break down the expression systematically using the logical operators AND (&&) and OR (||). This expression has two parts connected by OR: (level > 5 && enemies == 0) || (level <= 5 && timeLeft > 60).
Let's evaluate the current situation: level 3, timeLeft 45, enemies 0. For the first condition (level > 5 && enemies == 0): level 3 is not greater than 5, so this entire AND expression is false regardless of enemies being 0. For the second condition (level <= 5 && timeLeft > 60): level 3 is less than or equal to 5 (true), but timeLeft 45 is not greater than 60 (false), making this AND expression false. Since both parts of the OR are false, no bonus points are awarded.
To earn bonus points, you need to make at least one part true. The minimum change is increasing timeLeft to at least 61, which makes the second condition (level <= 5 && timeLeft > 60) evaluate to true.
Answer A is incorrect because it suggests either change works independently, but increasing level to 6 alone wouldn't help since enemies would need to be 0 AND level > 5. Answer B incorrectly focuses only on the first condition. Answer D makes the same error as A, suggesting level 6 alone would work.
Strategy tip: In OR expressions, you only need ONE part to be true. Always check which condition requires the fewest changes to become true, especially when dealing with multiple AND conditions within an OR statement.
Consider the compound condition: (x % 2 == 0 && x > 0) || (x % 3 == 0 && x < 0). For which value of x would this expression be false?
x = 8
x = -6
x = 5
x = 0
Explanation
When you encounter compound boolean expressions with logical operators, break them down systematically by evaluating each part separately, then combining the results using the operator precedence rules.
This expression uses both AND (&&) and OR (||) operators: (x % 2 == 0 && x > 0) || (x % 3 == 0 && x < 0). The entire expression is true if either the left side OR the right side (or both) evaluates to true. For it to be false, both sides must be false.
Let's test each option:
For choice A (x = -6): The left side is false because -6 is even but not positive. The right side is true because -6 is divisible by 3 and negative. Since one side is true, the entire expression is true.
For choice B (x = 8): The left side is true because 8 is even and positive. Since one side is true, the entire expression is true.
For choice D (x = 0): The left side is false because 0 is even but not positive. The right side is false because while 0 is divisible by 3, it's not negative. However, this creates ambiguity since 0 is neither positive nor negative.
For choice C (x = 5): The left side is false because 5 is odd (not even). The right side is false because 5 is not divisible by 3. Since both sides are false, the entire expression is false.
Therefore, C is correct.
Strategy tip: With compound conditions, systematically evaluate each subexpression and use a truth table approach. Remember that OR expressions are false only when all parts are false, while AND expressions are true only when all parts are true.
A program validates user input with: (age >= 18 && age <= 65) && (income > 30000 || hasGuarantor). If this expression evaluates to false, which of the following must be true?
The age is outside the range 18-65, and the income is <= 30000, and there is no guarantor
Either the age is outside the range 18-65, or the income is <= 30000, or there is no guarantor
The age is outside the range 18-65, and either the income is <= 30000 or there is no guarantor
Either the age is outside the range 18-65, or the income is <= 30000 and there is no guarantor
Explanation
For the AND expression to be false, at least one part must be false. Either (age >= 18 && age <= 65) is false (age outside range), OR (income > 30000 || hasGuarantor) is false (which means both income <= 30000 AND hasGuarantor is false). Option B incorrectly uses AND instead of OR for the main condition. Option C treats it as if all conditions were ORed together. Option D requires all conditions to fail simultaneously.
Given three boolean variables p, q, and r, the expression (p && q) || (p && r) || (q && r) represents which logical condition?
Exactly two of the three variables must be true
Either p must be true, or both q and r must be true
At least two of the three variables must be true
All three variables must be true, or exactly one must be true
Explanation
This expression is true when any two variables are true (which makes at least one of the AND pairs true), and it's also true when all three are true (which makes all AND pairs true). So it requires at least two of the three to be true. Option B is incorrect because it's also true when all three are true. Option C incorrectly describes the condition. Option D misrepresents the logical structure entirely.
A program needs to check if a student is eligible for a scholarship. The conditions are: GPA must be at least 3.5 AND (either community service hours >= 100 OR leadership position is true). Which compound Boolean expression correctly represents these conditions?
(gpa >= 3.5) && (hours >= 100) || (leadership == true)
(gpa >= 3.5) && ((hours >= 100) || (leadership == true))
(gpa >= 3.5) || (hours >= 100) && (leadership == true)
(gpa >= 3.5) && (hours >= 100) && (leadership == true)
Explanation
The conditions require GPA >= 3.5 AND either hours >= 100 OR leadership. This requires parentheses to ensure the OR is evaluated first, then ANDed with the GPA condition. Option A is incorrect due to operator precedence (AND before OR). Option C uses OR instead of AND for GPA. Option D requires all three conditions instead of GPA AND (hours OR leadership).
A security system uses the condition: !((status.equals("armed") && motion) || (status.equals("test") && !authorized)). Under which circumstances will this expression evaluate to true?
When the system is armed without motion, in test mode with authorization, or status is neither
When the system is not armed and no motion detected, or not in test mode and authorized
When the system is armed with motion detected, or in test mode without authorization
When the system status is armed or test, regardless of other states
Explanation
The expression is the negation of the inner compound condition. It's true when the inner condition is false. The inner condition is false when both parts of the OR are false: (status is not "armed" OR motion is false) AND (status is not "test" OR authorized is true). This happens when: system is armed without motion, or in test mode with authorization, or when status is something other than "armed" or "test". Option A describes when the inner condition is true. Options C and D misinterpret the logical structure.
Consider the expression: !(a || b) && (c && !d). This expression is logically equivalent to which of the following?
!(a && b) && (!c || d)
(!a && !b) && (c && !d)
(!a || !b) && (c || !d)
(!a && !b) || (c && !d)
Explanation
When you encounter Boolean logic expressions on the AP exam, you need to apply logical equivalence rules systematically, particularly De Morgan's Law and distribution properties.
Let's work through the original expression !(a || b) && (c && !d) step by step. First, apply De Morgan's Law to the first part: !(a || b) becomes (!a && !b). The second part (c && !d) remains unchanged since there's no negation affecting the entire expression.
So our expression becomes: (!a && !b) && (c && !d). This matches answer choice D exactly.
Let's examine why the other options are incorrect:
Choice A (!a && !b) || (c && !d) changes the main logical operator from AND (&&) to OR (||), which completely alters the truth conditions of the expression.
Choice B (!a || !b) && (c || !d) makes two errors: it incorrectly applies De Morgan's Law to get (!a || !b) instead of (!a && !b), and it changes (c && !d) to (c || !d).
Choice C !(a && b) && (!c || d) incorrectly leaves the first part as !(a && b) without applying De Morgan's Law, and completely reverses the second part by negating both c and d while changing AND to OR.
Study tip: Master De Morgan's Laws: !(A || B) = (!A && !B) and !(A && B) = (!A || !B). Practice by writing out truth tables when you're unsure—this will help you verify logical equivalences quickly on the exam.