What this quiz covers
This quiz focuses on Nested If Statements, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
Online Shopping Cart: apply discount using nested if. Code:
boolean isMember = true;
double purchaseAmount = 120.0, discount = 0.0;
// Members get higher discount for large purchases
if (isMember) {
discount = 0.10;
if (purchaseAmount >= 100.0) discount = 0.20;
}
What will be the output if the input is isMember=true, purchaseAmount=120.0?
AP Computer Science a Quiz
Practice Nested 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.
This quiz focuses on Nested If Statements, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.
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.
Online Shopping Cart: apply discount using nested if. Code:
boolean isMember = true;
double purchaseAmount = 120.0, discount = 0.0;
// Members get higher discount for large purchases
if (isMember) {
discount = 0.10;
if (purchaseAmount >= 100.0) discount = 0.20;
}
What will be the output if the input is isMember=true, purchaseAmount=120.0?
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, isMember is true so discount becomes 0.10, then the nested if checks if purchaseAmount >= 100.0 (120.0 >= 100.0 is true), so discount is updated to 0.20. The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A is incorrect because it stops at the first assignment without executing the nested condition; Choice B assumes no discount is applied; Choice D shows a mathematical error. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as stopping execution prematurely or misunderstanding how variable reassignment works within nested structures.
A grade program branches with nested ifs. What is grade when score=75 and extraCredit=true?
int score = 75;
boolean extraCredit = true;
String grade = "";
// Extra credit only affects borderline scores
if (score >= 80) {
grade = "B";
} else {
// Borderline can rise to B
if (extraCredit && score >= 78) grade = "B";
else grade = "C";
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, score=75 is not >= 80, so we go to the else block, where we check if extraCredit is true AND score >= 78, but 75 is not >= 78, so this condition is false and grade becomes "C". The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would require either score >= 80 or (extraCredit && score >= 78), and neither condition is met with score=75. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as misunderstanding compound conditions with AND operators or assuming extra credit always improves grades.
A shopping cart calculates discount with nested ifs. What is discount when isMember=false and purchaseAmount=200?
boolean isMember = false;
double purchaseAmount = 200.0;
double discount = 0.0;
// Members and non-members differ
if (isMember) {
if (purchaseAmount >= 150.0) discount = 0.20;
} else {
// Non-members only get discount at $250+
if (purchaseAmount >= 250.0) discount = 0.05;
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, isMember is false, so we skip the first if block and go to the else block, where we check if purchaseAmount (200.0) is >= 250.0, which is false, so discount remains 0.00. The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would require purchaseAmount >= 250 for non-members, and Choice B is only available to members with purchases >= 150. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as assuming non-members get the same discounts as members or misreading the threshold values.
In an online cart, this code applies discounts. What is discount when isMember=true and purchaseAmount=120?
boolean isMember = true;
double purchaseAmount = 120.0;
double discount = 0.0;
// Member gets bigger discount
if (isMember) {
// Larger discount for $100+
if (purchaseAmount >= 100.0) discount = 0.15;
else discount = 0.10;
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, since isMember is true, we enter the outer if block, then check if purchaseAmount (120.0) is >= 100.0, which is true, so discount becomes 0.15. The correct answer, Choice B, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A is incorrect because it would be the discount for members with purchases under $100, and Choice C is incorrect because members always get some discount in this code. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as assuming the else branch executes when the outer condition is true.
A traffic light uses nested if statements:
boolean vehicleDetected = true;
boolean pedestrianButtonPressed = true;
String lightStatus = "GREEN";
// Change only if cars absent
if (!vehicleDetected) {
if (pedestrianButtonPressed) lightStatus = "WALK";
else lightStatus = "FLASH";
}
What will be the output if the input is vehicleDetected=true and pedestrianButtonPressed=true?
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, the logic checks if !vehicleDetected (which evaluates to false since vehicleDetected is true), and since this condition is false, the entire nested if block is skipped, leaving lightStatus unchanged at "GREEN". The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choices A and B are incorrect because they assume the nested if block executes, which only happens when vehicleDetected is false. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as confusion with negation operators and assuming nested code always executes.
A traffic system updates lightStatus. What is lightStatus when vehicleDetected=false and pedestrianButtonPressed=true?
boolean vehicleDetected = false;
boolean pedestrianButtonPressed = true;
String lightStatus = "GREEN";
// Handle pedestrian request first
if (pedestrianButtonPressed) {
// If no car, keep pedestrians waiting
if (!vehicleDetected) lightStatus = "WAIT";
else lightStatus = "WALK";
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, pedestrianButtonPressed is true, so we enter the if block, then check if !vehicleDetected (which is !false = true), so lightStatus becomes "WAIT". The correct answer, Choice B, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would occur if vehicleDetected were true, and Choice C is incorrect because the code changes lightStatus when the conditions are met. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as misunderstanding the negation operator (!) or the logic of when pedestrians must wait.
A temperature controller runs this code. What are heaterStatus and acStatus when temperature=78 and humidity=55?
int temperature = 78;
int humidity = 55;
boolean heaterStatus = false;
boolean acStatus = false;
// Decide between heating and cooling
if (temperature > 75) {
// Only cool if humidity is moderate
if (humidity <= 60) acStatus = true;
} else {
if (temperature < 68) heaterStatus = true;
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, temperature=78 is greater than 75, so we enter the first if block, then check if humidity (55) is <= 60, which is true, so acStatus becomes true while heaterStatus remains false. The correct answer, Choice B, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would require temperature < 68, and Choice C would occur if humidity were > 60 or temperature were between 68 and 75. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as confusing the temperature thresholds or the humidity condition for cooling.
A grade calculator runs this nested if code. What is grade when score=92 and extraCredit=true?
int score = 92;
boolean extraCredit = true;
String grade = "";
// Top scores earn A variants
if (score >= 90) {
// Extra credit boosts label
if (extraCredit) grade = "A+";
else grade = "A";
} else {
grade = "B";
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, score=92 is >= 90, so we enter the first if block, then check if extraCredit is true, which it is, so grade becomes "A+". The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would occur if extraCredit were false, and Choice B would require score < 90. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as misunderstanding how boolean conditions affect string assignments within nested structures.
A cart program uses nested ifs. What is discount when isMember=true and purchaseAmount=80?
boolean isMember = true;
double purchaseAmount = 80.0;
double discount = 0.0;
// Members always get some discount
if (isMember) {
// Higher discount for $100+
if (purchaseAmount >= 100.0) discount = 0.15;
else discount = 0.10;
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, isMember is true, so we enter the if block, then check if purchaseAmount (80.0) is >= 100.0, which is false, so we execute the else branch and discount becomes 0.10. The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A would require purchaseAmount >= 100, and Choice B is incorrect because members always get some discount according to this code. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as assuming the higher discount applies to all member purchases.
A thermostat uses nested if statements. What are heaterStatus and acStatus when temperature=65 and humidity=40?
int temperature = 65;
int humidity = 40;
boolean heaterStatus = false;
boolean acStatus = false;
// Heat if too cold, cool if too hot
if (temperature < 68) {
// Avoid heating if very humid
if (humidity < 60) heaterStatus = true;
} else {
if (temperature > 75) acStatus = true;
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, temperature=65 is less than 68, so we enter the first if block, then check if humidity (40) is less than 60, which is true, so heaterStatus becomes true while acStatus remains false. The correct answer, Choice A, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice B would require temperature > 75, and Choice C would occur if humidity were >= 60 or temperature were between 68 and 75. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as assuming both heater and AC can be on simultaneously or misunderstanding the temperature thresholds.
A traffic light controller runs this code. What is lightStatus when vehicleDetected=true and pedestrianButtonPressed=true?
boolean vehicleDetected = true;
boolean pedestrianButtonPressed = true;
String lightStatus = "RED";
// Pedestrians override cars
if (pedestrianButtonPressed) {
// Only change if a car is present
if (vehicleDetected) lightStatus = "WALK";
else lightStatus = "WAIT";
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, pedestrianButtonPressed is true, so we enter the outer if block, then check if vehicleDetected is true, which it is, so lightStatus becomes "WALK". The correct answer, Choice A, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice D would occur if vehicleDetected were false, and Choice C is incorrect because the code explicitly changes lightStatus when conditions are met. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as confusing the logic of when pedestrians get priority over vehicles.
A grade program uses nested if logic. What is grade when score=88 and extraCredit=false?
int score = 88;
boolean extraCredit = false;
String grade = "";
// Base grade depends on score
if (score >= 90) {
grade = extraCredit ? "A+" : "A";
} else {
// Next tier is B
if (score >= 80) grade = "B";
else grade = "C";
}
Explanation: This question tests knowledge of nested if statements in AP Computer Science A, specifically understanding control flow and logical execution. Nested if statements allow for multiple conditional checks, where each level of nesting adds a layer of specificity to the decision-making process. In the provided code snippet, score=88 is not >= 90, so we skip to the else block, where we check if score >= 80, which is true (88 >= 80), so grade becomes "B". The correct answer, Choice C, correctly interprets the logic as it matches the expected outcome when the input conditions are applied. Choice A is incorrect because it requires a score of 90 or higher, and Choice D would require both score >= 90 AND extraCredit to be true. To help students: Encourage practice with tracing code execution paths, visualize nested conditions using flowcharts, and emphasize the importance of understanding each condition's role in the overall logic. Watch for common pitfalls such as misunderstanding the ternary operator or assuming all conditions execute sequentially.