1Z0-808 · Question #61
Given the code fragment: int x = 100; int a = x++; int b = ++x; int c = x++; int d = (a < b) ? (a < c) ? a : (b < c) ? b : c; System.out.println(d); What is the result?
The correct answer is E. Compilation fails. Option E is correct because the outer ternary expression (a < b) ? (a < c) ? a : (b < c) ? b : c is syntactically incomplete - it has three ? operators but only two : operators, leaving the outer ternary's false-branch missing. Every ternary requires the form condition ?…
Question
Options
- A100
- B101
- C102
- D103
- ECompilation fails
How the community answered
(30 responses)- A7% (2)
- B3% (1)
- C10% (3)
- D20% (6)
- E60% (18)
Explanation
Option E is correct because the outer ternary expression (a < b) ? (a < c) ? a : (b < c) ? b : c is syntactically incomplete - it has three ? operators but only two : operators, leaving the outer ternary's false-branch missing. Every ternary requires the form condition ? trueExpr : falseExpr, and Java's compiler enforces this strictly, producing a compile-time error before any values are ever computed. The numeric distractors (100, 101, 102, 103) map to the actual runtime values of a, intermediate steps, c, and final x respectively - they're traps for students who compute the post/pre-increment arithmetic correctly but miss the structural defect in the expression. Memory tip: Before evaluating ternary logic, count the ? and : symbols - they must match one-to-one; a mismatch means the code never runs.
Topics
Community Discussion
No community discussion yet for this question.