nerdexam
Oracle

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.

Using Operators and Decision Constructs

Question

Given the code fragment:

What could expression1 and expression2 be, respectively, in order to produce output -8, 16?

Exhibit

1Z0-803 question #205 exhibit

Options

  • A
    • +a, - -b
  • B
    • +a, b- -
  • CA+ +, - - b
  • DA + +, b - -

How the community answered

(35 responses)
  • A
    17% (6)
  • B
    74% (26)
  • C
    3% (1)
  • D
    6% (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+ +a, - -b

`+ +a` correctly evaluates to -8, but `- -b` is a pre-decrement that would evaluate to 15, not 16.

B+ +a, b- -Correct

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.

CA+ +, - - b

`a+ +` is a post-increment operator that would evaluate to the original value of `a` (-9), not -8.

DA + +, b - -

`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

#increment operators#decrement operators#operator precedence

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice