Conditionals
Help Questions
AP Computer Science Principles › Conditionals
In the given scenario, a security system uses: IF motionDetected==true alarm="ON" ELSE alarm="OFF". Which branch of the condition will execute if motionDetected is false?
The IF branch executes; alarm becomes "ON".
The ELSE branch executes; alarm becomes "OFF".
Neither branch executes; alarm stays unchanged.
Both branches execute; alarm toggles twice.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how boolean conditions determine which branch executes. Conditionals evaluate boolean expressions (true/false) and execute the corresponding branch based on the result. Choice B is correct because when motionDetected is false, the condition (motionDetected==true) evaluates to false, causing the ELSE branch to execute and set alarm to "OFF". Choice C is incorrect because it suggests both branches can execute, but in an IF-ELSE structure, exactly one branch runs - never both. To help students: Clarify that IF-ELSE is mutually exclusive and use state diagrams to show how the system responds to different inputs. Practice with boolean variables to reinforce true/false logic.
Based on the code snippet for student grading: IF score > 90 grade="A" ELSE IF score >= 80 grade="B" ELSE grade="C". What is the output when score is 90?
grade becomes "A" because $90 \ge 90$.
grade becomes "C" because 90 is a boundary case.
No grade is assigned because conditions conflict.
grade becomes "B" because $90$ is not $>90$.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically the critical difference between > (greater than) and >= (greater than or equal to) operators. Conditionals evaluate conditions precisely as written, and boundary values often reveal student understanding of comparison operators. Choice B is correct because when score is 90, the first condition (score > 90) is false since 90 is not greater than 90, but the second condition (score >= 80) is true, so grade becomes "B". Choice A is incorrect because it confuses > with >=, a common error when students don't pay attention to boundary conditions. To help students: Use number lines to visualize the difference between > and >= operators. Create exercises specifically testing boundary values and emphasize reading operators carefully.
In the given scenario, an online store uses: IF isPremium==true OR cartTotal>=100 discount=0.20 ELSE discount=0.00. Which branch of the condition will execute if isPremium is false and cartTotal is 120?
The ELSE branch executes; discount becomes 0.00.
The IF branch executes; discount becomes 0.20.
No branch executes because both must be true.
The ELSE branch executes; cartTotal overrides OR.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how the OR logical operator works in compound conditions. The OR operator makes a condition true if at least one of its parts is true, allowing multiple paths to the same outcome. Choice A is correct because even though isPremium is false, cartTotal (120) is greater than or equal to 100, making the overall condition true due to the OR operator, so the IF branch executes and discount becomes 0.20. Choice D is incorrect because it misunderstands how OR works - any true condition makes the whole expression true. To help students: Use truth tables for OR operations and practice evaluating compound conditions step by step. Emphasize that OR needs only one true condition, while AND needs all conditions true.
Based on the code snippet, a weather app uses: IF temperature>75 AND weather=="sunny" suggest="beach" ELSE suggest="cafe". Which branch of the condition will execute if temperature is 80 and weather is "cloudy"?
The ELSE branch executes; it suggests "cafe".
The IF branch executes because one condition is true.
No suggestion is made because AND cancels output.
The IF branch executes; it suggests "beach".
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how the AND logical operator requires all conditions to be true. The AND operator creates a compound condition that is only true when every individual condition is satisfied. Choice B is correct because although temperature (80) is greater than 75, weather is "cloudy" not "sunny", so the overall condition is false (true AND false = false), causing the ELSE branch to execute and suggest "cafe". Choice C is incorrect because it assumes one true condition is sufficient, confusing AND with OR logic. To help students: Create truth tables showing AND requires all conditions true and use real-world analogies like "you need money AND time to go on vacation". Practice tracing compound conditions with mixed true/false values.
In the given scenario, a traffic light uses: IF light=="green" action="go" ELSE IF light=="yellow" action="slow" ELSE action="stop". What will happen if light is "yellow"?
It sets action to "go" from the first IF.
It sets action to "slow" from the ELSE IF.
It sets no action because yellow is not green.
It sets action to "stop" using the final ELSE.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how cascading IF-ELSE IF-ELSE structures evaluate conditions in sequence. When multiple conditions exist, the program checks each in order and executes the first matching branch, then skips the rest. Choice C is correct because when light is "yellow", the first condition (light=="green") is false, but the second condition (light=="yellow") is true, so action becomes "slow" and the final ELSE is never reached. Choice B is incorrect because it assumes the program jumps to ELSE without checking intermediate conditions. To help students: Use flowcharts to trace the path through multiple conditions and emphasize that execution stops after the first true condition. Practice with various inputs to show how order matters in ELSE IF chains.
Based on the code snippet, a grading system uses: IF score>=90 grade="A" ELSE IF score>=70 grade="C" ELSE grade="F". How would you modify the condition to award "B" for $80\le score<90$?
Remove the ELSE IF score>=70 to avoid overlap.
Change the first test to IF score>80 grade="A".
Insert ELSE IF score>=80 grade="B" before score>=70.
Replace ELSE with IF score>=80 grade="B" at end.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how to modify existing conditional structures to add new logic branches. When adding conditions to an existing IF-ELSE IF chain, placement and order are crucial because conditions are evaluated sequentially. Choice A is correct because inserting "ELSE IF score>=80 grade="B"" between the existing conditions properly handles the range 80≤score<90 - scores of 90+ still get "A", scores 80-89 get "B", and the existing logic for "C" and "F" remains intact. Choice B is incorrect because changing to score>80 would exclude score=80 from getting an "A", leaving it to fall through to "C". To help students: Draw number lines showing grade ranges and emphasize that ELSE IF conditions must be ordered from most restrictive to least restrictive. Practice modifying existing code rather than just writing from scratch.
In the given scenario, an online store applies discounts using this pseudo-code: IF membershipLevel == "premium" THEN finalPrice = price * 0.80 ELSE finalPrice = price. Which branch of the condition will execute if membershipLevel is "premium"?
The ELSE branch executes; no discount applies.
The IF branch executes; 20% discount applies.
Neither branch executes; finalPrice stays unset.
Both branches execute; finalPrice is overwritten.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how logical branches determine program flow. Conditionals allow programs to execute different actions based on whether a condition is true or false. They use 'if', 'else if', and 'else' statements to control flow. Choice C is correct because when membershipLevel equals "premium", the condition in the IF statement evaluates to true, causing the IF branch to execute and apply the 20% discount (finalPrice = price * 0.80). Choice A is incorrect because it assumes the ELSE branch runs when the condition is true, which shows a fundamental misunderstanding of how IF-ELSE logic works. To help students: Use flowcharts to visualize the decision path and trace through examples with specific values. Emphasize that only one branch executes in an IF-ELSE structure - never both.
In the given scenario, a weather app uses: IF weatherCondition=="sunny" suggest="park" ELSE IF weatherCondition=="rainy" suggest="museum" ELSE suggest="stay home". What will happen if weatherCondition is "cloudy"?
It suggests "museum" because cloudy implies rain.
It suggests "stay home" using the ELSE branch.
It suggests nothing because no condition matches.
It suggests "park" because cloudy counts as sunny.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how ELSE clauses handle cases when no previous conditions match. Conditionals evaluate each condition sequentially, and the ELSE clause serves as a catch-all for any input not matching previous conditions. Choice C is correct because when weatherCondition is "cloudy", it doesn't match "sunny" or "rainy", so the program executes the ELSE branch and suggests "stay home". Choice B is incorrect because it assumes the program makes inferences about weather relationships, but conditionals only check exact matches. To help students: Emphasize that string comparisons require exact matches and that ELSE handles all unspecified cases. Create exercises with various inputs to show how ELSE acts as a default option.
Based on the code snippet for student grading: IF score >= 90 grade="A" ELSE IF score >= 80 grade="B" ELSE grade="C". What is the output when score is 89?
grade becomes "C" because 89 is not 90.
No grade is assigned due to missing ELSE IF.
grade becomes "A" because 89 is near 90.
grade becomes "B" because $89 \ge 80$.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how cascading IF-ELSE IF statements evaluate conditions sequentially. Conditionals check each condition in order and execute the first branch where the condition is true, then skip remaining conditions. Choice B is correct because when score is 89, the first condition (score >= 90) is false, but the second condition (score >= 80) is true, so grade becomes "B". Choice A is incorrect because it assumes rounding or approximation, which doesn't happen in conditional logic - 89 is strictly less than 90. To help students: Practice tracing through each condition step-by-step and emphasize that conditions are checked in order. Use number lines to visualize boundary values and teach students to test edge cases like 89, 90, and 80.
Based on the code snippet for a traffic light: IF lightColor=="green" action="go" ELSE action="stop". What is the output when lightColor is "red"?
No action is set because red needs ELSE IF.
action becomes "go" because red is not checked.
action becomes "go" because ELSE defaults to go.
action becomes "stop" because the ELSE runs.
Explanation
This question tests understanding of conditionals in AP Computer Science Principles, specifically how simple IF-ELSE structures handle non-matching conditions. Conditionals with IF-ELSE have exactly two paths: one for when the condition is true and one for all other cases. Choice B is correct because when lightColor is "red", it doesn't equal "green", so the condition is false and the ELSE branch executes, setting action to "stop". Choice A is incorrect because it misunderstands that ELSE handles all cases where the IF condition is false, not just unchecked cases. To help students: Use truth tables to show all possible outcomes and emphasize that IF-ELSE is exhaustive - one branch always executes. Practice with different values to reinforce that ELSE catches everything the IF doesn't.