Memory Management Quiz - Languages C++
Free practice quiz: Memory Management (Languages C++). 58 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages C++ practice quiz has 58 multiple-choice questions on Memory Management, and 58 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 58 questions, freeSample questions
In C++, what is the primary purpose of the `new` operator?
Show answer
Answer: B. To allocate memory on the heap for a single object or an array of objects.
The `new` operator in C++ is used to dynamically allocate memory on the heap during runtime. It returns a pointer to the newly allocated memory. It can be used to allocate memory for a single object or for an array of objects. `delete` or `delete[]` are used for deallocation.
What is the primary role of a smart pointer (e.g., `std::unique_ptr`, `std::shared_ptr`) in C++ memory management?
Show answer
Answer: B. To automate memory deallocation and prevent memory leaks by managing the lifetime of dynamically allocated objects.
Smart pointers are C++ objects that wrap raw pointers and automatically manage the memory they point to. They implement RAII principles, ensuring that the memory is deallocated when the smart pointer goes out of scope or when its ownership semantics dictate, thus preventing memory leaks and making manual `delete` calls largely unnecessary.
What is the potential consequence of calling `delete` on a pointer to an object that was created using `new[]`?
Show answer
Answer: C. It will lead to undefined behavior, potentially corrupting memory or crashing the program.
When memory is allocated for an array using `new[]`, the compiler assumes that `delete[]` will be used for deallocation. Using `delete` instead of `delete[]` means that only the destructor for the first element (if it's a class type) might be called, and the deallocation of the remaining memory is not guaranteed to be correct, leading to undefined behavior.
What is a primary concern when using `void*` for generic memory manipulation in C++?
Show answer
Answer: B. It loses type information, requiring manual casting and increasing the risk of type-related errors.
A `void*` pointer is a generic pointer that can point to any data type. However, it does not carry any type information. To access the data it points to, you must explicitly cast it to the correct pointer type. This manual casting bypasses C++'s type checking mechanisms, making it prone to errors if the cast is incorrect, potentially leading to memory corruption or crashes.
What is the primary reason to use `std::weak_ptr`?
Show answer
Answer: B. To allow access to a managed object without affecting its lifetime, typically to break cyclic references between `std::shared_ptr`s.
`std::weak_ptr` provides non-owning access to an object managed by `std::shared_ptr`. It does not increase the reference count. Its main use case is to break cyclic dependencies that can occur between `std::shared_ptr`s, which would otherwise prevent the objects from being deallocated. A `weak_ptr` can be converted to a `shared_ptr` using `lock()`, which will succeed only if the object still exists.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the purpose of the `delete` operator in C++?
- What is a memory leak in C++?
- Which of the following is a consequence of using `delete` on a pointer that was not allocated with `new` or has already been deallocated?
- What is the purpose of `new[]` and `delete[]` in C++?
- Consider the following C++ code snippet: `int* ptr = new int; *ptr = 10; delete ptr; ptr = new int; *ptr = 20;`. What is the most critical aspect of memory management demonstrated here?
- What is the risk associated with a dangling pointer in C++ memory management?
- In C++, when would you typically use `malloc()` and `free()` over `new` and `delete`?
- What is the primary advantage of RAII (Resource Acquisition Is Initialization) in C++ memory management?
- Consider `std::vector<int> v; v.push_back(10);`. How is memory managed for the elements of `v`?
- What happens to memory allocated with `new` if the program terminates unexpectedly (e.g., due to a crash) before `delete` is called?
- If a class has a destructor that releases dynamically allocated memory, and an object of this class is created using `new`, what must be done to correctly deallocate its memory and execute its destructor?
- What is the key difference between stack allocation and heap allocation in terms of scope and lifetime?
- Consider the following code: `char* buffer = new char[100]; ... // some operations ... delete[] buffer;`. If an exception is thrown between the `new[]` and `delete[]` calls, what is the likely outcome without proper exception handling?
- What is the purpose of `std::make_unique` in modern C++?
- When using `std::shared_ptr`, how is memory deallocated?
- What is a potential issue with a raw pointer managing dynamically allocated memory if the pointer itself goes out of scope without being `delete`d?
- What does the `std::bad_alloc` exception signify in C++?
- In C++, what is the purpose of `placement new`?
- What is a common strategy to prevent memory leaks when managing raw pointers in C++?
- Consider `int* arr = new int[5];`. If an exception occurs after this line but before `delete[] arr;`, and no exception handling is in place, what is the fate of the allocated memory?
- What is the purpose of `std::move` in conjunction with smart pointers, particularly `std::unique_ptr`?
- Which of the following best describes the concept of memory fragmentation?
- What is the primary advantage of using `std::vector` over a raw dynamically allocated array (`int* arr = new int[size]; delete[] arr;`) for managing collections of elements?
- If you have a `std::unique_ptr<MyClass> ptr(new MyClass());`, and you want to transfer ownership to another `std::unique_ptr`, how should you do it?
- What is the primary characteristic of memory allocated on the heap?
- What is the role of a destructor in C++ regarding memory management?
- Consider this C++ code: `int* p = nullptr; { int x = 10; p = &x; }`. What is the state of `p` after the block `{}`, and why?
- What is the primary motivation for using `std::make_shared` in C++?
- In C++, what is the purpose of the `sizeof` operator in relation to memory allocation?
- What is the key advantage of using `std::string` over a C-style character array (`char*`) for string manipulation in C++?
- Consider the following code: `MyClass* obj = new MyClass(); ... obj->~MyClass();`. Is this the correct way to deallocate memory and destroy an object allocated with `new`?
- What is the purpose of `std::vector::reserve()`?
- In C++, what is the fundamental difference between an lvalue and an rvalue regarding memory?
- What is the purpose of the `new` operator in C++ for object allocation?
- What is the primary danger of using `realloc()` from `<cstdlib>` in C++ with C++ objects?
- What is the role of the `std::unique_ptr` destructor?
- Which of the following is the most fundamental reason for managing memory dynamically in C++?
- What is the purpose of the `std::vector::shrink_to_fit()` member function?
- What is a potential issue with relying solely on exception handling (`try-catch`) for memory management when using raw pointers?
- What is the purpose of the `std::move` constructor or assignment operator?
- What is the correct way to deallocate an array of integers allocated with `new int[10]`?
- In C++, what is the purpose of `std::unique_ptr`?
- What is the critical problem with implementing a custom memory allocator in C++ without careful consideration of alignment?
- What is the purpose of the `std::bad_alloc::what()` method?
- When is it generally recommended to use `malloc`/`free` instead of `new`/`delete` in a C++ program?
- What is the primary benefit of using `std::vector`'s `emplace_back` over `push_back` when adding an object to the vector?
- What is the role of the `std::allocator` in C++ standard library containers?
- Consider the C++ code: `int* p = new int(5); int* q = p; delete p;`. What is the state of `q` after `delete p;`?
- What is the purpose of `std::shared_ptr`?
- What is the main advantage of RAII (Resource Acquisition Is Initialization) for managing dynamic memory in C++?
- What does the `std::string::capacity()` method return?
- What is the purpose of `delete static_cast<MyClass*>(ptr)` when `ptr` points to a single object allocated with `new MyClass()`?
- Which C++ feature directly addresses the problem of memory leaks caused by unhandled exceptions when managing raw pointers?