1Z0-803 · Question #205
Given the code fragment: What could expression1 and expression2 be, respectively, in order to produce output -8, 16?
The correct answer is B. + +a, b- -. This question tests the understanding of Java's pre-increment/decrement and post-increment/decrement operators and their evaluation order to produce specific output values.
Question
Given the code fragment:
What could expression1 and expression2 be, respectively, in order to produce output -8, 16?
Exhibit
Options
- A
- +a, - -b
- B
- +a, b- -
- CA+ +, - - b
- DA + +, b - -
How the community answered
(35 responses)- A17% (6)
- B74% (26)
- C3% (1)
- D6% (2)
Why each option
This question tests the understanding of Java's pre-increment/decrement and post-increment/decrement operators and their evaluation order to produce specific output values.
`+ +a` correctly evaluates to -8, but `- -b` is a pre-decrement that would evaluate to 15, not 16.
For expression1, `+ +a` is a pre-increment operator, which first increments `a` from -9 to -8, then evaluates to the new value -8. For expression2, `b- -` is a post-decrement operator, which evaluates to the original value of `b` (16) before decrementing `b` to 15, thus producing the output 16.
`a+ +` is a post-increment operator that would evaluate to the original value of `a` (-9), not -8.
`a+ +` is a post-increment operator that would evaluate to the original value of `a` (-9), not -8.
Concept tested: Java pre-increment/decrement and post-increment/decrement operators
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Topics
Community Discussion
No community discussion yet for this question.
