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);
46714member 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 changepointsfrom its starting value. - C is not correct: this stops after
points += 3;and ignores the inner selection, but the innerelsebranch still runs and subtracts1. - D is not correct:
points *= 2;belongs to the innerifbranch, which is skipped becauseearlyisfalsewhen that condition is evaluated.