AP Computer Science A • Unit 2.4

Nested if Statements

Detailed answer explanations for the Unit 2.4 multiple-choice practice questions.

Q1B
Q2D
Q3C
Q4A
Q5B
Q6A
Q7A
Q8B
Q9B
How to use this page: First try each question without looking at the explanation. Then compare your answer with the key and read why the other choices are not correct.

Answer Explanations

Q1

Consider the following code segment. What is printed as a result of executing the code segment?

int points = 4;
boolean member = true;
boolean early = false;

if (member) {
    points += 3;
    if (early) {
        points *= 2;
    } else {
        points -= 1;
    }
}
System.out.println(points);
A4
B6
C7
D14
Correct answer: B. The outer condition member is true, so points becomes 7, and the inner else then subtracts 1.
Explanation

Execution enters the outer block because member is true, so points += 3; raises points from 4 to 7. The inner condition early is false, so the inner else branch runs and points -= 1; leaves 6.

  • A is not correct: the outer condition is true, so the statements inside the outer block do run and change points from its starting value.
  • C is not correct: this stops after points += 3; and ignores the inner selection, but the inner else branch still runs and subtracts 1.
  • D is not correct: points *= 2; belongs to the inner if branch, which is skipped because early is false when that condition is evaluated.
Q2

Consider the following code segment. What is printed as a result of executing the code segment?

int level = 3;

if (level >= 3) {
    level += 2;
    if (level > 4) {
        level *= 2;
    }
}
System.out.println(level);
A3
B5
C8
D10
Correct answer: D. level is raised to 5 first, so the inner condition level > 4 is true and doubling gives 10.
Explanation

The outer condition level >= 3 is true, so the block runs. level += 2; stores 5, and the inner condition is tested against that updated value rather than the original 3, so level *= 2; runs.

  • A is not correct: the outer condition level >= 3 is true, so the block runs and level does not keep its starting value.
  • B is not correct: the inner condition is checked after the addition, and 5 is greater than 4, so the doubling statement also runs.
  • C is not correct: 8 would require the doubling to act on 4, but the addition happens first, so the value that is doubled is 5.
Q3

Consider the following code segment. What is printed as a result of executing the code segment?

boolean open = false;
int fee = 8;

if (open) {
    fee -= 2;
} else {
    if (fee > 5) {
        fee += 4;
    } else {
        fee = 0;
    }
}
System.out.println(fee);
A0
B6
C12
D8
Correct answer: C. open is false, so the outer else runs, and fee > 5 is true, giving 12.
Explanation

When the outer condition is false, Java skips the outer block and runs the else branch instead. That branch holds a nested if/else, and fee starts at 8, so fee += 4; produces 12.

  • A is not correct: fee = 0; is the inner else branch, and it runs only when fee > 5 is false.
  • B is not correct: fee -= 2; belongs to the branch taken when open is true, and that branch is skipped entirely here.
  • D is not correct: the outer else branch is entered rather than skipped, so fee does not keep its starting value of 8.
Q4

Consider the following code segment. If the entire second if statement were moved inside the body of the first if, what would be printed?

int total = 1;
boolean first = false;
boolean second = true;

if (first) {
    total += 2;
}
if (second) {
    total += 5;
}
System.out.println(total);
A1
B3
C6
D8
Correct answer: A. Moving the second if inside the first makes it unreachable, because first is false and the outer block is skipped.
Explanation

If the outer condition is false, Java skips the entire outer block, and any inner if statements inside it are never evaluated. The condition second is never reached, so total keeps its starting value.

  • B is not correct: total += 2; runs only when first is true, and it is false, so that addition never happens.
  • C is not correct: total += 5; would need the outer condition to be true, since nesting places that statement inside a block that is skipped.
  • D is not correct: both additions would require both conditions to be reached, but the outer condition is false, so neither statement runs.
Q5

Consider the following code segment. What is printed as a result of executing the code segment?

int weight = 10;

if (weight > 20) {
    System.out.println("Heavy");
} else if (weight >= 10) {
    System.out.println("Medium");
} else {
    System.out.println("Light");
}
AHeavy
BMedium
CLight
DNothing is printed.
Correct answer: B. The first condition is false, and weight >= 10 is true, so that branch prints Medium.
Explanation

An else if chain checks conditions in order, and only the first true branch runs. weight is 10, so weight > 20 is false and the next condition succeeds.

  • A is not correct: weight > 20 is false because 10 is not greater than 20, so that branch is skipped.
  • C is not correct: the final else runs only when none of the earlier conditions is true, and the second condition is already true here.
  • D is not correct: the chain ends with an else, so exactly one branch always runs and some output is produced.
Q6

Consider the following code segment. What is printed as a result of executing the code segment?

int temp = 35;

if (temp >= 20) {
    System.out.println("Warm");
} else if (temp >= 30) {
    System.out.println("Hot");
} else {
    System.out.println("Cool");
}
AWarm
BHot
CCool
DBoth Warm and Hot
Correct answer: A. temp >= 20 is true, so that branch runs and the later conditions in the chain are not checked.
Explanation

When conditions overlap, the order of checks can change the result. Only the first true branch of an else if chain runs, so the wider condition placed first also captures values that satisfy the later threshold.

  • B is not correct: temp >= 30 is never tested, because the earlier condition already matched and the chain stopped there.
  • C is not correct: the final else runs only when every earlier condition is false, and the first condition is true here.
  • D is not correct: an else if chain runs only the first true branch, so it cannot produce both messages in a single execution.
Q7

A program should add a bonus only when an order qualifies. A premium qualifying order earns 8 points; any other qualifying order earns 3 points. Which structure correctly implements the requirement?

AFirst check qualifies; inside that branch, check premium and choose 8 or 3 points.
BFirst check premium; add 3 points only when qualifies is false.
CUse two independent if statements so a premium order can receive both bonuses.
DAdd 8 points when qualifies is true and 3 points when it is false.
Correct answer: A. Checking qualifies first and then premium inside that branch matches the dependent decision the requirement describes.
Explanation

Nesting is useful when the second condition should be checked only after the first one is true. The bonus applies only to a qualifying order, and premium then decides which amount applies.

  • B is not correct: this checks premium first and awards points when qualifies is false, which rewards orders that do not qualify.
  • C is not correct: independent if statements are not a dependent decision, so a premium order would satisfy both and receive both bonuses.
  • D is not correct: this ignores premium entirely and still awards points when qualifies is false, so a nonqualifying order receives a bonus.
Q8

Consider the following code segment. What is printed as a result of executing the code segment?

boolean registered = true;
boolean paid = false;
String status = "Pending";

if (registered) {
    if (paid) {
        status = "Admit";
    } else {
        status = "Hold";
    }
} else {
    status = "Reject";
}
System.out.println(status);
AAdmit
BHold
CReject
DPending
Correct answer: B. registered is true and paid is false, so the inner else assigns Hold.
Explanation

The outer condition is true, so the program enters the first branch; the inner condition is false, so the inner else branch runs. That assignment replaces the earlier value of status before it is printed.

  • A is not correct: the inner if branch assigns Admit, and it runs only when paid is true.
  • C is not correct: Reject is assigned in the outer else branch, which is skipped because registered is true.
  • D is not correct: Pending is the starting value, and the nested selection always assigns a new value to status before it is printed.
Q9

Consider the following code segment. What is printed as a result of executing the code segment?

int category = 4;
String label;

if (category >= 2) {
    label = "Intermediate";
} else if (category >= 4) {
    label = "Advanced";
} else {
    label = "Beginning";
}
System.out.println(label);
AAdvanced
BIntermediate
CBeginning
DNothing is printed.
Correct answer: B. category >= 2 is true, so the first branch runs and the overlapping later condition is never checked.
Explanation

When conditions overlap, the order of checks can change the result. category is 4, which satisfies both thresholds, but only the first true branch of the chain runs. Checking the highest threshold first would classify it differently.

  • A is not correct: category >= 4 appears after a condition that is already true, so that else if branch is skipped.
  • C is not correct: the final else runs only when none of the earlier conditions is true, and the first one is true here.
  • D is not correct: the chain ends with an else, so one branch always assigns label and that value is printed.