Memory management is a crucial aspect of programming, especially in languages like C++ that allow direct memory control. This chapter covers dynamic memory allocation, avoiding memory leaks, and effective use of memory in applications. Here are 30 MCQs on memory management in C++.
Dynamic Memory Allocation (new and delete)
What is the purpose of the “new” operator in C++? a) To delete memory b) To allocate memory dynamically c) To check memory usage d) To clear the stack
Which of the following statements is correct about “new” in C++? a) It can only be used for integers b) It allocates memory on the stack c) It allocates memory on the heap d) It cannot be used with arrays
How do you deallocate memory in C++ that was allocated using “new”? a) Using “delete” b) Using “free” c) Using “remove” d) Using “release”
What happens if “delete” is not used for dynamically allocated memory? a) Memory is automatically deallocated b) Memory leaks occur c) The program terminates d) No impact on memory
Which of the following correctly deallocates memory allocated for an array in C++? a) delete ptr[]; b) delete [] ptr; c) delete *ptr; d) delete ptr;
In C++, what will happen if you try to delete a pointer that was not allocated with “new”? a) The program will compile successfully b) Undefined behavior may occur c) Memory will be deallocated safely d) Nothing will happen
Which of the following syntax is correct for dynamically allocating an array of integers with 10 elements? a) int* ptr = new int[10]; b) int ptr = new int(10); c) int* ptr = new[10] int; d) int ptr = int[10];
How do you allocate a single integer dynamically in C++? a) int ptr = malloc(sizeof(int)); b) int* ptr = new int; c) int ptr = allocate int; d) int* ptr = malloc(int);
In C++, what does “delete ptr;” do if “ptr” is a pointer to a dynamically allocated integer? a) Deletes the pointer variable b) Deallocates the memory pointed to by “ptr” c) Causes a memory leak d) Only nullifies the pointer
What should be used to delete a dynamically allocated 2D array in C++? a) delete array[ ][ ]; b) delete [] array; c) Loop through each row and delete[] d) delete all array elements directly
Memory Leaks and Avoidance
What is a memory leak? a) Releasing memory before it is used b) Memory that is allocated but not properly deallocated c) Allocating memory for global variables d) A feature of dynamic memory
Which of the following can cause a memory leak in C++? a) Using “delete” for each pointer b) Using “free” instead of “delete” c) Forgetting to use “delete” after “new” d) Releasing memory with “delete”
Which of the following is a sign of a memory leak in a program? a) Increased memory usage over time b) Immediate program crash c) Reduced memory usage d) Decreased CPU usage
What is one common way to avoid memory leaks in C++? a) Avoid using “new” and “delete” b) Always delete dynamically allocated memory when no longer needed c) Use global variables only d) Avoid using loops
When does a memory leak occur? a) When memory is allocated on the stack b) When memory allocated on the heap is not deallocated c) When variables are deleted before use d) When the program completes execution
How can a memory leak be prevented in C++? a) By avoiding dynamic allocation b) By deleting all pointers at the end of the program c) By using “delete” only at the beginning of the program d) By using “delete” for dynamically allocated memory when it’s no longer needed
Which of the following tools can be used to detect memory leaks? a) Code formatter b) Memory debugger c) Compiler optimizer d) Linker
Which is the correct way to prevent memory leaks when working with dynamic arrays? a) Avoid using arrays in the code b) Use “delete” instead of “delete[]” c) Use “delete[]” to free the array d) Use “malloc” to allocate the array
How do smart pointers in C++ help manage memory? a) By automatically deallocating memory when the pointer goes out of scope b) By preventing dynamic allocation c) By stopping the program when a memory leak occurs d) By avoiding memory allocation on the heap
What is the purpose of “unique_ptr” in C++? a) It automatically deletes the memory when it’s no longer needed b) It prevents other pointers from pointing to the memory c) It allows multiple references to the memory d) It doubles the memory allocation
Avoiding Common Memory Management Pitfalls
What happens if “delete” is called on a null pointer? a) It causes a segmentation fault b) It leads to a memory leak c) It does nothing d) It deletes all allocated memory
Which of the following best describes “dangling pointers”? a) Pointers that are never used b) Pointers that have been deleted but are still in use c) Pointers that are never deleted d) Pointers that hold null values
What is a common practice to avoid dangling pointers after using “delete”? a) Set the pointer to a random address b) Assign the pointer a new value c) Set the pointer to nullptr d) Leave the pointer as it is
How does a double deletion error occur? a) When a pointer is deleted twice b) When a pointer is allocated twice c) When two pointers point to the same address d) When “delete[]” is used instead of “delete”
What would happen if you use “delete[]” on a pointer that was allocated with “new” (not an array)? a) Memory is deallocated safely b) It leads to undefined behavior c) The array is deallocated completely d) The pointer is set to nullptr
How does improper memory management affect performance? a) It always improves performance b) It may lead to increased memory usage and program crashes c) It has no impact on performance d) It slows down the compilation process
In C++, what does “nullptr” signify? a) A pointer to null memory b) A reserved keyword for arrays c) A pointer with no valid memory address d) A pointer to stack memory
Which of the following is a benefit of using RAII (Resource Acquisition Is Initialization) in C++? a) It automatically deallocates resources when they go out of scope b) It reduces the need for smart pointers c) It keeps resources allocated indefinitely d) It prevents all memory allocation
When is memory automatically deallocated in C++? a) When using “delete” b) When memory goes out of scope, if it is stack-allocated c) When using “new” d) When a function is called
Which of the following is a potential consequence of memory leaks in long-running applications? a) Improved memory utilization b) Increased stability over time c) Gradual decrease in performance d) Reduced program complexity
Answers
Qno
Answer
1
b) To allocate memory dynamically
2
c) It allocates memory on the heap
3
a) Using “delete”
4
b) Memory leaks occur
5
b) delete [] ptr
6
b) Undefined behavior may occur
7
a) int* ptr = new int[10];
8
b) int* ptr = new int;
9
b) Deallocates the memory pointed to by “ptr”
10
c) Loop through each row and delete[]
11
b) Memory that is allocated but not properly deallocated
12
c) Forgetting to use “delete” after “new”
13
a) Increased memory usage over time
14
b) Always delete dynamically allocated memory when no longer needed
15
b) When memory allocated on the heap is not deallocated
16
d) By using “delete” for dynamically allocated memory when it’s no longer needed
17
b) Memory debugger
18
c) Use “delete[]” to free the array
19
a) By automatically deallocating memory when the pointer goes out of scope
20
a) It automatically deletes the memory when it’s no longer needed
21
c) It does nothing
22
b) Pointers that have been deleted but are still in use
23
c) Set the pointer to nullptr
24
a) When a pointer is deleted twice
25
b) It leads to undefined behavior
26
b) It may lead to increased memory usage and program crashes
27
c) A pointer with no valid memory address
28
a) It automatically deallocates resources when they go out of scope
29
b) When memory goes out of scope, if it is stack-allocated