Java Programming Multiple Choice Questions - Garbage Collection
This section focuses on the "Garbage Collection" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. What allows the programmer to destroy an object x?
A. x.finalize()
B. x.delete()
C. Runtime.getRuntime().gc()
D. Only the garbage collection system can destroy an object.
View Answer
Ans : D
Explanation: When an object is no longer referenced, it may be reclaimed by the garbage collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to give the object a last chance to clean up resources that would not otherwise be released. When a class is no longer needed, it may be unloaded.
2. Which statement is true?
A. Programs will not run out of memory.
B. Objects that are referred to by other objects will never be garbage collected.
C. Objects that will never again be used are eligible for garbage collection.
D. Objects that can be reached from a live thread will never be garbage collected.
View Answer
Ans : D
Explanation: C See the note above on Islands of Isolation (An object is eligible for garbage collection when no live thread can access it - even though there might be references to it).
B is wrong. ""Never again be used"" does not mean that there are no more references to the object.
A is wrong. Even though Java applications can run out of memory there another answer supplied that is more right.
3. Which of the below is not a memory leak solution?
A. GC parameter tuning
B. Code changes
C. Process restart
D. JVM parameter tuning
View Answer
Ans : C
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will resurge again..
4. Which of the below is not a Java Profiler?
A. JVM
B. JProfiler
C. JConsole
D. Eclipse Profiler
View Answer
Ans : A
Explanation: Memory leak is like holding a strong reference to an object although it would never be needed anymore. Objects that are reachable but not live are considered memory leaks. Various tools help us to identify memory leaks.
5. Which statement is true?
A. All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
B. Objects from a class with the finalize() method overridden will never be garbage collected.
C. Objects with at least one reference will never be garbage collected.
D. Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
View Answer
Ans : D
Explanation: All objects are placed in the garbage collectible heap.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
A. jmap
B. jvmmap
C. memorymap
D. memorypath
View Answer
Ans : A
Explanation: We can use jmap as jmap -J-d64 -heap pid.
7. What is -Xms and -Xmx while starting jvm?
A. Initial; Maximum memory
B. Initial memory
C. Maximum memory
D. Maximum; Initial memory
View Answer
Ans : A
Explanation: JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory. java -Xmx2048m -Xms256m.
8. Which of the following has the highest memory requirement?
A. Stack
B. Class
C. JVM
D. Heap
View Answer
Ans : C
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
9. Where is a new object allocated memory?
A. Young space
B. JVM
C. Young or Old space depending on space availability
D. Old space
View Answer
Ans : A
Explanation: A new object is always created in young space. Once young space is full, a special young collection is run where objects which have lived long enough are moved to old space and memory is freed up in young space for new objects..
10. Which of the following is a garbage collection technique?
A. Sweep model
B. Mark and sweep model
C. Space management model
D. Cleanup model
View Answer
Ans : B
Explanation: A mark and sweep garbage collection consists of two phases, the mark phase and the sweep phase. I mark phase all the objects reachable by java threads, native handles and other root sources are marked alive and others are garbage. In sweep phase, the heap is traversed to find gaps between live objects and the gaps are marked free list used for allocating memory to new objects.
Also check :
Discussion