Skip to content
QuiznExam

Memory Management in C++

Test your knowledge of memory management in C++, including stack and heap memory, dynamic allocation, new and delete, pointers, smart pointers, memory leaks, and RAII.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. Which operator is used to dynamically allocate memory for a single object in C++?

    • A. malloc
    • B. alloc
    • C. new
    • D. create
    Show answer

    C. new

    The new operator dynamically allocates memory and returns a pointer to the allocated object.

  2. 2. Which operator is used to release memory allocated with new?

    • A. free
    • B. delete
    • C. remove
    • D. release
    Show answer

    B. delete

    The delete operator releases memory allocated for a single object using new.

  3. 3. Which operator should be used to release memory allocated with new[]?

    • A. delete
    • B. free
    • C. delete[]
    • D. release[]
    Show answer

    C. delete[]

    Memory allocated with new[] must be released using delete[] so that the appropriate array destruction and deallocation occur.

  4. 4. Where are local automatic variables typically stored?

    • A. Stack
    • B. Heap
    • C. ROM
    • D. Database
    Show answer

    A. Stack

    Local automatic variables are typically associated with stack storage, although exact implementation details are determined by the compiler and platform.

  5. 5. What is commonly used to allocate memory dynamically during program execution?

    • A. Heap
    • B. Register
    • C. Constant pool
    • D. Source file
    Show answer

    A. Heap

    Dynamic storage is commonly provided from the heap or free store during program execution.

  6. 6. What is a memory leak?

    • A. Reading a valid variable
    • B. Memory that remains allocated but is no longer accessible
    • C. Using a stack variable
    • D. Deleting memory twice
    Show answer

    B. Memory that remains allocated but is no longer accessible

    A memory leak occurs when dynamically allocated memory is not released and the program can no longer access the allocation.

  7. 7. Which C++ feature helps manage dynamically allocated objects automatically?

    • A. Smart pointers
    • B. Preprocessor macros
    • C. Namespaces
    • D. Header guards
    Show answer

    A. Smart pointers

    Smart pointers such as std::unique_ptr and std::shared_ptr automatically manage object lifetime according to ownership rules.

  8. 8. Which smart pointer provides exclusive ownership of a dynamically allocated object?

    • A. std::shared_ptr
    • B. std::weak_ptr
    • C. std::unique_ptr
    • D. std::auto_ptr
    Show answer

    C. std::unique_ptr

    std::unique_ptr represents exclusive ownership and cannot normally be copied.

  9. 9. Which smart pointer allows multiple shared owners of an object?

    • A. std::unique_ptr
    • B. std::shared_ptr
    • C. std::weak_ptr
    • D. std::raw_ptr
    Show answer

    B. std::shared_ptr

    std::shared_ptr uses shared ownership and destroys the managed object when the last owning shared_ptr is gone.

  10. 10. What is the primary purpose of std::weak_ptr?

    • A. To allocate arrays
    • B. To provide non-owning access to an object managed by shared_ptr
    • C. To replace new
    • D. To increase stack size
    Show answer

    B. To provide non-owning access to an object managed by shared_ptr

    std::weak_ptr observes an object managed by shared_ptr without contributing to its reference count.

  11. 11. What does RAII stand for in C++?

    • A. Resource Acquisition Is Initialization
    • B. Runtime Allocation Is Immediate
    • C. Resource Allocation Is Internal
    • D. Runtime Access Is Initialization
    Show answer

    A. Resource Acquisition Is Initialization

    RAII ties resource ownership to object lifetime so resources are acquired during initialization and released during destruction.

  12. 12. What happens when new cannot allocate the requested memory by default?

    • A. It returns zero
    • B. It returns nullptr
    • C. It throws std::bad_alloc
    • D. It silently ignores the request
    Show answer

    C. It throws std::bad_alloc

    The throwing form of new throws std::bad_alloc when allocation fails.

  13. 13. Which function can be used with a nothrow allocation to avoid throwing an exception when allocation fails?

    • A. std::nothrow
    • B. std::safealloc
    • C. std::noexcept
    • D. std::nullalloc
    Show answer

    A. std::nothrow

    Using new with std::nothrow returns nullptr instead of throwing std::bad_alloc when allocation fails.

  14. 14. What is a dangling pointer?

    • A. A pointer that points to valid allocated memory
    • B. A pointer that points to an object whose lifetime has ended
    • C. A pointer initialized to nullptr
    • D. A pointer stored on the stack
    Show answer

    B. A pointer that points to an object whose lifetime has ended

    A dangling pointer refers to storage whose referenced object's lifetime has ended or whose allocation has been released.

  15. 15. Which practice is generally recommended for safer dynamic memory management in modern C++?

    • A. Use raw new and delete everywhere
    • B. Prefer RAII and appropriate smart pointers
    • C. Never release allocated memory
    • D. Use malloc for all objects
    Show answer

    B. Prefer RAII and appropriate smart pointers

    Modern C++ generally favors RAII and smart pointers to express ownership and automatically manage resource lifetimes.