Garbage Collection and Memory Management # MCQs Practice set

Q.1 What is the main purpose of garbage collection in programming languages?

To allocate memory to variables
To automatically reclaim unused memory
To speed up program execution
To manage CPU scheduling
Explanation - Garbage collection automatically identifies and frees memory that is no longer in use, preventing memory leaks.
Correct answer is: To automatically reclaim unused memory

Q.2 Which of the following is a common garbage collection technique?

Reference counting
Stack allocation
Loop unrolling
Binary search
Explanation - Reference counting keeps track of the number of references to each object. When the count reaches zero, the object can be safely deallocated.
Correct answer is: Reference counting

Q.3 In memory management, what is 'heap fragmentation'?

Memory blocks are too large
Memory is allocated contiguously
Free memory is split into small unusable blocks
Memory is allocated on the stack instead of the heap
Explanation - Heap fragmentation occurs when free memory is divided into small non-contiguous blocks that cannot be used for new allocations efficiently.
Correct answer is: Free memory is split into small unusable blocks

Q.4 Which of the following is true about reference counting garbage collection?

It can automatically collect circular references
It requires manual intervention
It cannot handle cyclic references
It uses a stack-based approach
Explanation - Reference counting fails to collect objects that reference each other in a cycle, as their reference count never drops to zero.
Correct answer is: It cannot handle cyclic references

Q.5 Mark-and-sweep garbage collection works in which two phases?

Allocate and deallocate
Mark and sweep
Count and reclaim
Push and pop
Explanation - Mark-and-sweep first marks all reachable objects, then sweeps the memory to reclaim unmarked (unreachable) objects.
Correct answer is: Mark and sweep

Q.6 Which memory management strategy divides memory into fixed-size blocks?

Paging
Segmentation
Garbage collection
Stack allocation
Explanation - Paging divides memory into fixed-size pages to efficiently manage memory allocation and avoid fragmentation.
Correct answer is: Paging

Q.7 What is a key disadvantage of using a stop-the-world garbage collector?

Memory leaks
CPU-bound computation slows
Program execution halts during collection
Cannot allocate heap memory
Explanation - Stop-the-world collectors pause the program entirely to perform garbage collection, which may cause latency issues in real-time systems.
Correct answer is: Program execution halts during collection

Q.8 In generational garbage collection, objects are categorized based on what?

Memory address
Size of object
Age of object
Type of object
Explanation - Generational garbage collection assumes most objects die young, so it categorizes objects into generations based on their lifespan to optimize collection.
Correct answer is: Age of object

Q.9 Which of the following is NOT a benefit of garbage collection?

Reduces memory leaks
Automatically frees unused memory
Eliminates need for careful memory allocation
Always increases execution speed
Explanation - Garbage collection can introduce runtime overhead and may slow down execution, though it helps prevent memory leaks.
Correct answer is: Always increases execution speed

Q.10 What is 'lazy sweep' in garbage collection?

Reclaiming memory immediately
Deferring memory reclamation until needed
Collecting memory in the background thread
Allocating memory on demand
Explanation - Lazy sweep delays reclaiming unused memory until the program requests it, which can help improve program performance.
Correct answer is: Deferring memory reclamation until needed

Q.11 Which type of memory is usually managed by garbage collection?

Stack memory
Heap memory
Registers
Cache memory
Explanation - Heap memory is dynamically allocated at runtime and often requires garbage collection, whereas stack memory is automatically managed.
Correct answer is: Heap memory

Q.12 What does the term 'memory leak' refer to?

Memory that is deallocated too early
Memory that is allocated but never freed
Memory that is fragmented
Memory that is on the stack
Explanation - A memory leak occurs when a program allocates memory but loses all references to it without freeing it, leading to wasted memory.
Correct answer is: Memory that is allocated but never freed

Q.13 Which algorithm traverses all objects starting from root references?

Mark-and-sweep
Reference counting
Stack allocation
Paging
Explanation - Mark-and-sweep begins from root objects and marks all reachable objects before reclaiming unmarked ones.
Correct answer is: Mark-and-sweep

Q.14 What is the main advantage of incremental garbage collection?

It collects all memory at once
It spreads collection work over time to reduce pauses
It uses no CPU resources
It requires no memory allocation
Explanation - Incremental GC breaks the collection process into smaller steps to reduce pause times, improving responsiveness.
Correct answer is: It spreads collection work over time to reduce pauses

Q.15 Which of the following is an example of automatic memory management?

malloc() in C
new operator in Java
free() in C
delete in C++
Explanation - In Java, memory allocated with 'new' is automatically managed by the garbage collector, unlike manual allocation in C/C++.
Correct answer is: new operator in Java

Q.16 What is the role of a 'finalizer' in garbage collected languages?

To allocate memory for objects
To execute cleanup code before object reclamation
To increment reference counts
To manage stack frames
Explanation - A finalizer is a special method that allows an object to clean up resources before the memory is reclaimed by the garbage collector.
Correct answer is: To execute cleanup code before object reclamation

Q.17 What is the main drawback of reference counting?

High CPU usage
Cannot handle cyclic references
Requires manual deallocation
Cannot allocate memory
Explanation - Reference counting cannot detect cycles where objects reference each other, which can lead to memory leaks.
Correct answer is: Cannot handle cyclic references

Q.18 What does 'compaction' in memory management refer to?

Merging free memory blocks to reduce fragmentation
Allocating new memory
Marking reachable objects
Counting references
Explanation - Compaction moves objects to reduce fragmentation and combine free memory blocks for future allocations.
Correct answer is: Merging free memory blocks to reduce fragmentation

Q.19 Which garbage collection strategy is typically used in real-time systems?

Stop-the-world GC
Incremental GC
Reference counting
Mark-and-sweep
Explanation - Incremental garbage collection spreads collection work over time to minimize pause durations, making it suitable for real-time systems.
Correct answer is: Incremental GC

Q.20 In mark-and-compact GC, what happens after marking reachable objects?

All memory is freed
Unmarked objects are compacted and memory is defragmented
Reference counts are updated
Memory allocation stops
Explanation - Mark-and-compact first marks reachable objects, then moves them together to compact memory and reduce fragmentation.
Correct answer is: Unmarked objects are compacted and memory is defragmented

Q.21 Which of the following is a common sign of memory leak?

Program crashes immediately
Memory usage keeps growing over time
CPU usage drops
Objects are immediately deallocated
Explanation - A memory leak gradually increases memory usage because allocated memory is never properly released.
Correct answer is: Memory usage keeps growing over time

Q.22 Which of these languages typically uses garbage collection?

C
C++
Java
Assembly
Explanation - Java uses automatic garbage collection to manage heap memory, whereas C/C++ require manual memory management.
Correct answer is: Java

Q.23 What is a 'root set' in garbage collection?

All allocated heap memory
All global and local references from which objects are reachable
Memory blocks to be freed
Temporary stack variables
Explanation - The root set contains references from which the GC begins marking reachable objects.
Correct answer is: All global and local references from which objects are reachable

Q.24 Which type of garbage collection reduces pause times by performing collection concurrently with the program?

Stop-the-world GC
Concurrent GC
Reference counting
Mark-and-sweep
Explanation - Concurrent GC performs garbage collection in parallel with program execution to reduce pause times.
Correct answer is: Concurrent GC