if Statements
Help Questions
AP Computer Science A › if Statements
A bank app blocks withdrawals that exceed the available balance. Which condition must be met for balance to decrease?
int balance = 200;
int withdrawAmount = 150;
// Withdraw only if funds are sufficient
if (withdrawAmount <= balance) {
balance = balance - withdrawAmount;
}
System.out.println(balance);
balance is less than or equal to withdrawAmount
withdrawAmount is not equal to balance
withdrawAmount is less than or equal to balance
withdrawAmount is greater than balance
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically conditional execution for validation logic. If statements can protect operations by checking preconditions before allowing state changes. In this scenario, the code only subtracts withdrawAmount from balance if withdrawAmount <= balance, ensuring sufficient funds exist. Choice B is correct because for the balance to decrease (the withdrawal to occur), the condition withdrawAmount <= balance must be true - meaning the withdrawal amount must be less than or equal to the available balance. Choice A is incorrect because it reverses the logic - if withdrawAmount > balance, the condition would be false and no withdrawal would occur. To help students: Use real-world analogies like ATM withdrawals to make the logic concrete. Practice identifying what conditions allow or prevent code execution. Watch for: confusion about when conditions prevent versus allow actions, and mixing up the direction of inequality operators.
A weather alert system warns users for either heavy rain or high wind. Which condition must be met to print "Alert"?
int rainMm = 12;
int windMph = 20;
// Issue alert for heavy rain or high wind
if (rainMm >= 10 || windMph >= 40) {
System.out.println("Alert");
} else {
System.out.println("No Alert");
}
Either rainMm >= 10 or windMph >= 40
rainMm must be less than 10 for Alert
Only windMph >= 40, regardless of rainMm
Both rainMm >= 10 and windMph >= 40
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically the OR (||) operator in compound conditions. If statements with OR operators execute their code block when at least one of the connected conditions is true. In this scenario, the code checks if either rainMm >= 10 OR windMph >= 40, and since rainMm is 12 (which is >= 10), the first condition is true, making the entire compound condition true. Choice B is correct because the || (OR) operator requires only one condition to be true - either heavy rain (rainMm >= 10) or high wind (windMph >= 40) will trigger the alert. Choice A is incorrect because it describes AND logic, which would require both conditions to be true simultaneously. To help students: Create truth tables for OR operations showing that only one true condition is needed. Use real-world examples where either condition triggers an action. Watch for: confusing || (OR) with && (AND) operators, leading to incorrect logical interpretations.
A weather app issues alerts for dangerous conditions. What will be the output if the input windMph is 45?
int windMph = 45;
String alert = "None";
// Trigger alert for high wind
if (windMph >= 40) {
alert = "Wind Advisory";
}
System.out.println(alert);
Wind Watch
Wind Advisory
45
None
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically variable reassignment within conditional blocks. If statements can modify variables when their conditions are true, changing the program's state. In this scenario, the code initializes alert to "None", then checks if windMph >= 40, and if true, reassigns alert to "Wind Advisory". Choice C is correct because windMph (45) is greater than or equal to 40, so the condition is true, alert is reassigned to "Wind Advisory", and that new value is printed. Choice A is incorrect because it assumes the initial value persists, missing that the if statement modifies the variable. To help students: Trace through code showing how variables change value when conditions are met. Emphasize that assignment statements inside if blocks only execute when the condition is true. Watch for: students overlooking variable reassignments or assuming initial values remain unchanged.
A bank app charges a fee only when an account is overdrawn. What will be the output if the input balance is -5?
int balance = -5;
int fee = 0;
// Apply overdraft fee when balance is negative
if (balance < 0) {
fee = 35;
}
System.out.println(fee);
-5
0
30
35
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically conditional assignment based on negative value detection. If statements can check for special cases like negative numbers to trigger specific actions like applying fees. In this scenario, the code checks if balance < 0, and since -5 < 0 is true, the overdraft fee of 35 is applied. Choice C is correct because the balance of -5 satisfies the condition balance < 0 (negative values are less than zero), so fee is set to 35 and that value is printed. Choice B is incorrect because it prints the balance value instead of the fee value - the question asks for the output, which is the fee variable. To help students: Review how negative numbers compare to zero and positive numbers. Practice tracing which variable is being printed in the output statement. Watch for: confusion about which variable is being output or misunderstanding how negative numbers work in comparisons.
A login system locks accounts after too many failed attempts. How does the code handle failedAttempts input of 3?
int failedAttempts = 3;
boolean locked = false;
// Lock account after 3 or more failures
if (failedAttempts >= 3) {
locked = true;
}
System.out.println(locked);
It prints false because 3 is not greater than 3
It prints true only when failedAttempts equals 4
It prints 3 because failedAttempts is printed
It prints true because failedAttempts >= 3 is true
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically boolean variable assignment and the >= operator. If statements can set boolean flags based on conditions, commonly used for state tracking. In this scenario, the code checks if failedAttempts >= 3, and since 3 >= 3 is true (the >= operator includes equality), locked is set to true. Choice B is correct because failedAttempts (3) satisfies the condition failedAttempts >= 3, making the condition true, so locked becomes true and that value is printed. Choice A is incorrect because it misunderstands the >= operator - it includes the boundary value, so 3 >= 3 is true, not false. To help students: Clarify the difference between > (strictly greater) and >= (greater or equal) operators. Use number lines to visualize which values satisfy different comparison operators. Watch for: confusion between > and >= operators, especially at boundary values.
In a grading system, letter grades depend on score thresholds. How does the code handle a score input of 90?
int score = 90;
String grade;
// Assign letter grade
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else {
grade = "C";
}
System.out.println(grade);
It prints nothing because grade is uninitialized
It prints "A" because score >= 90 is true
It prints "C" because only the else runs
It prints "B" because 90 is borderline
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically if-else-if chains and boundary conditions. If-else-if statements check conditions sequentially, executing the first block where the condition is true and skipping remaining conditions. In this scenario, the code checks score >= 90 first, and since 90 >= 90 is true, it assigns grade = "A" and skips the remaining else-if and else blocks. Choice B is correct because the score of 90 satisfies the first condition (score >= 90), so grade becomes "A" and that value is printed. Choice A is incorrect because it misunderstands that >= includes the boundary value - 90 is not "borderline" but fully satisfies the condition. To help students: Emphasize that >= means "greater than or equal to" and includes the exact value. Practice with boundary values to reinforce understanding of comparison operators. Watch for: students forgetting that once a condition in an if-else-if chain is true, subsequent conditions are not evaluated.
A shopping cart adds free shipping for large orders. What will be the output if the input totalAmount is 50?
int totalAmount = 50;
int shipping = 8;
// Free shipping for orders over 75
if (totalAmount > 75) {
shipping = 0;
}
System.out.println(shipping);
0
8
50
75
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically conditional modification of values based on thresholds. If statements can selectively modify variables when certain conditions are met, leaving them unchanged otherwise. In this scenario, shipping starts at 8 and only changes to 0 if totalAmount > 75, but since 50 is not greater than 75, the condition is false and shipping remains 8. Choice B is correct because the condition totalAmount > 75 evaluates to false (50 > 75 is false), so the code inside the if block doesn't execute, leaving shipping at its initial value of 8. Choice A is incorrect because it assumes the if statement always modifies the variable, not recognizing that false conditions skip the block. To help students: Emphasize that when if conditions are false, the code block is skipped entirely. Practice with examples where variables do and don't change based on conditions. Watch for: assuming if statements always execute their blocks regardless of the condition.
A grading tool assigns pass/fail based on a minimum score. What changes are needed for the code to treat score 60 as passing?
int score = 60;
String result = "Fail";
// Passing requires at least 60
if (score > 60) {
result = "Pass";
}
System.out.println(result);
Change print statement to System.out.print(score)
Change result initialization to "Pass"
Change condition to score >= 60
Change condition to score == 100
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically identifying and fixing off-by-one errors in conditions. If statements must use the correct comparison operator to include or exclude boundary values as intended. In this scenario, the current condition score > 60 excludes a score of exactly 60, but the requirement states 60 should be passing, requiring score >= 60 instead. Choice A is correct because changing the condition from score > 60 to score >= 60 would include the boundary value of 60, making it evaluate to true and setting result to "Pass". Choice C is incorrect because changing the initialization wouldn't fix the logic - the if statement would still change it back to "Fail" for scores not greater than 60. To help students: Draw number lines showing which values satisfy > versus >= conditions. Practice identifying boundary cases in problem descriptions. Watch for: off-by-one errors where students use > when >= is needed or vice versa.
In a login screen, access is granted only with correct credentials. Which condition must be met for the code to print "Access granted"?
String inputUser = "admin";
String inputPass = "p@ss";
String storedUser = "admin";
String storedPass = "p@ss";
// Verify credentials
if (inputUser.equals(storedUser) && inputPass.equals(storedPass)) {
System.out.println("Access granted");
} else {
System.out.println("Access denied");
}
Either username or password matches stored values
Both username and password match stored values
Username differs but password matches stored value
Username matches, regardless of the password
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically compound conditions using the AND (&&) operator. If statements with compound conditions require all parts connected by && to be true for the entire condition to evaluate to true. In this scenario, the code checks if both inputUser.equals(storedUser) AND inputPass.equals(storedPass) are true before granting access. Choice C is correct because the && operator requires both conditions to be true - the username must match AND the password must match for "Access granted" to print. Choice A is incorrect because it describes OR logic, not AND logic - with &&, one matching credential is not sufficient. To help students: Use truth tables to demonstrate how && requires both conditions to be true. Practice evaluating compound conditions piece by piece. Watch for: confusing && (AND) with || (OR) operators, which is a common source of logic errors.
In a shopping cart, discounts apply when totals are high. What will be the output if the input totalAmount is 120?
int totalAmount = 120;
int discount = 0;
// Apply discount for large purchases
if (totalAmount > 100) {
discount = 10;
}
int finalTotal = totalAmount - discount;
System.out.println(finalTotal);
10
100
110
120
Explanation
This question tests understanding of if statements in AP Computer Science A, specifically conditional execution and arithmetic operations. If statements allow a program to make decisions based on conditions, executing certain blocks of code when conditions are true. In this scenario, the code checks if totalAmount > 100 and applies a discount of 10 if true, then calculates finalTotal = totalAmount - discount. Choice A (110) is correct because when totalAmount is 120, the condition 120 > 100 is true, so discount becomes 10, and finalTotal = 120 - 10 = 110. Choice B (120) is incorrect because it assumes no discount is applied, which would only happen if the condition were false. To help students: Practice tracing code step-by-step with specific values to see how variables change. Encourage students to identify what conditions trigger which code blocks. Watch for: students forgetting to apply the arithmetic operation after the if statement or misunderstanding the greater-than operator.