1Z0-819 · Question #166
var i = 10; var j = 5; var z = (++i + j++) / i - 2; System.out.println(i); What is the result?
The correct answer is E. 12. There appears to be an error in the provided answer key - D (11) is actually the correct answer, not E (12). Here's the step-by-step trace: | Step | Expression | i | j | Value | |------|-----------|-----|-----|-------| | Start | - | 10 | 5 | - | | ++i | pre-increment | 11 | 5 |…
Question
Options
- A
- B
- C10
- D11
- E12
How the community answered
(26 responses)- A8% (2)
- B4% (1)
- D12% (3)
- E77% (20)
Explanation
There appears to be an error in the provided answer key - D (11) is actually the correct answer, not E (12).
Here's the step-by-step trace:
| Step | Expression | i | j | Value |
|---|---|---|---|---|
| Start | - | 10 | 5 | - |
++i | pre-increment | 11 | 5 | 11 |
j++ | post-increment | 11 | 6 | 5 (old value used) |
(11 + 5) / i | i is now 11 | 11 | 6 | 16 / 11 = 1 (integer division) |
1 - 2 | 11 | 6 | z = -1 |
System.out.println(i) prints 11 because ++i (pre-increment) raised i from 10 to 11, and nothing else modifies i after that.
Why the distractors fail:
- A (5) and B (3) - these would only arise from confusing
zwithi, or miscounting increments - C (10) - this forgets that
++imodifiesibefore use (unlikei++) - E (12) - this would require
ito be incremented twice, but there is only one++iin the expression
Memory tip: The prefix ++i vs. postfix i++ rule: prefix = increment before using the value (and thus modifies i immediately), postfix = use the value first, increment after. In ++i + j++, only i is changed before the expression evaluates.
Topics
Community Discussion
No community discussion yet for this question.