Java OCA OCP Practice Question 2569

Question

Consider the following program and predict the output:

class Base {/*  w ww  .j  a  v  a2s .  c o  m*/
     protected void finalize() {
             System.out.println("in Base.finalize");
     }
}

class Derived extends Base {
     protected void finalize() {
             System.out.println("in Derived.finalize");
     }
}

public class Main {
     public static void main(String []args) {
             Derived d = new Derived();
             d = null;
             Runtime.runFinalizersOnExit(true);
    }
}
  • a) This program prints the following: in Base.finalize.
  • b) This program prints the following: in Derived.finalize.
  • c) This program throws a CannotRunFinalizersOnExitException.
  • d) This program throws a NullPointerException.


b)

Note

The statement Runtime.runFinalizersOnExit(true); makes sure that the finalize method is invoked when the application exits.

Here, the dynamic type of the object is "Derived," so the finalize() method of the Derived class is invoked before the application exits.




PreviousNext

Related