This quiz on C++ Memory Management covers a comprehensive range of topics, including stack and heap memory, to advanced techniques such as dynamic memory allocation and smart pointers. Each question contains a detailed explanation to make you understand the correct answers and also grasp the underlying concepts.
Question 1
What is dynamic memory allocation in C++?
Management of memory automatically
Allocation of memory at compile time
Allocation of memory during runtime
Increasing allocated memory
Question 2
What is a memory leak in C++?
When the program uses too much memory
When memory is not allocated properly
When the program tries to access restricted memory
When dynamically allocated memory is not freed
Question 3
Which of the following is not a valid way to allocate memory dynamically for an integer in C++?
int* ptr = new int;
int* ptr = new;
int* ptr = new int(10);
int* ptr = new int[1];
Question 4
What is a dangling pointer in C++ ?
A pointer that has not been initialized
A pointer that points to a static variable
A pointer that points to an invalid memory location
A pointer that points to multiple locations
Question 5
What is the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int* ptr = new int;
*ptr = 10;
delete ptr;
cout << *ptr;
}
0
Runtime error
10
Garbage value
Question 6
What of the following is the correct way to use 'new' and 'delete' for dynamic memory allocation of an object in C++ ?
MyClass* obj = new MyClass(); delete obj;
MyClass obj = new MyClass(); delete obj;
MyClass* obj = new MyClass(); delete[] obj;
MyClass* obj = new MyClass[1]; delete obj;
Question 7
How do you check if memory allocation by 'new' was successful in C++?
Checking if the pointer is null
Using the isAllocated() function
Catching an exception
Both A and C
Question 8
What is the initial value of dynamically allocated memory in C++?
0
1
Null
Indeterminate
Question 9
What happens when 'new' fails to allocate memory in C++?
Program continues execution
It returns a null pointer
It throws a bad_alloc exception
Program terminates immediately
Question 10
Which of the following is true about move constructors?
They are used when an object is copied
They avoid deep copying by transferring ownership
They always throw exceptions
They only work with pointers
There are 20 questions to complete.