C++ Memory Management Quiz - MCQ Questions and Answers

Welcome to our "C++ Memory Management Quiz," a resource designed to test and enhance your understanding of one of the most crucial aspects of C++ programming: memory management. This quiz comprises a series of multiple-choice questions (MCQs) that delve into the core concepts of dynamic memory allocation, pointers, memory leaks, and more. 

Each question is accompanied by an in-depth explanation, providing a comprehensive learning experience. Whether you're prepping for an interview, an exam, or simply looking to brush up on your C++ memory management skills, this quiz is for you. Let's get started and see how much you really know about managing memory in C++!

1. What is dynamic memory allocation in C++?

a) Allocating memory at compile time
b) Automatically managing memory
c) Allocating memory during runtime
d) Increasing the size of the stack

2. Which operator is used to allocate memory dynamically in C++?

a) malloc
b) alloc
c) new
d) create

3. What is the purpose of the 'delete' operator in C++?

a) To delete variables from the stack
b) To free dynamically allocated memory
c) To remove values from an array
d) To terminate the program

4. How is memory allocated for an array dynamically in C++?

a) int* arr = new int[10];
b) int arr = new int[10];
c) int arr[10] = new int;
d) int* arr = alloc int[10];

5. What is a memory leak in C++?

a) When the program uses too much memory
b) When memory is not allocated properly
c) When dynamically allocated memory is not freed
d) When the program tries to access restricted memory

6. Which of the following is not a valid way to allocate memory dynamically for an integer in C++?

a) int* ptr = new int;
b) int* ptr = new int(10);
c) int* ptr = new;
d) int* ptr = new int[1];

7. What happens when you use the 'delete' operator on a pointer that is not dynamically allocated?

a) The program will successfully delete the pointer
b) It will cause a runtime error
c) The compiler will produce an error
d) It may lead to undefined behavior

8. How do you allocate and initialize memory for a single integer to 5 in C++?

a) int* ptr = new int(5);
b) int* ptr = new int = 5;
c) int* ptr = new int[5];
d) int* ptr = new int(1, 5);

9. What is a dangling pointer in C++?

a) A pointer that has not been initialized
b) A pointer that points to an invalid memory location
c) A pointer that points to a static variable
d) A pointer that points to multiple locations

10. What does the 'delete[]' operator do in C++?

a) Deletes an array from the stack
b) Frees memory allocated for a single variable
c) Frees memory allocated for an array
d) Deletes a pointer

11. What is the output of the following C++ code?

   int* ptr = new int;
   *ptr = 10;
   delete ptr;
   cout << *ptr;
a) 0
b) 10
c) Garbage value
d) Runtime error

12. How do you prevent a memory leak in C++?

a) By using the malloc function
b) By not using dynamic memory allocation
c) By freeing dynamically allocated memory using 'delete' or 'delete[]'
d) By using static memory allocation only

13. What is the correct way to use 'new' and 'delete' for dynamic memory allocation of an object in C++?

a) MyClass* obj = new MyClass(); delete obj;
b) MyClass obj = new MyClass(); delete obj;
c) MyClass* obj = new MyClass(); delete[] obj;
d) MyClass* obj = new MyClass[1]; delete obj;

14. How do you check if memory allocation by 'new' was successful in C++?

a) Checking if the pointer is null
b) Using the 'isAllocated' function
c) Catching an exception
d) Both a) and c)

15. What is the initial value of dynamically allocated memory in C++?

a) 0
b) 1
c) Null
d) Indeterminate

16. What happens when 'new' fails to allocate memory in C++?

a) The program continues execution
b) It returns a null pointer
c) It throws a bad_alloc exception
d) The program terminates immediately

17. How can you dynamically allocate memory for a two-dimensional array in C++?

a) int** arr = new int[rows][cols];
b) int* arr = new int[rows][cols];
c) int** arr = new int*[rows]; for(int i = 0; i < rows; i++) arr[i] = new int[cols];
d) int* arr = new int*(rows, cols);

18. What is RAII in the context of C++ memory management?

a) Resource Allocation Is Initialization
b) Random Access of Independent Indexes
c) Release After Immediate Initialization
d) Resource Allocation In Iteration

19. What is the advantage of smart pointers over raw pointers in C++ for memory management?

a) Smart pointers are faster than raw pointers
b) Smart pointers provide automatic memory management
c) Smart pointers can store more types of data
d) Smart pointers use less memory

20. What is the output of the following C++ code?

int* ptr = new int(10);
   cout << ptr;
a) 10
b) The memory address stored in ptr
c) 0
d) Error

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare