Java OCA OCP Practice Question 612

Question

Given the following code:

class MyClass  {  } 
class Main { //from   ww  w .  j  a  v a 2  s .  co  m
   private MyClass m = new MyClass (); 
   public void m (MyClass pMyClass){ 
      pMyClass = null; 
    } 
   public void m2 (){ 
      m (m); 
    } 
   public static void main (String [] args){ 
      Main n = new Main (); 
      n.m2 (); 
    } 
} 

Which of the following statements are correct?

Select 1 option

  • A. There are no instances of MyClass to be garbage collected till the program ends.
  • B. A call to n.m2 () marks the private instance of MyClass for garbage collection.
  • C. Setting pMyClass = null; in m (), marks the private instance of MyClass for garbage collection.
  • D. private members of a class become eligible for garbage collection only when the instance of the class itself becomes eligible for garbage collection.


Correct Option is  : A

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