Java OCA OCP Practice Question 366

Question

What is the value of myField after the execution of the following code snippet?

long myField = 5 >= 5 ? 1+2 : 1*1; 
if(++myField < 4) 
        myField += 1; 
  • A. 3
  • B. 4
  • C. 5
  • D. The answer cannot be determined until runtime.


B.

Note

The initial assignment of myField follows the first branch of the ternary expression.

Since 5 >= 5 evaluates to true, a value of 3 is assigned to myField.

In the next line, the pre-increment operator increments the value of myField to 4 and returns a value of 4 to the expression. Since 4 < 4 evaluates to false, the if-then block is skipped.

This leaves the value of myField as 4, making Option B the correct answer.




PreviousNext

Related