Garbage Collections Questions and Answers
Practice ModeShowing 10 of 32 questions
Q11
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}
after line 11 runs, how many objects are eligible for garbage collection?
Answer: Option C
Q12
What allows the programmer to destroy an object x?
Answer: Option D
Q13
What is garbage collection in Java?
Answer: Option B
Explanation: Garbage collection automatically manages memory by reclaiming unused objects.
Q14
Which method is called by the garbage collector before destroying an object?
Answer: Option B
Explanation: finalize() method is called by garbage collector before object destruction.
Q15
Who performs garbage collection in Java?
Answer: Option C
Explanation: JVM automatically handles garbage collection without programmer intervention.
Q16
Which of the following is NOT eligible for garbage collection?
Answer: Option C
Explanation: Objects with active references cannot be garbage collected.
Q17
How can we make an object eligible for garbage collection?
Answer: Option C
Explanation: Nullifying references makes objects eligible for GC.
Q18
Which of these methods is used to request JVM to run garbage collection?
Answer: Option B
Explanation: System.gc() suggests JVM to run garbage collection.
Q19
What is the purpose of the finalize() method?
Answer: Option B
Explanation: finalize() provides cleanup before garbage collection.
Q20
When does garbage collection occur in Java?
Answer: Option C
Explanation: Garbage collection timing is unpredictable and JVM-dependent.