Q2. How can you force garbage collection of an object?
A. Garbage collection cannot be forced.
B. Call System.gc().
C. Call System.gc() passing in a reference to the object to be garbage collected.
D. Call Runtime.gc().
E. Set all references to the object to new values(null, for example).
Correct Answer: A
Explanation:
A is correct. Garbage collection cannot be forced.
B and D are not correct as calling System.gc() or Runtime.gc() is not 100 percent
reliable, since the garbage collection thread might defer to a thread of higher
priority.
C is incorrect as gc method does not take argument.
E makes object to be garbage collected while next time it refers.
Disclaimer: Simulationexams.com is not affiliated with any certification vendor, and Sim-Ex™ Practice Exams are written independently by SimulationExams.com and not affiliated or authorized by respective certification providers. Sim-Ex™ is a trade mark of SimulationExams.com or entity representing Simulationexams.com.SCJP® is a trademark of Oracle™.