What is the output of the following application?
public class Baby { public static String play(int toy, int age) { final String game; if(toy<2) game = age > 1 ? 1 : 10; // p1 else /*from w w w . j a v a 2 s . c om*/ game = age > 3 ? "Ball" : "Swim"; // p2 return game; } public static void main(String[] variables) { System.out.print(play(5,2)); } }
C.
The type of the value returned by a ternary operation is determined by the expressions on the right-hand side.
On line p1, the expressions are of type int, but the assignment is to the variable game, of type String.
Since the assignment is invalid, the code does not compile, and Option C is correct.