Search Tutorials


Java Garbage Collection MCQ Questions and Answers | JavaInUse

Java Garbage Collection MCQ Questions and Answers

Q. What is the primary purpose of garbage collection in Java?

A. To automatically manage memory allocation and deallocation
B. To improve program performance by optimizing memory usage
C. To ensure that all objects are properly destroyed before program termination
D. To prevent memory leaks and improve memory efficiency

Q. What is a garbage collector in Java?

A. It is a low-level utility class in the Java API
B. It is a built-in feature of the Java Virtual Machine (JVM)
C. It is a separate process that runs alongside the JVM
D. It is a developer-defined method for memory management

Q. What is a garbage collection algorithm?

A. It is a set of rules followed by the garbage collector to manage memory
B. It defines the strategy used by the garbage collector to identify unused objects
C. It determines the order in which objects are allocated memory
D. It specifies how the garbage collector reclaims memory from unused objects

Q. What is a garbage collection cycle?

A. The process of allocating memory for new objects
B. The sequence of steps followed by the garbage collector
C. The time between the creation of an object and its destruction
D. The interval between two consecutive garbage collection runs

Q. What is a garbage collection policy?

A. It defines the criteria for selecting objects to be garbage collected
B. It determines the frequency of garbage collection cycles
C. It specifies the algorithm used by the garbage collector
D. It defines how memory is allocated to objects

Q. What is a garbage collection root?

A. It is a special type of object that is never garbage collected
B. It serves as a starting point for the garbage collector's traversal
C. It is a reference to an object that is always reachable
D. It is a data structure used by the garbage collector to track objects

Q. What is a garbage collection trace?

A. It is a report generated after each garbage collection cycle
B. It is a visual representation of the garbage collector's traversal
C. It is a log of all objects that were garbage collected
D. It is a record of all objects reachable from the roots

Q. What is a garbage collection pause?

A. The time taken by the garbage collector to complete a cycle
B. The interval during which the application is paused for garbage collection
C. The duration between the start of garbage collection and the resumption of application threads
D. The time taken by the garbage collector to identify all unused objects

Q. What is a garbage collection heap?

A. It is a data structure used by the garbage collector to store objects
B. It refers to the portion of memory managed by the garbage collector
C. It is a separate memory area used exclusively for garbage collection
D. It is a stack-like structure used for temporary object storage

Q. What is a garbage collection generation?

A. It is a group of objects based on their age
B. It refers to the number of times an object has been garbage collected
C. It is a category of objects based on their memory usage
D. It is a classification of objects based on their creation time





Q. What is a garbage collection survivor space?

A. It is a memory area where objects that survived garbage collection are moved
B. It is a temporary storage for objects during garbage collection
C. It is a data structure used to track objects that are frequently garbage collected
D. It is a separate heap area for objects with long-term survival

Q. What is a garbage collection finalizer?

A. It is a method invoked before an object is garbage collected
B. It is a callback mechanism to perform cleanup tasks after garbage collection
C. It is a way to mark objects as eligible for garbage collection
D. It is a thread that runs concurrently with the garbage collector

Q. What is a garbage collection safe point?

A. It is a point in the program where garbage collection can occur
B. It is a synchronization mechanism used by the garbage collector
C. It is a point where all threads are paused for garbage collection
D. It is a location in the code where objects are safe from garbage collection

Q. What is a garbage collection log?

A. It is a record of all garbage collection activities
B. It provides detailed information about garbage collection cycles
C. It is a tool for analyzing garbage collection performance
All of the above

Q. What is a garbage-first garbage collector?

A. It is a type of garbage collector that focuses on collecting the most garbage first
B. It is a garbage collector that uses a generational collection approach
C. It is a concurrent, low-pause-time garbage collector introduced in Java 8
D. It is a garbage collector that prioritizes objects based on their memory usage

Q. What is the purpose of garbage collection in Java?

A.
To automatically manage the allocation and deallocation of memory for objects, ensuring efficient memory usage.
B.
To improve the performance of Java applications by periodically cleaning up unused objects.
C.
To provide a mechanism for manual memory management, allowing developers to explicitly allocate and deallocate memory.
D.
To optimize the layout of objects in memory and reduce memory fragmentation.

Q. Which of the following statements best describes how garbage collection works in Java?

A.
The garbage collector periodically scans the heap for unused objects and reclaims their memory.
B.
The garbage collector uses reference counting to track the number of references to each object and collects objects with no references.
C.
The garbage collector employs a mark-and-sweep algorithm to identify and collect unreachable objects.
D.
All of the above.

Q. Which of the following garbage collection algorithms are commonly used in Java?

A.
Copying Collection, Mark-Sweep-Compact, Generational Collection.
B.
Mark-Compact, Reference Counting, Stop-and-Copy.
C.
Generational Collection, Mark-Sweep, Copying Collection.
D.
Reference Counting, Mark-Compact, Stop-and-Copy.

Q. How does the Java Virtual Machine (JVM) determine which objects are eligible for garbage collection?

A.
By using reference counting to track the number of references to each object.
B.
By performing a mark-and-sweep algorithm to identify objects that are no longer reachable.
C.
By maintaining a list of all allocated objects and periodically checking their liveness.
D.
By analyzing the stack and heap to identify objects that are no longer reachable.

Q. What is the difference between minor garbage collection and major garbage collection in Java?

A.
Minor GC collects young generation objects, while Major GC collects old generation objects.
B.
Minor GC occurs more frequently and collects short-lived objects, while Major GC occurs less frequently and collects long-lived objects.
C.
Minor GC uses a copying collection algorithm, while Major GC uses a mark-sweep-compact algorithm.
D.
All of the above.

Q. How can you force a garbage collection in Java?

A.
System.gc();

B.
Runtime.getRuntime().gc();

C.
Both A and B are correct.
D.
Garbage collection cannot be forced in Java.

Q. What is the purpose of finalizers in Java garbage collection?

A.
To perform cleanup actions before an object is garbage collected.
B.
To prevent an object from being garbage collected.
C.
To optimize the performance of garbage collection.
D.
To provide a mechanism for manual memory deallocation.

Q. What is the difference between weak references and soft references in Java garbage collection?

A.
Weak references are not tracked by the garbage collector, while soft references are tracked but with lower priority.
B.
Weak references are used for objects that can be garbage collected at any time, while soft references are used for objects that should be kept until memory is low.
C.
Soft references are used for objects that can be garbage collected at any time, while weak references are used for objects that should be kept until memory is low.
D.
Weak references are tracked by the garbage collector, while soft references are not.

Q. How does garbage collection handle circular references?

A.
Circular references are automatically detected and collected by the garbage collector.
B.
Circular references are not eligible for garbage collection and must be manually broken.
C.
The garbage collector uses reference counting to handle circular references.
D.
The garbage collector uses a mark-and-sweep algorithm to handle circular references.

Q. What is the "stop-the-world" event in garbage collection?

A.
It refers to the suspension of all application threads during garbage collection.
B.
It is an event triggered when a garbage collection cycle is about to start.
C.
It is an event triggered when a garbage collection cycle has completed.
D.
It refers to the resumption of application threads after garbage collection.