Java OCA OCP Practice Question 519

Question

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)); 
        } 
} 
  • A. Ball
  • B. Swim
  • C. The code does not compile due to p1.
  • D. The code does not compile due to p2.


C.

Note

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.




PreviousNext

Related