Garbage collection MCQs

By: Prof. Dr. Fazal Rehman Shamil | Last updated: September 20, 2024

What is garbage collection?
A) The process of cleaning up unused memory
B) A method to allocate memory
C) A way to optimize CPU usage
D) A technique for data encryption
Answer: A) The process of cleaning up unused memory

Which of the following languages primarily uses garbage collection?
A) C
B) C++
C) Java
D) Assembly
Answer: C) Java

What is the main purpose of garbage collection?
A) To speed up the program execution
B) To free up memory automatically
C) To enhance CPU performance
D) To improve code readability
Answer: B) To free up memory automatically

Which of the following is NOT a type of garbage collection?
A) Mark-and-sweep
B) Reference counting
C) Memory pooling
D) Generational garbage collection
Answer: C) Memory pooling

In the mark-and-sweep algorithm, what is the first step?
A) Sweep the memory
B) Mark reachable objects
C) Free memory
D) Allocate new memory
Answer: B) Mark reachable objects

What does reference counting do?
A) Tracks the number of references to an object
B) Counts the number of objects in memory
C) Allocates memory for new objects
D) Marks objects for deletion
Answer: A) Tracks the number of references to an object

What happens when an object’s reference count reaches zero?
A) The object is retained in memory
B) The object is marked for garbage collection
C) The object is immediately deleted
D) The program crashes
Answer: B) The object is marked for garbage collection

Which garbage collection method involves dividing memory into generations?
A) Mark-and-sweep
B) Generational garbage collection
C) Reference counting
D) Tracing garbage collection
Answer: B) Generational garbage collection

In garbage collection, what does the term “root set” refer to?
A) The list of objects to be collected
B) The references that are accessible directly
C) The memory allocated for new objects
D) The total memory usage of a program
Answer: B) The references that are accessible directly

Which of the following can cause memory leaks?
A) Properly implemented garbage collection
B) Circular references in objects
C) Manual memory management
D) Generational garbage collection
Answer: B) Circular references in objects

What is a common drawback of garbage collection?
A) Increased performance
B) Automatic memory management
C) Non-deterministic timing of collection
D) Reduces memory consumption
Answer: C) Non-deterministic timing of collection

What does “stop-the-world” mean in the context of garbage collection?
A) The program crashes
B) All application threads are paused
C) The garbage collector runs in parallel
D) No memory is allocated
Answer: B) All application threads are paused

What is the role of a garbage collector in a programming language?
A) To compile code
B) To manage memory allocation and deallocation
C) To optimize CPU usage
D) To execute user-defined functions
Answer: B) To manage memory allocation and deallocation

Which of the following is a characteristic of the mark-and-sweep algorithm?
A) It is non-intrusive
B) It uses reference counting
C) It can lead to fragmentation
D) It requires constant memory
Answer: C) It can lead to fragmentation

How does a tracing garbage collector work?
A) It counts object references
B) It marks and sweeps objects
C) It identifies unreachable objects by traversing reachable ones
D) It allocates memory for new objects
Answer: C) It identifies unreachable objects by traversing reachable ones

What is a potential advantage of using garbage collection?
A) Manual memory management
B) Reduced programmer workload
C) Increased application complexity
D) Guaranteed performance
Answer: B) Reduced programmer workload

Which algorithm is primarily used in languages like C# for garbage collection?
A) Mark-and-sweep
B) Reference counting
C) Generational garbage collection
D) Stop-and-copy
Answer: C) Generational garbage collection

What is the main limitation of reference counting?
A) It is too complex to implement
B) It cannot handle cyclic references
C) It requires a lot of memory
D) It is slower than mark-and-sweep
Answer: B) It cannot handle cyclic references

Which of the following is a sign of a memory leak?
A) Consistent memory usage
B) Increasing memory usage over time
C) Decreased performance
D) Program crashing
Answer: B) Increasing memory usage over time

What is the purpose of garbage collection in Java?
A) To manage CPU resources
B) To optimize I/O operations
C) To reclaim memory automatically
D) To improve execution speed
Answer: C) To reclaim memory automatically

What does the term “finalization” mean in garbage collection?
A) The process of permanently deleting an object
B) The action taken before an object is collected
C) The allocation of memory for a new object
D) The marking of reachable objects
Answer: B) The action taken before an object is collected

Which of the following can improve the efficiency of garbage collection?
A) Increasing object lifetime
B) Reducing object creation
C) Frequent collection cycles
D) Allocating more memory
Answer: B) Reducing object creation

What is “heap fragmentation”?
A) A condition where free memory is scattered
B) The process of merging free memory blocks
C) Memory allocated for temporary objects
D) A type of memory leak
Answer: A) A condition where free memory is scattered

In what scenario is garbage collection typically invoked?
A) When the program is running smoothly
B) When memory usage exceeds a certain threshold
C) At the beginning of program execution
D) Every time a variable is created
Answer: B) When memory usage exceeds a certain threshold

What is the main challenge with automatic garbage collection?
A) It is too fast
B) It can introduce latency during program execution
C) It uses too much memory
D) It simplifies memory management
Answer: B) It can introduce latency during program execution

How can developers help minimize garbage collection overhead?
A) Use more global variables
B) Create fewer temporary objects
C) Use static variables
D) Increase the size of the heap
Answer: B) Create fewer temporary objects

Which method is NOT a part of garbage collection in Java?
A) System.gc()
B) Runtime.getRuntime().gc()
C) free()
D) finalize()
Answer: C) free()

What happens if garbage collection is disabled in a language that relies on it?
A) The program will not compile
B) The program will crash immediately
C) Memory leaks will occur
D) The performance will improve
Answer: C) Memory leaks will occur

What is the role of a “garbage collector” in a virtual machine?
A) To execute bytecode
B) To allocate memory
C) To manage memory cleanup
D) To compile source code
Answer: C) To manage memory cleanup

Which technique does not rely on a garbage collector?
A) Manual memory management
B) Mark-and-sweep
C) Reference counting
D) Generational garbage collection
Answer: A) Manual memory management

Which garbage collection algorithm can lead to pauses in application execution?
A) Concurrent garbage collection
B) Incremental garbage collection
C) Stop-the-world garbage collection
D) Lazy garbage collection
Answer: C) Stop-the-world garbage collection

What is “concurrent garbage collection”?
A) Collection that happens during program execution
B) Collection that runs after program execution
C) Collection that requires all threads to stop
D) Collection that is only performed on the heap
Answer: A) Collection that happens during program execution

What is a significant benefit of generational garbage collection?
A) It minimizes memory fragmentation
B) It avoids all memory leaks
C) It assumes most objects die young
D) It eliminates the need for manual memory management
Answer: C) It assumes most objects die young

In which scenario is manual garbage collection preferable?
A) When performance is critical
B) When working with low-level system programming
C) In high-level programming languages
D) When memory allocation is infrequent
Answer: A) When performance is critical

What does “tracing” in garbage collection refer to?
A) Counting references
B) Identifying reachable objects
C) Tracking memory allocation
D) Freeing unused memory
Answer: B) Identifying reachable objects

 

Data Structures MCQs

Basic Concepts

  1. Introduction to Data Structures
  2. Complexity Analysis MCQs

Linear Data Structures MCQs

  1. Arrays MCQs
  2. Linked Lists MCQs
  3. Stacks MCQs
  4. Queues MCQs

Non-Linear Data Structures MCQs

  1. Trees MCQs
  2. Heaps MCQs
  3. Graphs MCQs

Hashing MCQs MCQs

  1. Hash Tables

Sorting and Searching Algorithms MCQs 

  1. Sorting Algorithms MCQs
  2. Searching Algorithms MCQs

Miscellaneous

  1. Memory Management in data structures MCQs
  2. String Manipulation Algorithms MCQs
  1. Data Structures MCQs 1
  2. Data Structures MCQs 2
  3. Data Structures MCQs 3
  4. Data Structures MCQs 4
  5. Data Structures MCQs 5
  6. Stacks Solved MCQs
  7. Queues MCQs
  8. pointer mcqs
  9. Array MCQs