Java OCA OCP Practice Question 2582

Question

Given:

2. class Shape {  
3.   static String os = "";  
4.   void m() { os += "super "; }  
5. }  /*from w w w.  j a v a 2  s.c om*/
6. public class Main extends Shape {    
7.   public static void main(String[] args) {  
8.     new Main().go();  
9.   }  
10.   void go() {  
11.     Shape s = new Main();     
12.     Main c = (Main)s;     
13.     // insert code here  
14.   }  
15.   void m() { os += "cool "; }  
16. } 

If the rest of the code compiles, which line(s) of code, inserted independently at line 13, compile? (Choose all that apply.).

  • A. c.m();
  • B. s.m();
  • C. this.m();
  • D. super.m();
  • E. c.super.m();
  • F. s.super.m();
  • G. this.super.m();
  • H. There are other errors in the code.


A, B, C, and D are correct.

Note

A, B, and C will invoke Main's version of m(), and D will invoke Shape's version of m().

E, F, and G are all illegal syntax to try to invoke Shape's version of m().

H is incorrect because the rest of the code is legal.




PreviousNext

Related