nerdexam
Oracle

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 |…

Working with Java Data Types

Question

var i = 10; var j = 5; var z = (++i + j++) / i - 2; System.out.println(i); What is the result?

Options

  • A
  • B
  • C10
  • D11
  • E12

How the community answered

(26 responses)
  • A
    8% (2)
  • B
    4% (1)
  • D
    12% (3)
  • E
    77% (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:

StepExpressionijValue
Start-105-
++ipre-increment11511
j++post-increment1165 (old value used)
(11 + 5) / ii is now 1111616 / 11 = 1 (integer division)
1 - 2116z = -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 z with i, or miscounting increments
  • C (10) - this forgets that ++i modifies i before use (unlike i++)
  • E (12) - this would require i to be incremented twice, but there is only one ++i in 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

#increment operators#pre vs post increment#operator precedence#expression evaluation

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice