Java OCA OCP Practice Question 2786

Question

Given:

2. class MyClass {  
3.   static String os = "";  
4.   void print() { os += "super "; }  
5. }  /* w  w  w.ja v  a 2s .c o  m*/
6. public class Main extends MyClass {    
7.   public static void main(String[] args) { new Main().go(); }  
8.   void go() {  
9.      MyClass s = new Main();     
10.     Main p = (Main)s;     
11.     p.print();  
12.     s.print();  
13.     p.write();  
14.     s.write();   
15.     System.out.println(os);   
16.   }  
17.   void print() { os += "over "; }  
18.   void write() { os += "write "; }  
19. } 

What is the result?

  • A. Compilation fails.
  • B. over over write write
  • C. over super write write
  • D. super super write write
  • E. An exception is thrown at runtime.


A is correct.

Note

Compilation fails at line 14 because the MyClass class reference variable doesn't know about the write() method.




PreviousNext

Related