Java OCA OCP Practice Question 691

Question

Consider the following code:

class Super  { static String ID = "myId";  } 

class Sub extends Super{ 
   static  { System.out.print ("In Sub");  } 
} 
public class Main{ 
   public static void main (String [] args){ 
      System.out.println (Sub .ID); 
    } /*  w ww  . ja  v  a2 s .  co m*/
} 

What will be the output when class Main is run?

Select 1 option

  • A. It will print In Sub and myId.
  • B. It will print myId.
  • C. Depends on the implementation of JVM.
  • D. It will not even compile.
  • E. None of the above.


Correct Option is  : B

Note

A class or interface type T will be initialized at its first active use, which occurs if:

T is a class and a method actually declared in T is invoked.

T is a class and a constructor for class T is invoked, or U is an array with element type T, and an array of type U is created.

A non-constant field declared in T is used or assigned.

A constant field is one that is both final and static.

That is initialized with the value of a compile-time constant expression.




PreviousNext

Related