Java OCA OCP Practice Question 147

Question

Given:

3. class Animal { 
4.   String name = "furry "; 
5.   String talk() { return "generic noise"; } 
6. } /*from   w w w . j  a v  a  2  s .  c om*/
7. class Dog extends Animal { 
8.   String name = "stripes "; 
9.   String talk() { return "bray"; } 
10. } 
11. public class Main { 
12.   public static void main(String[] args) { new Main().go(); } 
13.   void go() { 
14.     Animal m = new Dog(); 
15.     System.out.println(m.name + m.talk()); 
16.   } 
17. } 

What is the result?

  • A. furry bray
  • B. stripes bray
  • C. furry generic noise
  • D. stripes generic noise
  • E. Compilation fails
  • F. An exception is thrown at runtime


A is correct.

Note

Polymorphism is only for instance methods, not instance variables.




PreviousNext

Related