Java OCA OCP Practice Question 778

Question

Consider the following code:

class MyClass {/*from w w  w  . jav a 2s.  c o  m*/
}

public class Main {
   MyClass getMyClassObject() {
      MyClass mc = new MyClass(); // 1
      return mc; // 2
   }

   public static void main(String[] args) {
      Main tc = new Main(); // 3
      MyClass x = tc.getMyClassObject(); // 4
      System.out.println("got myclass object"); // 5
      x = new MyClass(); // 6
      System.out.println("done"); // 7
   }
}

After what line the MyClass object created at line 1 will be eligible for garbage collection?

Select 1 option

  • A. 2
  • B. 5
  • C. 6
  • D. 7
  • E. Never till the program ends.


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




PreviousNext

Related