This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Heap and Garbage Collection”.
1. Which of the following has the highest memory requirement?
Explanation: JVM is the super set which contains heap, stack, objects, pointers, etc.
2. Where is a new object allocated memory?
a) Young space
b) Old space
c) Young or Old space depending on space availability
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.
3. Which of the following is a garbage collection technique?
a) Cleanup model
b) Mark and sweep model
c) Space management model
d) Sweep model
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.
4. What is -Xms and -Xmx while starting jvm?
a) Initial; Maximum memory
b) Maximum; Initial memory
c) Maximum memory
d) Initial memory
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.
5. Which exception is thrown when java is out of memory?
Explanation: The Xms flag has no default value, and Xmx typically has a default value of 256MB. A common use for these flags is when you encounter a java.lang.OutOfMemoryError.
6. How to get prints of shared object memory maps or heap memory maps for a given process?
Explanation: We can use jmap as jmap -J-d64 -heap pid.
7. What happens to the thread when garbage collection kicks off?
a) The thread continues its operation
b) Garbage collection cannot happen until the thread is running
c) The thread is paused while garbage collection runs
d) The thread and garbage collection do not interfere with each other
Explanation: The thread is paused when garbage collection runs which slows the application performance.
8. Which of the below is not a Java Profiler?
d) Eclipse Profiler
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.
9. Which of the below is not a memory leak solution?
a) Code changes
b) JVM parameter tuning
c) Process restart
d) GC parameter tuning
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will resurge again.
10. Garbage Collection can be controlled by a program?
Explanation: Garbage Collection cannot be controlled by a program.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.