AP Computer Science a Quiz: If Statements
20 questions · exam conditions
0:00
If StatementsQuestion 1 of 20

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 89 is below 70
Prints PASS because 89 meets the threshold
Prints FAIL because result cannot change
Prints PASS because score must equal 70
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: If Statements

Practice If Statements in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on If Statements, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 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);
  1. Prints FAIL because 89 is below 70
  2. Prints PASS because 89 meets the threshold (correct answer)
  3. Prints FAIL because result cannot change
  4. Prints PASS because score must equal 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.

Question 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");
}
  1. Both rainMm >= 10 and windMph >= 40
  2. Either rainMm >= 10 or windMph >= 40 (correct answer)
  3. Only windMph >= 40, regardless of rainMm
  4. rainMm must be less than 10 for Alert

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.

Question 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);
  1. None
  2. Wind Watch
  3. Wind Advisory (correct answer)
  4. 45

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.

Question 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);
  1. totalAmount is greater than 200
  2. totalAmount is at least 200 (correct answer)
  3. discountPercent equals 15
  4. totalAmount is less than 200

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.

Question 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);
  1. B
  2. A (correct answer)
  3. F
  4. 90

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.

Question 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);
  1. 0
  2. 10 (correct answer)
  3. 100
  4. 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.

Question 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);
  1. Prints true because both conditions evaluate true (correct answer)
  2. Prints false because strings cannot be compared
  3. Prints true because only username must match
  4. 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.

Question 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);
  1. withdrawAmount is greater than balance
  2. withdrawAmount is less than or equal to balance (correct answer)
  3. balance is less than or equal to withdrawAmount
  4. withdrawAmount is not 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.

Question 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);
  1. It prints "B" because 90 is borderline
  2. It prints "A" because score >= 90 is true (correct answer)
  3. It prints "C" because only the else runs
  4. It prints nothing because grade is uninitialized

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.

Question 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);
  1. 0
  2. 8 (correct answer)
  3. 75
  4. 50

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.

Question 11

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);
  1. It prints false because 3 is not greater than 3
  2. It prints true because failedAttempts >= 3 is true (correct answer)
  3. It prints 3 because failedAttempts is printed
  4. It prints true only when failedAttempts equals 4

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.

Question 12

In bank account management, how does the code handle withdrawalAmount = 80?

// Allow withdrawal only if sufficient funds exist
int balance = 50;
int withdrawalAmount = 80;

if (withdrawalAmount <= balance) {
    balance = balance - withdrawalAmount;
}

System.out.println(balance);
  1. Prints -30 because withdrawal always subtracts
  2. Prints 50 because the condition evaluates false (correct answer)
  3. Prints 80 because balance becomes withdrawalAmount
  4. Prints 0 because balance cannot be negative

Explanation: This question tests understanding of if statements in AP Computer Science A, specifically conditional execution preventing invalid 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 withdrawalAmount (80) is less than or equal to balance (50) before allowing the withdrawal. Choice B is correct because the condition (80 <= 50) evaluates to false, so the withdrawal doesn't occur and balance remains 50, which is then printed. Choice A is incorrect because it assumes the subtraction always happens regardless of the condition, not understanding that the if statement protects against overdrafts. To help students: Use real-world banking scenarios to explain why conditions prevent certain operations. Trace code showing what happens when conditions are false (no execution of the block). Watch for: assuming operations inside if blocks always execute or misunderstanding comparison direction.

Question 13

In a weather alert system, which condition must be met for the code to print "ALERT"?

// Issue an alert when wind speed is dangerous
int windMph = 45;

if (windMph > 50) {
    System.out.println("ALERT");
}

System.out.println("DONE");
  1. windMph is at least 50
  2. windMph is greater than 50 (correct answer)
  3. windMph is less than 50
  4. System.out.println("DONE") executes first

Explanation: This question tests understanding of if statements in AP Computer Science A, specifically strict inequality comparisons. 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 windMph is strictly greater than 50 to trigger an alert. Choice B is correct because the condition uses the > operator, which requires windMph to be greater than (not equal to) 50 for "ALERT" to print - since windMph is 45, the condition is false and only "DONE" prints. Choice A is incorrect because it confuses > (strictly greater) with >= (greater than or equal), missing that 50 itself would not trigger the alert. To help students: Emphasize the difference between > and >= operators using number line visualizations. Practice with boundary values like 49, 50, and 51 to see behavior differences. Watch for: assuming > includes the boundary value or that no output occurs when the condition is false.

Question 14

In a weather alert system, what will be the output if the input is temperatureF = 32?

// Freeze warning when temperature is at or below freezing
int temperatureF = 32;
String message = "OK";

if (temperatureF <= 32) {
    message = "FREEZE";
}

System.out.println(message);
  1. OK
  2. FREEZE (correct answer)
  3. 32
  4. ERROR

Explanation: This question tests understanding of if statements in AP Computer Science A, specifically less-than-or-equal-to comparisons. 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 temperatureF is at or below 32 degrees (freezing point) to issue a freeze warning. Choice B is correct because when temperatureF is 32, the condition (32 <= 32) evaluates to true, so message is changed from "OK" to "FREEZE", which is then printed. Choice A is incorrect because it assumes the condition is false at the boundary value, not understanding that <= includes equality. To help students: Use real-world examples like freezing point to make boundary conditions memorable. Practice with values at, above, and below thresholds to understand inclusive operators. Watch for: confusing <= with < or assuming the temperature value itself gets printed.

Question 15

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);
  1. 0
  2. -5
  3. 35 (correct answer)
  4. 30

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.

Question 16

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);
  1. Change condition to score >= 60 (correct answer)
  2. Change condition to score == 100
  3. Change result initialization to "Pass"
  4. Change print statement to System.out.print(score)

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.

Question 17

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");
}
  1. Either username or password matches stored values
  2. Username matches, regardless of the password
  3. Both username and password match stored values (correct answer)
  4. Username differs but password matches stored value

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.

Question 18

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);
  1. 110 (correct answer)
  2. 120
  3. 100
  4. 10

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.

Question 19

In a login authentication check, which condition must be met for the code to set isAuthenticated to true?

// Authenticate when either an admin account OR a support account logs in
String username = "support";
boolean isAuthenticated = false;

if (username.equals("admin") || username.equals("support")) {
    isAuthenticated = true;
}

System.out.println(isAuthenticated);
  1. username equals both "admin" and "support"
  2. username equals either "admin" or "support" (correct answer)
  3. username is not equal to "admin"
  4. isAuthenticated is already true before the if

Explanation: This question tests understanding of if statements in AP Computer Science A, specifically compound conditions using the OR (||) 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 username equals either "admin" OR "support" using the || operator. Choice B is correct because the condition requires username to equal either "admin" or "support" - since username is "support", one part of the OR condition is true, making the entire condition true. Choice A is incorrect because it misinterprets || as requiring both conditions, which would be impossible for a single string variable. To help students: Emphasize that || (OR) requires only ONE condition to be true for the overall result to be true. Practice with truth tables to understand OR logic. Watch for: confusing || (OR) with && (AND) or thinking a variable must satisfy multiple equality conditions simultaneously.

Question 20

In bank account management, which condition must be met for the balance update statement to execute?

// Apply a $5 fee only when balance drops below $25
int balance = 20;

if (balance < 25) {
    balance = balance - 5;
}

System.out.println(balance);
  1. balance is less than 25 (correct answer)
  2. balance is less than or equal to 25
  3. balance is greater than 25
  4. balance equals 5 after the if statement

Explanation: This question tests understanding of if statements in AP Computer Science A, specifically identifying the exact condition for code 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 applies a $5 fee when the balance drops below $25 using the < operator. Choice A is correct because the condition explicitly states (balance < 25), meaning the balance must be strictly less than 25 for the fee to be applied - with balance at 20, this condition is true. Choice B is incorrect because it includes equality (<=), but the actual condition uses strict inequality (<), so a balance of exactly 25 would not trigger the fee. To help students: Focus on reading conditions exactly as written without adding assumptions. Practice distinguishing between < and <= with specific test values like 24, 25, and 26. Watch for: adding equality when not present in the condition or confusing what the condition tests versus what the code does.