Garbage Collections Questions and Answers
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
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.
13. What is garbage collection in Java?
- Manual memory management
- Automatic memory management
- Database cleanup process
- Network optimization technique
14. Which method is called by the garbage collector before destroying an object?
- destroy()
- finalize()
- delete()
- cleanup()
15. Who performs garbage collection in Java?
- Programmer
- Operating System
- Java Virtual Machine
- Compiler
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
17. How can we make an object eligible for garbage collection?
- Using new keyword
- Calling finalize() directly
- Nullifying all references
- Using System.runGC()
18. Which of these methods is used to request JVM to run garbage collection?
- Runtime.gc()
- System.gc()
- JVM.gc()
- Memory.gc()
19. What is the purpose of the finalize() method?
- Object initialization
- Cleanup operations before destruction
- Memory allocation
- Thread synchronization
20. When does garbage collection occur in Java?
- At predictable intervals
- When programmer calls it
- When JVM decides it's needed
- Every 5 minutes