Comparing Boolean Expressions
Help Questions
AP Computer Science A › Comparing Boolean Expressions
An algorithm selector compares isDataSorted and isResourceAvailable:
if (!isDataSorted && isResourceAvailable)
plan = "SORT_THEN_FAST"
else
plan = "DIRECT"
for each item
if (plan == "DIRECT")
ops = ops + 1
Which Boolean expression evaluates to true given isDataSorted is true and isResourceAvailable is true?
!isDataSorted && isResourceAvailable
isDataSorted && isResourceAvailable
!isDataSorted || isResourceAvailable
!(isDataSorted && isResourceAvailable)
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, we need to identify which expression evaluates to true when both isDataSorted and isResourceAvailable are true. Choice B (isDataSorted && isResourceAvailable) is correct because it evaluates to true && true = true, as the AND operator requires both operands to be true. Choice A is incorrect because it would evaluate to !true && true = false && true = false, with the NOT operator making the first part false. To help students: Practice evaluating expressions systematically, applying operators in the correct order and understanding that AND requires all parts to be true. Watch for: Students forgetting to apply NOT operators or misunderstanding the requirement that AND needs all conditions true.
A simulation uses isSimulationRunning and isErrorDetected:
while (isSimulationRunning)
if (isErrorDetected)
isSimulationRunning = false
ticks = ticks + 1
How does the program flow change when isSimulationRunning becomes false inside the loop?
The loop continues until ticks reaches 0
The loop restarts immediately without checking
The loop stops before ticks increments
The loop condition fails next check and stops
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, when isSimulationRunning becomes false inside the loop, it affects the next iteration's condition check. Choice A is correct because after isSimulationRunning is set to false, the loop completes its current iteration (including incrementing ticks), then checks the condition again at the start of the next iteration, finds it false, and stops. Choice B is incorrect because it suggests the loop stops immediately without completing the current iteration, misunderstanding loop execution flow. To help students: Emphasize that changes to loop variables inside the loop body don't affect the current iteration but impact the next condition check. Watch for: Confusion about when loop conditions are checked and the order of operations within a loop iteration.
A form validator uses isEmailValid and isPasswordSecure:
if (!isEmailValid || !isPasswordSecure)
showError = true
else
showError = false
for each attempt
if (showError)
attempts = attempts + 1
What will be the output of the following code if isEmailValid is true and isPasswordSecure is true?
showError becomes false
showError becomes true, then false
showError becomes true
showError remains unspecified by default
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, the condition (!isEmailValid || !isPasswordSecure) checks if either field is invalid to determine whether to show an error. Choice B (showError becomes false) is correct because when both isEmailValid and isPasswordSecure are true, the condition becomes !true || !true = false || false = false, so the else branch executes setting showError to false. Choice A is incorrect because it assumes the if condition is true, not recognizing that both NOT operations create false values. To help students: Practice evaluating expressions with multiple NOT operators and trace through both branches of if-else statements. Watch for: Students forgetting to apply NOT operators or misunderstanding how OR combines false values.
A game AI checks isEnemyVisible and isHealthLow each turn:
if (isEnemyVisible || isHealthLow)
alert = true
else
alert = false
for turn from 1 to 5
if (isEnemyVisible && isHealthLow)
mode = "PANIC"
Which Boolean expression evaluates to true when isEnemyVisible is false and isHealthLow is true?
isEnemyVisible && isHealthLow
!isEnemyVisible && !isHealthLow
!(isEnemyVisible || isHealthLow)
isEnemyVisible || isHealthLow
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, we need to find which expression evaluates to true when isEnemyVisible is false and isHealthLow is true. Choice C (isEnemyVisible || isHealthLow) is correct because it evaluates to false || true = true, as the OR operator returns true when at least one operand is true. Choice A is incorrect because it would evaluate to false && true = false, requiring both conditions to be true for the AND operation. To help students: Emphasize the difference between AND (both must be true) and OR (at least one must be true) operations. Watch for: Students confusing when AND versus OR operations yield true results.
In access control, hasValidCredentials and isAccountActive are used:
if (hasValidCredentials)
if (isAccountActive)
result = "LOGIN"
else
result = "INACTIVE"
else
result = "FAIL"
What does the Boolean expression hasValidCredentials && !isAccountActive signify in the provided code?
Credentials are invalid and account is inactive
Credentials are valid but account is inactive
User can log in successfully
Account is active regardless of credentials
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, the expression hasValidCredentials && !isAccountActive represents a specific state where credentials are valid but the account is inactive. Choice B is correct because this expression evaluates to true only when hasValidCredentials is true AND isAccountActive is false (making !isAccountActive true), which signifies valid credentials with an inactive account status. Choice A is incorrect because it describes the opposite scenario where both conditions are positive, missing the NOT operator's effect. To help students: Emphasize reading Boolean expressions as English statements - "has valid credentials AND account is NOT active". Watch for: Students overlooking the NOT operator or misinterpreting its effect on the overall meaning.
A simulation loop uses isSimulationRunning and isErrorDetected:
while (isSimulationRunning && !isErrorDetected)
steps = steps + 1
if (steps > maxSteps)
isSimulationRunning = false
How does the program flow change when isErrorDetected becomes true?
steps resets to 0 and loop restarts
The while loop runs one extra time
Only maxSteps is checked, loop continues
The while loop stops immediately
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, the while loop condition (isSimulationRunning && !isErrorDetected) controls loop execution, requiring both conditions to be met for continuation. Choice A is correct because when isErrorDetected becomes true, the condition becomes isSimulationRunning && !true = isSimulationRunning && false = false, causing the loop to terminate immediately. Choice B is incorrect because it suggests the loop runs again after the condition becomes false, misunderstanding how while loops check their condition before each iteration. To help students: Emphasize that while loops check their condition at the start of each iteration and stop immediately when the condition becomes false. Watch for: Confusion about when loop conditions are evaluated and the immediate effect of condition changes.
In a game update loop, isEnemyVisible and isHealthLow control actions:
while (isGameRunning)
if (isEnemyVisible && !isHealthLow)
action = "ATTACK"
else if (isEnemyVisible && isHealthLow)
action = "RETREAT"
else
action = "SEARCH"
What will be the output of the following code if isEnemyVisible is true and isHealthLow is false?
action becomes "RETREAT" then "SEARCH"
action becomes "SEARCH"
action becomes "RETREAT"
action becomes "ATTACK"
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, expressions like isEnemyVisible && !isHealthLow are used to determine game actions, influencing the program's decision-making process. Choice B (action becomes "ATTACK") is correct because when isEnemyVisible is true and isHealthLow is false, the first condition (isEnemyVisible && !isHealthLow) evaluates to true && !false = true && true = true, causing action to be set to "ATTACK". Choice A is incorrect because "RETREAT" requires both isEnemyVisible and isHealthLow to be true, which doesn't match our given values. To help students: Emphasize tracing through conditional statements sequentially and evaluating each Boolean expression completely before moving to the next. Watch for: Students jumping to conclusions without carefully evaluating each condition in order.
In a login system, hasValidCredentials and isAccountActive are checked:
if (hasValidCredentials && isAccountActive)
access = "ALLOW"
else
access = "DENY"
for attempts from 1 to 3
if (!hasValidCredentials || !isAccountActive)
lockCounter = lockCounter + 1
Which Boolean expression evaluates to true when hasValidCredentials is true and isAccountActive is false?
hasValidCredentials || isAccountActive
!hasValidCredentials || !isAccountActive
!hasValidCredentials && !isAccountActive
hasValidCredentials && isAccountActive
Explanation
This question tests understanding of comparing Boolean expressions in AP Computer Science A, focusing on logical evaluation and selection/iteration concepts. Boolean expressions are logical statements that evaluate to true or false, guiding program flow through conditional constructs like if statements and loops. In the provided scenario, expressions like hasValidCredentials && isAccountActive are used to determine access control, influencing the program's decision-making process. Choice C (!hasValidCredentials || !isAccountActive) is correct because when hasValidCredentials is true and isAccountActive is false, the expression becomes !true || !false = false || true = true, accurately reflecting the logical condition. Choice A is incorrect because it would evaluate to true && false = false, a common error when students don't carefully evaluate each part of the expression. To help students: Emphasize the importance of understanding logical operators (&&, ||, !) and practice evaluating expressions step-by-step with concrete true/false values. Watch for: Confusion between AND/OR operations and misunderstanding of the NOT operator's effect on Boolean values.