Java OCA OCP Practice Question 2887

Question

Given:

3. class Shape {  
4.   void print() { System.out.print("4-beat "); }  
5. }  // w ww  . j  ava 2  s  .  co  m
6. public class Main extends Shape {  
7.   public static void main(String[] args) {  
8.     new Main().go();  
9.     new Shape().print();  
10.   }  
11.   void go() {  
12.     Shape h1 = new Main();      
13.     h1.print();  
14.     Main v = (Main) h1;  
15.     v.print();      
16.   }  
17.   void print() { System.out.print("pacey "); }  
18. } 

What is the result? (Choose all that apply.)

A.   4-beat pacey pacey
B.   pacey pacey 4-beat
C.   4-beat 4-beat 4-beat
D.   4-beat pacey 4-beat
E.   pacey, followed by an exception
F.   4-beat, followed by an exception


B is correct.

Note

The print() method's first two invocations come from the go() method, and the object type determines which (overridden) method is used at runtime.

A, C, and D are incorrect based on the above.

E and F are incorrect.

The casts are legal.




PreviousNext

Related