Java OCA OCP Practice Question 500

Question

What is the output of the following application?


public class MyClass { 
        public static void main(String[] arguments) { 
           int turtle = 10 * (2 + (3 + 2) / 5); 
           int hare = turtle < 5 ? 10 : 25; 
           System.out.print(turtle < hare ? "Hare wins!" : "Turtle wins!"); 
        } 
} 
  • A. Hare wins!
  • B. Turtle wins!
  • C. The code does not compile.
  • D. The code compiles but throws a division by zero error at runtime.


B.

Note

The code compiles and runs without issue, making Options C and D incorrect.

The key here is understanding operator precedence and applying the parentheses to override precedence correctly.

The first expression is evaluated as follows:

10 * (2 + (3 + 2) / 5) -> 10 * (2 + 5 / 5) -> 10 * (2 + 1) -> 10 * 3, 

with a final value of 30 for turtle.

Since turtle is not less than 5, a value of 25 is assigned to hare.

Since turtle is not less than hare, the last expression evaluates to Turtle wins!, which is outputted to the console, making Option B the correct answer.




PreviousNext

Related