Nested if Statements

Help Questions

AP Computer Science A › Nested if Statements

Questions 1 - 10
1

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;

}

discount remains 0.00

discount becomes 0.10

discount becomes 0.25

discount becomes 0.15

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.

2

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

}

lightStatus remains "GREEN"

lightStatus becomes "WAIT"

lightStatus becomes "RED"

lightStatus becomes "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.

3

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;

}

heaterStatus=true, acStatus=false

heaterStatus=false, acStatus=true

heaterStatus=true, acStatus=true

heaterStatus=false, acStatus=false

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.

4

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

}

grade is "A+"

grade is "A"

grade is "C"

grade is "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.

5

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;

}

discount becomes 0.10

discount remains 0.00

discount becomes 0.15

discount becomes 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 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.

6

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;

}

discount becomes 0.25

discount becomes 0.20

discount becomes 0.05

discount remains 0.00

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.

7

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;

}

heaterStatus=false, acStatus=true

heaterStatus=false, acStatus=false

heaterStatus=true, acStatus=false

heaterStatus=true, 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.

8

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

}

grade is "A+"

grade is "B"

grade is "C"

grade is "A"

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.

9

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

}

lightStatus remains "RED"

lightStatus becomes "WAIT"

lightStatus becomes "WALK"

lightStatus becomes "GREEN"

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.

10

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

}

grade is "B"

grade is "A"

grade is "A+"

grade is "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.

Page 1 of 2