Return to site

JAVA CERTIFICATION QUESTION: When are Java objects eligible for garbage collection?

· java

This quiz is about your understanding of an object’s lifecycle, especially garbage collection.

Given the following class:

public class GCDemo {
    public static ArrayList<Object> l = new ArrayList<>();    
    public void doIt() {
        HashMap<String, Object> m = new HashMap<>();     
        Object o1 = new Object(); // line n1
        Object o2 = new Object();
        m.put("o1", o1);
        o1 = o2; // line n2
        o1 = null; // line n3
        l.add(m);
        m = null; // line n4
        System.gc();// line n5
    }
}

And given this code fragment:

GCDemo demo = new GCDemo();
demo.doIt();
demo = null;  // line n6

When does the object created at line n1 become eligible for garbage collection? Choose one.

  • A. At line n2
  • B. At line n3
  • C. At line n4
  • D. At line n5
  • E. At line n6
  • F. None of the above

 

 

 

 ·ꓞ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ