Java OCA OCP Practice Question 1972

Question

Which scenario cannot definitely be the result of compiling and running the following program?

public class Main {
   private char v;

   Main(char v) {
      this.v = v;
   }// w  ww .j a  v a2  s .co  m

   public void finalize() throws Throwable {
      System.out.print(v);
      super.finalize();
   }

   public static void main(String[] args) {
      new Main('A');
      new Main('F');
      System.gc();
   }
}

Select the one correct answer.

  • (a) The program may print AF.
  • (b) The program may print FA.
  • (c) The program may print A.
  • (d) The program may print F.
  • (e) The program may print AFA.
  • (f) The program may not print anything.


(e)

Note

It is not guaranteed if and when the garbage collection will be run, nor in which order the objects will be finalized.

However, it is guaranteed that the finalization of an object will only be run once, hence (e) cannot possible be a result from running the program.




PreviousNext

Related