Java OCA OCP Practice Question 140

Question

Given:

3. public class MyBird extends Bird { 
4.   public static String sing() { return "fa"; } 
5.   public static void main(String[] args) { 
6.     MyBird t = new MyBird(); 
7.     Bird s = new MyBird(); 
8.     System.out.println(t.sing() + " " + s.sing()); 
9.   } 
10. } 
11. class Bird { public static String sing() { return "la"; } } 

What is the result?

  • A. fa fa
  • B. fa la
  • C. la la
  • D. Compilation fails
  • E. An exception is thrown at runtime


B is correct.

Note

The code is correct, but polymorphism doesn't apply to static methods.




PreviousNext

Related