Java OCA OCP Practice Question 319

Question

What is the value of tip after executing the following code snippet?

int myField = 5; 
int tip = 2; 
int total = myField + (myField>6 ? ++tip : --tip); 
  • A. 1
  • B. 2
  • C. 3
  • D. 6


A.

Note

In ternary expressions, only one of the two right-most expressions are evaluated.

Since myField>6 is false, --tip is evaluated and ++tip is skipped.

The result is that tip is changed from 2 to 1, making Option A the correct answer.

The value of total is 6, since the pre-increment operator was used on tip, although you did not need to know this to solve the question.




PreviousNext

Related