Function Overloading in C++
15 questions · 12 min · Medium
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.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
C. new
The new operator dynamically allocates memory and returns a pointer to the allocated object.
B. delete
The delete operator releases memory allocated for a single object using new.
C. delete[]
Memory allocated with new[] must be released using delete[] so that the appropriate array destruction and deallocation occur.
A. Stack
Local automatic variables are typically associated with stack storage, although exact implementation details are determined by the compiler and platform.
A. Heap
Dynamic storage is commonly provided from the heap or free store during program execution.
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.
A. Smart pointers
Smart pointers such as std::unique_ptr and std::shared_ptr automatically manage object lifetime according to ownership rules.
C. std::unique_ptr
std::unique_ptr represents exclusive ownership and cannot normally be copied.
B. std::shared_ptr
std::shared_ptr uses shared ownership and destroys the managed object when the last owning shared_ptr is gone.
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.
A. Resource Acquisition Is Initialization
RAII ties resource ownership to object lifetime so resources are acquired during initialization and released during destruction.
C. It throws std::bad_alloc
The throwing form of new throws std::bad_alloc when allocation fails.
A. std::nothrow
Using new with std::nothrow returns nullptr instead of throwing std::bad_alloc when allocation fails.
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.
B. Prefer RAII and appropriate smart pointers
Modern C++ generally favors RAII and smart pointers to express ownership and automatically manage resource lifetimes.