Java OCA OCP Practice Question 471

Question

What is the output of the following application?


public class MyClass { 
        public static String travel(int distance) { 
           return distance<1000 ? "train" : 10; 
        } 
        public static void main(String[] answer) { 
           System.out.print(travel(500)); 
        } 
} 
  • A. train
  • B. 10
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


C.

Note

Ternary operations require both right-hand expressions to be of compatible data types.

In the code above, the first right-hand expression of the outer ternary operation is of type String, while the second right-hand expression is of type int.

Since these data types are incompatible, the code does not compile, and Option C is the correct answer.




PreviousNext

Related