Java Questions & Answers – Heap and Garbage Collection

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?
a) Heap
b) Stack
c) JVM
d) Class
View Answer

Answer: c
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
d) JVM
View Answer

Answer: 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.

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
View Answer

Answer: 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.
advertisement
advertisement

4. What is -Xms and -Xmx while starting jvm?
a) Initial; Maximum memory
b) Maximum; Initial memory
c) Maximum memory
d) Initial memory
View Answer

Answer: 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.

5. Which exception is thrown when java is out of memory?
a) MemoryFullException
b) MemoryOutOfBoundsException
c) OutOfMemoryError
d) MemoryError
View Answer

Answer: c
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?
a) jmap
b) memorymap
c) memorypath
d) jvmmap
View Answer

Answer: a
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
View Answer

Answer: c
Explanation: The thread is paused when garbage collection runs which slows the application performance.
advertisement

8. Which of the below is not a Java Profiler?
a) JVM
b) JConsole
c) JProfiler
d) Eclipse Profiler
View Answer

Answer: 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.

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
View Answer

Answer: c
Explanation: Process restart is not a permanent fix to memory leak problem. The problem will resurge again.
advertisement

10. Garbage Collection can be controlled by a program?
a) True
b) False
View Answer

Answer: b
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.