1. What is the primary purpose of garbage collection in a compiler?
A) To optimize the runtime performance of the program
B) To reclaim memory that is no longer in use by the program
C) To translate source code into machine code
D) To check for syntax errors in the code
Answer: B) To reclaim memory that is no longer in use by the program
2. Which of the following is a common garbage collection technique used in compilers?
A) Reference counting
B) Register allocation
C) Dead code elimination
D) Constant folding
Answer: A) Reference counting
3. Which of the following is a disadvantage of using garbage collection in compilers?
A) It increases the program’s memory usage
B) It can introduce runtime overhead due to the collection process
C) It leads to errors in the compiled code
D) It reduces the execution speed of the code
Answer: B) It can introduce runtime overhead due to the collection process
4. In which phase does the garbage collector work to identify and reclaim unused memory?
A) During lexical analysis
B) During semantic analysis
C) During runtime execution
D) During code generation
Answer: C) During runtime execution
5. What is the main difference between “mark-and-sweep” and “reference counting” garbage collection methods?
A) Mark-and-sweep requires manual memory management, while reference counting does not
B) Mark-and-sweep can collect garbage from cyclic dependencies, while reference counting cannot
C) Reference counting is slower than mark-and-sweep
D) Mark-and-sweep is only used for static memory allocation, while reference counting is dynamic
Answer: B) Mark-and-sweep can collect garbage from cyclic dependencies, while reference counting cannot
6. Which garbage collection method works by moving objects around in memory to avoid fragmentation?
A) Mark-and-sweep
B) Reference counting
C) Stop-and-copy
D) Generational garbage collection
Answer: C) Stop-and-copy
7. What is a significant advantage of generational garbage collection?
A) It guarantees zero memory fragmentation
B) It is faster at reclaiming memory from long-lived objects
C) It is more efficient for programs that allocate and deallocate a large number of objects frequently
D) It requires fewer CPU cycles compared to other methods
Answer: C) It is more efficient for programs that allocate and deallocate a large number of objects frequently
8. In the context of garbage collection, what is the term “stop-the-world” referring to?
A) A technique for reducing memory leaks
B) A point where the program halts execution to allow garbage collection to happen
C) The phase where dead code is eliminated
D) A method to prevent fragmentation
Answer: B) A point where the program halts execution to allow garbage collection to happen
9. Which of the following statements is true about “mark-and-sweep” garbage collection?
A) It collects garbage by counting references and deleting unreachable objects
B) It requires objects to be moved to a new location in memory
C) It involves two phases: marking live objects and sweeping to collect garbage
D) It is primarily used to optimize loops in compiled code
Answer: C) It involves two phases: marking live objects and sweeping to collect garbage
10. What is the role of the garbage collector in a runtime environment?
A) It performs lexical and syntax analysis
B) It reclaims memory that is no longer in use, preventing memory leaks
C) It improves the speed of the compiler
D) It generates the final machine code
Answer: B) It reclaims memory that is no longer in use, preventing memory leaks
11. What is the disadvantage of using reference counting as a garbage collection technique?
A) It requires high computational power during runtime
B) It fails to handle cyclic references properly
C) It increases memory fragmentation
D) It performs better than mark-and-sweep
Answer: B) It fails to handle cyclic references properly
12. Which of the following garbage collection techniques can potentially reduce memory fragmentation?
A) Reference counting
B) Mark-and-sweep
C) Stop-and-copy
D) Generational collection
Answer: C) Stop-and-copy
13. Which of the following is a potential drawback of using garbage collection in compiled programs?
A) It may require significant CPU resources, leading to overhead
B) It prevents programs from freeing up memory
C) It eliminates all potential memory leaks
D) It reduces the execution time of the program
Answer: A) It may require significant CPU resources, leading to overhead
14. Which of the following is a goal of incremental garbage collection?
A) To ensure garbage collection happens during compile-time
B) To minimize the pause time during garbage collection by breaking it into small steps
C) To prevent the compiler from generating dead code
D) To only collect garbage during the program’s initialization phase
Answer: B) To minimize the pause time during garbage collection by breaking it into small steps
15. Which of the following can help improve the efficiency of garbage collection in a system?
A) Using reference counting
B) Generational garbage collection
C) Using manual memory management
D) Enabling multiple garbage collectors at the same time
Answer: B) Generational garbage collection