Chapter 7: MCQs on Memory Management in C++ | C++ Programming

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)

  1. 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
  2. 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
  3. How do you deallocate memory in C++ that was allocated using “new”? a) Using “delete”
    b) Using “free”
    c) Using “remove”
    d) Using “release”
  4. 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
  5. 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;
  6. 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
  7. 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];
  8. 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);
  9. 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
  10. 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

  1. 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
  2. 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”
  3. 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
  4. 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
  5. 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
  6. 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
  7. Which of the following tools can be used to detect memory leaks? a) Code formatter
    b) Memory debugger
    c) Compiler optimizer
    d) Linker
  8. 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
  9. 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
  10. 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

  1. 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
  2. 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
  3. 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
  4. 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”
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

QnoAnswer
1b) To allocate memory dynamically
2c) It allocates memory on the heap
3a) Using “delete”
4b) Memory leaks occur
5b) delete [] ptr
6b) Undefined behavior may occur
7a) int* ptr = new int[10];
8b) int* ptr = new int;
9b) Deallocates the memory pointed to by “ptr”
10c) Loop through each row and delete[]
11b) Memory that is allocated but not properly deallocated
12c) Forgetting to use “delete” after “new”
13a) Increased memory usage over time
14b) Always delete dynamically allocated memory when no longer needed
15b) When memory allocated on the heap is not deallocated
16d) By using “delete” for dynamically allocated memory when it’s no longer needed
17b) Memory debugger
18c) Use “delete[]” to free the array
19a) By automatically deallocating memory when the pointer goes out of scope
20a) It automatically deletes the memory when it’s no longer needed
21c) It does nothing
22b) Pointers that have been deleted but are still in use
23c) Set the pointer to nullptr
24a) When a pointer is deleted twice
25b) It leads to undefined behavior
26b) It may lead to increased memory usage and program crashes
27c) A pointer with no valid memory address
28a) It automatically deallocates resources when they go out of scope
29b) When memory goes out of scope, if it is stack-allocated
30c) Gradual decrease in performance

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top