Question 971
What is printed?
int a = 7, b = 3, c = 5;
int result = 0;
if (a > b + c) {
result = 1;
} else if (a > b && a > c) {
if (b + c > a + 1) {
result = 2;
} else {
result = 3;
}
} else {
result = 4;
}
System.out.println(result);Main Solution
Step-by-step trace with a = 7, b = 3, c = 5:
- First condition:
a > b + c→7 > 3 + 5→7 > 8→ false. Skip the first block. - else-if condition:
a > b && a > c→7 > 3 && 7 > 5→true && true→ true. Enter this block. - Inner if condition:
b + c > a + 1→3 + 5 > 7 + 1→8 > 8→ false. Skip inner if, go to inner else. - Inner else:
result = 3. System.out.println(result)prints 3.
The correct answer is C) 3.
Teacher Explanation
This question tests students' ability to trace nested if/else-if/else logic with compound boolean conditions. The key insight is that the first condition (7 > 8) is false, the else-if condition (7 > 3 && 7 > 5) is true, but the inner condition (8 > 8) is false (strict inequality), so the inner else executes, setting result to 3.
Education Coach Advice
AP Exam Strategy: When tracing nested conditionals, evaluate each condition carefully one at a time. Write down the values of all variables first (a=7, b=3, c=5), then substitute into each boolean expression. Pay special attention to strict inequality (>) vs. greater-than-or-equal (≥) — a single character difference can change the entire outcome. Work top-to-bottom and stop as soon as you find the first true branch.
Traps
Common traps:
- Option B (2): Students may incorrectly evaluate
b + c > a + 1as8 > 8being true (confusing > with ≥). Since it is strict greater-than, 8 is NOT greater than 8, so result = 2 is never reached. - Option A (1): Students may miscalculate
b + cas something other than 8, making the first condition seem true. - Option D (4): Students may fail to recognize that the else-if condition is true, falling through to the final else.
Answer images

