Garbage Collections Questions and Answers

Practice Mode
Showing 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?
  • A 0
  • B 1
  • C 2
  • D 3
Answer: Option C
Q12
What allows the programmer to destroy an object x?
  • A x.delete()
  • B x.finalize()
  • C Runtime.getRuntime().gc()
  • D Only the garbage collection system can destroy an object.
Answer: Option D
Q13
What is garbage collection in Java?
  • A Manual memory management
  • B Automatic memory management
  • C Database cleanup process
  • D Network optimization technique
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?
  • A destroy()
  • B finalize()
  • C delete()
  • D cleanup()
Answer: Option B
Explanation: finalize() method is called by garbage collector before object destruction.
Q15
Who performs garbage collection in Java?
  • A Programmer
  • B Operating System
  • C Java Virtual Machine
  • D Compiler
Answer: Option C
Explanation: JVM automatically handles garbage collection without programmer intervention.
Q16
Which of the following is NOT eligible for garbage collection?
  • A Null referenced objects
  • B Objects out of scope
  • C Objects with active references
  • D Island of isolation objects
Answer: Option C
Explanation: Objects with active references cannot be garbage collected.
Q17
How can we make an object eligible for garbage collection?
  • A Using new keyword
  • B Calling finalize() directly
  • C Nullifying all references
  • D Using System.runGC()
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?
  • A Runtime.gc()
  • B System.gc()
  • C JVM.gc()
  • D Memory.gc()
Answer: Option B
Explanation: System.gc() suggests JVM to run garbage collection.
Q19
What is the purpose of the finalize() method?
  • A Object initialization
  • B Cleanup operations before destruction
  • C Memory allocation
  • D Thread synchronization
Answer: Option B
Explanation: finalize() provides cleanup before garbage collection.
Q20
When does garbage collection occur in Java?
  • A At predictable intervals
  • B When programmer calls it
  • C When JVM decides it's needed
  • D Every 5 minutes
Answer: Option C
Explanation: Garbage collection timing is unpredictable and JVM-dependent.
Questions and Answers for Competitive Exams Various Entrance Test