Java OCA OCP Practice Question 1407

Question

Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected?

public class Main{ 
   private Object o; 
   void doSomething (Object s){  o = s;    } 

   public static void main (String args []){ 
      Object obj = new Object (); // 1 
      Main tc = new Main (); //2 
      tc.doSomething (obj); //3 
      obj = new Object ();    //4 
      obj = null;    //5 
      tc.doSomething (obj); //6 
    } /* www . ja va  2 s. c  o m*/
} 

Select 1 option

  • A. Line 1
  • B. Line 2
  • C. Line 3
  • D. Line 4
  • E. Line 5
  • F. Line 6


Correct Option is  : F

Note

An object can be made eligible for garbage collection by making sure there are no references pointing to that object.

You cannot directly invoke the garbage collector. You can suggest the JVM to perform garbage collection by calling System.gc ();




PreviousNext

Related