if Statements

Help Questions

AP Computer Science A › if Statements

Questions 1 - 10
1

In a grading system, how does the code handle score = 89?


// Determine pass/fail for a course

int score = 89;

String result = "FAIL";

if (score >= 70) {

    result = "PASS";

}

System.out.println(result);

Prints FAIL because result cannot change

Prints PASS because score must equal 70

Prints PASS because 89 meets the threshold

Prints FAIL because 89 is below 70

Explanation

This question tests understanding of if statements in AP Computer Science A, specifically evaluating conditions with comparison operators. 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 score (89) is greater than or equal to 70 to determine pass/fail status. Choice B is correct because when score is 89, the condition (89 >= 70) evaluates to true, so result is changed from "FAIL" to "PASS", which is then printed. Choice A is incorrect because it misreads the condition as checking if the score is below 70, when actually it checks if the score is 70 or above. To help students: Practice evaluating numeric comparisons with specific values to build intuition. Encourage reading conditions carefully to avoid inverting the logic. Watch for: misreading >= as < or assuming the threshold value itself would fail.

2

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");
}

Only windMph >= 40, regardless of rainMm

rainMm must be less than 10 for Alert

Either rainMm >= 10 or windMph >= 40

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.

3

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 Advisory

45

Wind Watch

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.

4

In a shopping cart discount system, which condition must be met for the code to execute the discount assignment block?


// Apply a 15% discount for orders of $200 or more

int totalAmount = 180;

int discountPercent = 0;

if (totalAmount >= 200) {

    discountPercent = 15;

}

System.out.println(discountPercent);

totalAmount is at least 200

totalAmount is greater than 200

totalAmount is less than 200

discountPercent equals 15

Explanation

This question tests understanding of if statements in AP Computer Science A, specifically conditional operators and boundary conditions. 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 is greater than or equal to 200 using the >= operator. Choice B is correct because the condition (totalAmount >= 200) means totalAmount must be at least 200 (200 or more) for the discount assignment block to execute. Choice A is incorrect because it suggests only values strictly greater than 200 would work, missing that >= includes the boundary value of 200 itself. To help students: Emphasize the difference between > (strictly greater) and >= (greater than or equal to) operators. Practice with boundary values to understand inclusive vs exclusive conditions. Watch for: confusing >= with > or assuming the current value of totalAmount affects the condition definition.

5

In a grading system, what will be the output if the input is score = 90?


// Assign letter grade based on score threshold

int score = 90;

String grade = "F";

if (score >= 90) {

    grade = "A";

}

System.out.println(grade);

A

B

90

F

Explanation

This question tests understanding of if statements in AP Computer Science A, specifically variable reassignment within conditional blocks. 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 score (90) is greater than or equal to 90 and reassigns the grade variable if true. Choice B is correct because when score is 90, the condition (90 >= 90) evaluates to true, so grade is changed from "F" to "A", which is then printed. Choice C is incorrect because it assumes the initial value "F" cannot be changed, misunderstanding that variables can be reassigned within if statements. To help students: Trace through code showing how variable values change when conditions are met. Emphasize that initial values can be overwritten by assignments inside if blocks. Watch for: assuming initial values are permanent or that the condition value gets printed instead of the variable.

6

In a shopping cart discount system, what will be the output if the input is totalAmount = 120?


// Apply a 10% discount for orders over $100

int totalAmount = 120;

int discountPercent = 0;

if (totalAmount > 100) {

    discountPercent = 10;

}

System.out.println(discountPercent);

0

10

100

120

Explanation

This question tests understanding of if statements in AP Computer Science A, specifically selection and conditional execution. 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 (120) is greater than 100 and executes the discount assignment if true. Choice B is correct because when totalAmount is 120, the condition (120 > 100) evaluates to true, so discountPercent is set to 10, which is then printed. Choice C is incorrect because it assumes the discount amount (12) would be printed instead of the discount percentage, which is a common error when students confuse percentage with calculated values. To help students: Practice tracing code step-by-step with specific values to see variable changes. Encourage students to identify what exactly is being printed (discountPercent, not a calculation). Watch for: assuming mathematical operations occur when only assignments are present.

7

In a login authentication check, how does the code handle username="admin" and password="1234"?


// Authenticate user based on exact credentials

String username = "admin";

String password = "1234";

boolean isAuthenticated = false;

if (username.equals("admin") && password.equals("1234")) {

    isAuthenticated = true;

}

System.out.println(isAuthenticated);

Prints true because only username must match

Prints true because both conditions evaluate true

Prints false because strings cannot be compared

Prints false because && requires either condition true

Explanation

This question tests understanding of if statements in AP Computer Science A, specifically compound conditions using the AND (&&) operator. 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 both username equals "admin" AND password equals "1234" using the && operator. Choice A is correct because both conditions (username.equals("admin") and password.equals("1234")) evaluate to true, so the entire compound condition is true, setting isAuthenticated to true. Choice D is incorrect because it misunderstands how && works - it requires BOTH conditions to be true, not just either one. To help students: Practice evaluating compound conditions step by step, checking each part separately first. Emphasize that && requires ALL conditions to be true for the overall result to be true. Watch for: confusing && (AND) with || (OR) or assuming partial matches are sufficient.

8

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);

withdrawAmount is not equal to balance

withdrawAmount is greater than balance

balance is less than or equal to withdrawAmount

withdrawAmount is less than or equal to 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.

9

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 "C" because only the else runs

It prints "B" because 90 is borderline

It prints "A" because score >= 90 is true

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.

10

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.

Page 1 of 2