Garbage Collections Questions and Answers

Take Exam

Garbage Collection in Java is a critical concept for memory management and performance optimization. This topic includes Java programming questions and answers related to JVM memory models, automatic garbage collection, and reference handling. Practicing garbage collection questions with answers helps learners prepare for TCS, Accenture, Infosys, and other software company interviews. A solid understanding of garbage collection enables developers to write efficient Java programs, reducing memory leaks and improving application stability.

Garbage Collections

Showing 10 of 32 questions

11. 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?

  • 0
  • 1
  • 2
  • 3
Show Answer Report

12. What allows the programmer to destroy an object x?

  • x.delete()
  • x.finalize()
  • Runtime.getRuntime().gc()
  • Only the garbage collection system can destroy an object.
Show Answer Report

13. What is garbage collection in Java?

  • Manual memory management
  • Automatic memory management
  • Database cleanup process
  • Network optimization technique
Show Answer Report

14. Which method is called by the garbage collector before destroying an object?

  • destroy()
  • finalize()
  • delete()
  • cleanup()
Show Answer Report

15. Who performs garbage collection in Java?

  • Programmer
  • Operating System
  • Java Virtual Machine
  • Compiler
Show Answer Report

16. Which of the following is NOT eligible for garbage collection?

  • Null referenced objects
  • Objects out of scope
  • Objects with active references
  • Island of isolation objects
Show Answer Report

17. How can we make an object eligible for garbage collection?

  • Using new keyword
  • Calling finalize() directly
  • Nullifying all references
  • Using System.runGC()
Show Answer Report

18. Which of these methods is used to request JVM to run garbage collection?

  • Runtime.gc()
  • System.gc()
  • JVM.gc()
  • Memory.gc()
Show Answer Report

19. What is the purpose of the finalize() method?

  • Object initialization
  • Cleanup operations before destruction
  • Memory allocation
  • Thread synchronization
Show Answer Report

20. When does garbage collection occur in Java?

  • At predictable intervals
  • When programmer calls it
  • When JVM decides it's needed
  • Every 5 minutes
Show Answer Report
Questions and Answers for Competitive Exams Various Entrance Test