Any2Quiz
Computer Science / Languages / C++

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.

Play all 58 questions free

Sample questions

  1. In C++, what is the primary purpose of the `new` operator?

    1. To deallocate memory allocated on the stack.
    2. To allocate memory on the heap for a single object or an array of objects.
    3. To deallocate memory allocated on the heap.
    4. To allocate memory on the stack for a variable.
    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.

  2. What is the primary role of a smart pointer (e.g., `std::unique_ptr`, `std::shared_ptr`) in C++ memory management?

    1. To provide direct low-level control over memory addresses.
    2. To automate memory deallocation and prevent memory leaks by managing the lifetime of dynamically allocated objects.
    3. To increase the size of allocated memory blocks.
    4. To perform garbage collection, similar to Java or Python.
    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.

  3. What is the potential consequence of calling `delete` on a pointer to an object that was created using `new[]`?

    1. It will correctly deallocate the entire array.
    2. It will deallocate only the first element of the array.
    3. It will lead to undefined behavior, potentially corrupting memory or crashing the program.
    4. It will successfully deallocate the memory and call destructors for all elements.
    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.

  4. What is a primary concern when using `void*` for generic memory manipulation in C++?

    1. It automatically performs type casting.
    2. It loses type information, requiring manual casting and increasing the risk of type-related errors.
    3. It guarantees memory safety.
    4. It is always more efficient than using specific pointer types.
    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.

  5. What is the primary reason to use `std::weak_ptr`?

    1. To increase the reference count of a `std::shared_ptr`.
    2. To allow access to a managed object without affecting its lifetime, typically to break cyclic references between `std::shared_ptr`s.
    3. To exclusively own a dynamically allocated object.
    4. To perform garbage collection.
    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.

Take the full quiz

More quizzes

Browse all C++ quizzes