Java OCA OCP Practice Question 468

Question

Consider the following code snippet:

public class Test{ 
  void test (){ /*from   w  w  w.  jav a 2s  .  c om*/
      MyClass obj = new MyClass (); 
      obj.name = "jack"; 
      // 1 insert code here 
   } 
} 

//In MyClass.java 
public class MyClass{ 
  int value; 
  String name; 
} 

What can be inserted at // 1, which will make the object referred to by obj eligible for garbage collection?

Select 1 option

  • A. obj.destroy ();
  • B. Runtime.getRuntime().gc ();
  • C. obj = null;
  • D. obj.finalize ()
  • E. obj.name = null; as well as obj = null;



Correct Option is  : C

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();

Nothing can ensure that an object will definitely be destroyed by the garbage collector.

You can at most make an object eligible for GC by making sure that there are no references to it.




PreviousNext

Related