Dynamic memory allocation and garbage collection(MCQs)

What function is used in C++ to allocate memory dynamically?
a) malloc
b) calloc
c) new
d) alloc
Answer: c) new

Which function is used to deallocate memory allocated by new in C++?
a) delete
b) free
c) release
d) remove
Answer: a) delete

What does new[] do in C++?
a) Allocates memory for a single object
b) Allocates memory for an array of objects
c) Deallocates memory for an array of objects
d) Allocates memory for a variable
Answer: b) Allocates memory for an array of objects

What does delete[] do in C++?
a) Deallocates memory for a single object
b) Deallocates memory for an array of objects
c) Allocates memory for an array of objects
d) Allocates memory for a single object
Answer: b) Deallocates memory for an array of objects

Which operator is used to allocate memory dynamically in C?
a) malloc
b) new
c) calloc
d) alloc
Answer: a) malloc

What is the purpose of calloc in C?
a) Allocate memory and initialize it to zero
b) Allocate memory without initializing
c) Deallocate memory
d) Reallocate memory
Answer: a) Allocate memory and initialize it to zero

Which function is used to deallocate memory allocated by malloc in C?
a) free
b) delete
c) release
d) remove
Answer: a) free

What is a potential problem of using malloc and free in C?
a) Memory leaks
b) Memory fragmentation
c) Stack overflow
d) Overflow errors
Answer: a) Memory leaks

What is a memory leak?
a) Memory that is not properly released
b) Memory that is deallocated prematurely
c) Memory that is allocated but never used
d) Memory that is shared across processes
Answer: a) Memory that is not properly released

Which of the following is NOT a characteristic of dynamic memory allocation?
a) Allocates memory at runtime
b) Requires explicit deallocation
c) Allocates memory on the stack
d) Allows for flexible memory usage
Answer: c) Allocates memory on the stack

In C++, how do you allocate memory for an integer using new?
a) int* p = new int;
b) int p = new int;
c) int* p = malloc(sizeof(int));
d) int p = malloc(sizeof(int));
Answer: a) int* p = new int;

Which method is used to resize dynamically allocated memory in C++?
a) realloc
b) resize
c) extend
d) expand
Answer: a) realloc

In C++, which of the following is a correct syntax to delete a single object allocated with new?
a) delete p;
b) delete[] p;
c) free(p);
d) remove(p);
Answer: a) delete p;

Which of the following can lead to undefined behavior in dynamic memory allocation?
a) Double free
b) Single free
c) Allocating memory
d) Releasing memory
Answer: a) Double free

In which case should you use delete instead of delete[]?
a) When deallocating memory for an array
b) When deallocating memory for a single object
c) When allocating memory for an array
d) When reallocating memory
Answer: b) When deallocating memory for a single object

Which of the following is a valid operation for memory management in C++?
a) new[] and delete[]
b) malloc and delete
c) calloc and free
d) realloc and new
Answer: a) new[] and delete[]

Which of the following is an example of automatic memory management?
a) Garbage collection
b) Manual malloc and free
c) new and delete
d) realloc
Answer: a) Garbage collection

In which programming language is garbage collection typically automatic?
a) C
b) C++
c) Java
d) Assembly
Answer: c) Java

What does a garbage collector do?
a) Reclaims memory that is no longer in use
b) Allocates memory dynamically
c) Manages stack memory
d) Handles CPU scheduling
Answer: a) Reclaims memory that is no longer in use

Which of the following is NOT a goal of garbage collection?
a) Prevent memory leaks
b) Improve program performance
c) Reclaim unused memory
d) Optimize CPU usage
Answer: d) Optimize CPU usage

What is a common approach used by garbage collectors to determine if an object is still in use?
a) Reference counting
b) Memory fragmentation
c) Allocation tracking
d) Cache management
Answer: a) Reference counting

Which of the following is a method used by garbage collectors to find objects that are still in use?
a) Mark-and-sweep
b) Memory paging
c) Direct mapping
d) Load balancing
Answer: a) Mark-and-sweep

In the mark-and-sweep garbage collection algorithm, what happens during the “mark” phase?
a) Objects are marked as in use
b) Objects are deleted
c) Memory is allocated
d) Objects are copied to a new location
Answer: a) Objects are marked as in use

In the mark-and-sweep garbage collection algorithm, what happens during the “sweep” phase?
a) Unmarked objects are deallocated
b) All objects are marked
c) Memory is allocated
d) Objects are copied
Answer: a) Unmarked objects are deallocated

What does the term “stop-the-world” mean in the context of garbage collection?
a) The program is halted while garbage collection is performed
b) The garbage collector runs in the background
c) The program continues running while collecting garbage
d) Garbage collection happens in a different process
Answer: a) The program is halted while garbage collection is performed

Which of the following is a disadvantage of garbage collection?
a) It can introduce overhead and pause times
b) It guarantees that all memory is always reclaimed
c) It improves the performance of all programs
d) It prevents memory fragmentation
Answer: a) It can introduce overhead and pause times

What is reference counting in garbage collection?
a) Counting the number of references to an object to determine if it is still in use
b) Counting the total memory used by the program
c) Counting the number of allocations and deallocations
d) Counting the number of objects in memory
Answer: a) Counting the number of references to an object to determine if it is still in use

What is the primary purpose of the realloc function in C?
a) To allocate new memory
b) To resize an existing block of memory
c) To deallocate memory
d) To allocate memory for an array
Answer: b) To resize an existing block of memory

Which of the following is a feature of a good garbage collector?
a) It should minimize pause times for the program
b) It should increase memory usage
c) It should manually manage memory
d) It should require frequent manual intervention
Answer: a) It should minimize pause times for the program

What is a potential problem with reference counting garbage collection?
a) It cannot handle cyclic references
b) It cannot reclaim memory quickly
c) It has high overhead
d) It requires frequent pauses
Answer: a) It cannot handle cyclic references

Which of the following is NOT typically managed by garbage collectors?
a) Stack memory
b) Heap memory
c) Global variables
d) Local variables
Answer: a) Stack memory

In C++, what is the purpose of using the std::shared_ptr smart pointer?
a) To automatically manage memory with reference counting
b) To allocate memory dynamically
c) To manage stack memory
d) To prevent memory leaks manually
Answer: a) To automatically manage memory with reference counting

What does std::unique_ptr in C++ do?
a) Automatically manages a single object’s lifetime with exclusive ownership
b) Allows shared ownership of an object
c) Allocates memory for an array
d) Tracks memory leaks
Answer: a) Automatically manages a single object’s lifetime with exclusive ownership

What happens if you use delete on a pointer that was allocated with new[]?
a) Undefined behavior
b) Memory is correctly deallocated
c) A runtime error occurs
d) Memory is leaked
Answer: a) Undefined behavior

Which C++ feature can be used to ensure that memory allocated with new is properly deallocated even if an exception occurs?
a) RAII (Resource Acquisition Is Initialization)
b) try and catch blocks
c) Manual memory management
d) Garbage collection
Answer: a) RAII (Resource Acquisition Is Initialization)

What is the primary purpose of std::weak_ptr in C++?
a) To break circular references while using std::shared_ptr
b) To allocate memory dynamically
c) To manage stack memory
d) To automatically manage memory with reference counting
Answer: a) To break circular references while using std::shared_ptr

What does the term “heap” refer to in memory management?
a) Memory allocated at runtime
b) Memory allocated at compile time
c) Memory that is managed automatically by the stack
d) Memory used for global variables
Answer: a) Memory allocated at runtime

Which of the following is a method to manually manage dynamic memory in C++?
a) new and delete
b) malloc and free
c) calloc and realloc
d) All of the above
Answer: d) All of the above

In C++, which smart pointer is most appropriate for managing the lifetime of a single object that is not shared?
a) std::unique_ptr
b) std::shared_ptr
c) std::weak_ptr
d) std::auto_ptr
Answer: a) std::unique_ptr

What is the role of std::auto_ptr in C++?
a) To automatically manage the lifetime of a dynamically allocated object
b) To provide shared ownership of an object
c) To break cyclic references
d) To manage stack memory
Answer: a) To automatically manage the lifetime of a dynamically allocated object

Which of the following statements about garbage collection is FALSE?
a) It can automatically reclaim memory
b) It is available in all programming languages
c) It can reduce memory leaks
d) It can sometimes introduce performance overhead
Answer: b) It is available in all programming languages

What does std::allocator do in C++?
a) Provides a way to allocate and deallocate memory
b) Manages garbage collection
c) Allocates memory for global variables
d) Handles memory fragmentation
Answer: a) Provides a way to allocate and deallocate memory

Which of the following is a challenge with manual memory management?
a) Ensuring memory is freed correctly
b) Memory is automatically managed
c) Garbage collection is handled automatically
d) Automatic handling of cyclic references
Answer: a) Ensuring memory is freed correctly

In the context of dynamic memory management, what is a “fragmentation” issue?
a) Memory is divided into small unusable blocks
b) Memory is not allocated
c) Memory is automatically reclaimed
d) Memory is allocated for global variables
Answer: a) Memory is divided into small unusable blocks

Which of the following is a common feature of memory leaks?
a) The program consumes more memory over time
b) The program runs out of stack space
c) The program experiences segmentation faults
d) The program terminates unexpectedly
Answer: a) The program consumes more memory over time

What is the role of std::make_shared in C++?
a) To create a shared pointer and allocate memory for the object
b) To manage stack memory
c) To automatically manage the lifetime of a single object
d) To deallocate memory for a shared pointer
Answer: a) To create a shared pointer and allocate memory for the object

Which C++ feature can be used to manage memory for arrays dynamically?
a) std::vector
b) std::list
c) std::map
d) std::deque
Answer: a) std::vector

What is the purpose of the std::string class in C++ with respect to memory management?
a) To manage dynamic memory for strings automatically
b) To allocate fixed-size memory for strings
c) To manually handle memory for string operations
d) To manage global variables
Answer: a) To manage dynamic memory for strings automatically

What should be used to handle exceptions in C++ when dealing with dynamic memory allocation?
a) RAII (Resource Acquisition Is Initialization)
b) try and catch blocks
c) Manual memory management
d) Garbage collection
Answer: a) RAII (Resource Acquisition Is Initialization)

Which of the following best describes a “dangling pointer”?
a) A pointer that points to memory that has been deallocated
b) A pointer that points to a valid memory address
c) A pointer that is correctly initialized
d) A pointer that is not used
Answer: a) A pointer that points to memory that has been deallocated