Java OCA OCP Practice Question 486

Question

What is the value of 12 + 6 * 3 % (1 + 1) in Java?

  • A. 0
  • B. 12
  • C. 14
  • D. None of the above


B.

Note

The question is about operator precedence and order of operation.

The multiplication * and modulus % operators have the highest precedence, although what is inside the parentheses needs to be evaluated first.

We can reduce the expression to the following: 12 + 6 * 3 % 2.

Since multiplication * and modulus % have the same operator precedence, we evaluate them from left to right as follows:

12 + 6 * 3 % 2 -> 
12 + 18 % 2    ->
12 + 0         ->
12.  

We see that despite all of the operators on the right-hand side of the expression, the result is zero, leaving us a value of 12, making Option B the correct answer.




PreviousNext

Related