Java OCA OCP Practice Question 1491

Question

Which letters will be printed when the following program is run?

public class Main{ 
  public static void main (String args []){ 
    MyClass2 b = new MyClass3 (); 
    MyClass1 a = b; //from   ww w . j av a 2  s  .c  o  m
    if  (a instanceof MyClass1) System.out.println ("MyClass1"); 
    if  (a instanceof MyClass2) System.out.println ("MyClass2"); 
    if  (a instanceof MyClass3) System.out.println ("MyClass3"); 
    if  (a instanceof MyClass4) System.out.println ("MyClass4"); 
   } 
} 
class MyClass1  {  } 
class MyClass2 extends MyClass1  {  } 
class MyClass3 extends MyClass2  {  } 
class MyClass4 extends MyClass3  {  } 

Select 3 options

  • A. MyClass1 will be printed.
  • B. MyClass2 will be printed.
  • C. MyClass3 will be printed.
  • D. MyClass4 will be printed.


Correct Options are  : A B C

Note

The program will print MyClass1, MyClass2 and MyClass4 when run.

The object denoted by reference a is of type MyClass3.

The object is also an instance of MyClass1 and MyClass2, since MyClass3 is a subclass of MyClass2 and MyClass2 is a subclass of MyClass1.

The object is not an instance of MyClass4.




PreviousNext

Related