C++ Smart Pointer Quiz

C++
0 Passed
0% acceptance

40 in-depth questions covering C++ smart pointer types, ownership semantics, memory management, and cyclic reference avoidance — with 16 code examples to solidify understanding.

40 Questions
~80 minutes
1

Question 1

What is the purpose of std::unique_ptr in C++?

A
To provide exclusive ownership of dynamically allocated objects with automatic cleanup when going out of scope
B
To share ownership between multiple pointers
C
To create weak references to objects
D
To manage static memory
2

Question 2

What is the difference between std::unique_ptr and raw pointers?

cpp
std::unique_ptr<int> ptr = std::make_unique<int>(42);
// Automatic cleanup when ptr goes out of scope

int* raw = new int(42);
delete raw; // Manual cleanup required
A
unique_ptr automatically manages object lifetime and prevents memory leaks, raw pointers require manual memory management and are prone to leaks and dangling pointers
B
They are identical in functionality
C
Raw pointers are safer than unique_ptr
D
unique_ptr cannot be moved or transferred
3

Question 3

What is the purpose of std::shared_ptr?

cpp
#include <memory>

std::shared_ptr<int> ptr1 = std::make_shared<int>(42);
std::shared_ptr<int> ptr2 = ptr1; // Reference count = 2
// Object deleted when last shared_ptr is destroyed
A
To enable shared ownership of objects through reference counting, where the object is deleted when the last shared_ptr referencing it is destroyed
B
To provide exclusive ownership
C
To create weak references
D
To manage stack memory
4

Question 4

What is std::weak_ptr used for?

A
To break cyclic references between shared_ptr objects by providing non-owning references that can be checked for validity
B
To provide exclusive ownership
C
To create new objects
D
To manage static memory
5

Question 5

What is ownership semantics in smart pointers?

A
The rules defining which pointer is responsible for deleting the object, including exclusive ownership (unique_ptr) and shared ownership (shared_ptr)
B
The size of pointer objects
C
The speed of pointer operations
D
The color of pointer handles
6

Question 6

What is the difference between std::make_unique and std::make_shared?

cpp
auto unique = std::make_unique<int>(42); // Allocates int separately

auto shared = std::make_shared<int>(42); // Allocates int and control block together
A
make_unique allocates object and control block separately, make_shared allocates them together for better cache performance and reduced allocations
B
They are identical functions
C
make_shared is slower than make_unique
D
make_unique requires two allocations
7

Question 7

What is a cyclic reference in smart pointers?

cpp
class A { std::shared_ptr<B> b; };
class B { std::shared_ptr<A> a; };

auto a = std::make_shared<A>();
auto b = std::make_shared<B>();
a->b = b; // a owns b
b->a = a; // b owns a
// Memory leak: objects never deleted due to circular ownership
A
When two or more shared_ptr objects hold references to each other, preventing their reference counts from reaching zero and causing memory leaks
B
When pointers point to the same object
C
When pointers are null
D
When pointers are moved
8

Question 8

What is the purpose of custom deleters in smart pointers?

cpp
auto fileDeleter = [](FILE* f) { if (f) fclose(f); };
std::unique_ptr<FILE, decltype(fileDeleter)> filePtr(
fopen("data.txt", "r"), fileDeleter); // Custom cleanup
A
To specify custom cleanup logic for resources that aren't deleted with standard delete, such as C-style resources or special memory pools
B
To change pointer ownership
C
To create new objects
D
To modify object data
9

Question 9

What is the difference between unique_ptr and shared_ptr performance?

A
unique_ptr has zero runtime overhead compared to raw pointers, shared_ptr has reference counting overhead for thread safety and shared ownership
B
They have identical performance
C
shared_ptr is faster than unique_ptr
D
unique_ptr requires reference counting
10

Question 10

What is the purpose of std::weak_ptr::lock()?

cpp
std::weak_ptr<int> weak = someSharedPtr;
if (auto shared = weak.lock()) {
    // Object still exists, use it
    *shared = 42;
} else {
    // Object has been deleted
}
A
To attempt to create a shared_ptr from a weak_ptr, returning empty shared_ptr if the object has been deleted
B
To delete the pointed object
C
To create a new weak_ptr
D
To change ownership
11

Question 11

What is the difference between move semantics and smart pointer ownership transfer?

cpp
std::unique_ptr<int> ptr1 = std::make_unique<int>(42);
std::unique_ptr<int> ptr2 = std::move(ptr1); // Ownership transfer
// ptr1 is now nullptr
A
Move semantics transfer ownership efficiently without copying, smart pointer moves transfer resource ownership between pointer objects
B
They are identical concepts
C
Move semantics copy objects
D
Smart pointer moves require copying
12

Question 12

What is the difference between shared_ptr thread safety guarantees?

A
Reference counting is thread-safe for concurrent operations, but accessing the pointed object requires external synchronization
B
shared_ptr is completely thread-safe for all operations
C
shared_ptr is not thread-safe at all
D
Thread safety depends on the pointed object type
13

Question 13

What is the purpose of std::enable_shared_from_this?

cpp
class NetworkConnection : public std::enable_shared_from_this<NetworkConnection> {
public:
    void connect() {
        // Get shared_ptr to this object safely
        auto self = shared_from_this();
        // Pass self to callbacks without ownership issues
    }
};
A
To allow objects to safely create shared_ptr instances to themselves, enabling proper shared ownership within member functions
B
To disable shared ownership
C
To create unique_ptr instances
D
To prevent object creation
14

Question 14

What is the difference between unique_ptr and auto_ptr?

A
unique_ptr is move-only and prevents accidental copies that could cause double deletion, auto_ptr used copy semantics that were error-prone
B
They are identical classes
C
auto_ptr is safer than unique_ptr
D
unique_ptr allows copying
15

Question 15

What is the purpose of std::shared_ptr::use_count()?

cpp
auto ptr1 = std::make_shared<int>(42);
std::cout << ptr1.use_count(); // 1

auto ptr2 = ptr1;
std::cout << ptr1.use_count(); // 2
A
To return the current reference count, useful for debugging and understanding ownership sharing but not for control flow decisions
B
To delete the pointed object
C
To create new shared_ptr instances
D
To change ownership
16

Question 16

What is the difference between smart pointers and garbage collection?

A
Smart pointers provide deterministic cleanup at scope exit through RAII, garbage collection provides non-deterministic cleanup when memory pressure occurs
B
They are identical approaches
C
Garbage collection is faster
D
Smart pointers require runtime overhead
17

Question 17

What is the difference between unique_ptr and scoped_ptr?

A
unique_ptr is standard C++11 and moveable, scoped_ptr was a Boost library pointer that was non-copyable but not moveable
B
They are identical classes
C
scoped_ptr is part of standard C++
D
unique_ptr cannot be moved
18

Question 18

What is the difference between shared_ptr and intrusive_ptr?

A
shared_ptr manages reference counting automatically, intrusive_ptr requires the object to contain reference count members
B
They are identical classes
C
intrusive_ptr is part of standard C++
D
shared_ptr requires reference count members
19

Question 19

What is the difference between weak_ptr and raw pointers?

A
weak_ptr provides safe access to potentially deleted objects through lock(), raw pointers can become dangling without notification
B
They are identical in safety
C
Raw pointers are safer than weak_ptr
D
weak_ptr can become dangling
20

Question 20

What is the difference between smart pointer ownership and object ownership?

A
Smart pointer ownership refers to pointer lifetime management, object ownership refers to who has permission to modify or delete the object
B
They are identical concepts
C
Object ownership includes pointer lifetime
D
Smart pointer ownership is about modification permissions
21

Question 21

What is the difference between unique_ptr reset and assignment?

cpp
std::unique_ptr<int> ptr = std::make_unique<int>(42);
ptr.reset(); // Delete current object, set to nullptr

ptr = std::make_unique<int>(100); // Delete current, assign new
A
reset() deletes current object and sets pointer to nullptr, assignment deletes current object and takes ownership of new object
B
They are identical operations
C
Assignment doesn't delete objects
D
reset() assigns new objects
22

Question 22

What is the difference between shared_ptr aliasing and ownership?

cpp
struct Data { int value; std::string name; };
auto data = std::make_shared<Data>();
std::shared_ptr<int> valuePtr(data, &data->value); // Aliases data
// valuePtr keeps data alive but points to value member
A
Ownership manages object lifetime, aliasing allows pointing to object members while maintaining ownership of the containing object
B
They are identical concepts
C
Aliasing manages object lifetime
D
Ownership allows pointing to members
23

Question 23

What is the difference between smart pointer overhead and raw pointer overhead?

A
Smart pointers have small overhead for reference counting and deletion, raw pointers have zero overhead but require manual management
B
They have identical overhead
C
Raw pointers have more overhead
D
Smart pointers have zero overhead
24

Question 24

What is the difference between weak_ptr expired and lock?

cpp
std::weak_ptr<int> weak = someSharedPtr;
if (!weak.expired()) {
    auto shared = weak.lock(); // Safe access
    // Use shared...
}
A
expired() checks if object still exists without creating shared_ptr, lock() attempts to create shared_ptr and returns empty if expired
B
They are identical functions
C
lock() checks existence
D
expired() creates shared_ptr
25

Question 25

What is the difference between unique_ptr for arrays and vectors?

cpp
std::unique_ptr<int[]> arr = std::make_unique<int[]>(100);
// Array syntax: arr[50]

std::vector<int> vec(100);
// Vector interface: vec.at(50), vec.size()
A
unique_ptr arrays provide basic array access, vectors provide rich container interface with bounds checking and size management
B
They are identical containers
C
Vectors provide only array access
D
unique_ptr arrays have bounds checking
26

Question 26

What is the difference between shared_ptr thread safety and data race freedom?

A
shared_ptr reference counting is thread-safe, but accessing the pointed-to object requires external synchronization to prevent data races
B
shared_ptr is completely thread-safe for all operations
C
shared_ptr prevents all data races
D
Thread safety depends on the pointed object
27

Question 27

What is the difference between custom deleter syntax for unique_ptr and shared_ptr?

cpp
auto deleter = [](int* p) { delete p; };
std::unique_ptr<int, decltype(deleter)> uptr(new int(42), deleter);

std::shared_ptr<int> sptr(new int(42), deleter);
A
unique_ptr specifies deleter type as template parameter, shared_ptr accepts deleter as constructor parameter
B
They use identical syntax
C
shared_ptr requires template parameter for deleter
D
unique_ptr accepts deleter as constructor parameter
28

Question 28

What is the difference between weak_ptr use_count and shared_ptr use_count?

A
weak_ptr::use_count() returns current shared ownership count, shared_ptr::use_count() also returns shared ownership count but from owning pointer
B
They return different counts
C
weak_ptr doesn't have use_count
D
shared_ptr doesn't have use_count
29

Question 29

What is the difference between smart pointer composition and inheritance?

A
Composition uses smart pointers as members for resource management, inheritance creates derived classes with smart pointer behavior
B
They are identical design patterns
C
Inheritance uses smart pointers as members
D
Composition creates derived classes
30

Question 30

What is the difference between unique_ptr null assignment and reset?

cpp
std::unique_ptr<int> ptr = std::make_unique<int>(42);
ptr = nullptr; // Delete object, set to null

ptr.reset(); // Same effect: delete object, set to null
A
Both delete the current object and set pointer to nullptr, but assignment provides more explicit intent for nullification
B
They have different effects
C
Assignment doesn't delete objects
D
reset() sets to null without deleting
31

Question 31

What is the difference between shared_ptr make_shared and constructor?

cpp
auto ptr1 = std::make_shared<int>(42); // One allocation

auto ptr2 = std::shared_ptr<int>(new int(42)); // Two allocations
A
make_shared allocates object and control block together for efficiency, constructor separates allocations which can cause memory fragmentation
B
They are identical in allocation strategy
C
Constructor is more efficient
D
make_shared requires two allocations
32

Question 32

What is the difference between weak_ptr and observer_ptr?

A
weak_ptr works with shared ownership and reference counting, observer_ptr is a non-owning raw pointer wrapper without ownership semantics
B
They are identical classes
C
observer_ptr is part of standard C++
D
weak_ptr doesn't own objects
33

Question 33

What is the difference between smart pointer move and copy?

cpp
std::unique_ptr<int> ptr1 = std::make_unique<int>(42);
std::unique_ptr<int> ptr2 = std::move(ptr1); // Transfer ownership

std::shared_ptr<int> sptr1 = std::make_shared<int>(42);
std::shared_ptr<int> sptr2 = sptr1; // Share ownership
A
Move transfers ownership (unique_ptr), copy shares ownership (shared_ptr) by incrementing reference count
B
They are identical operations
C
Copy transfers ownership
D
Move shares ownership
34

Question 34

What is the difference between unique_ptr and optional?

A
unique_ptr manages dynamic objects with custom deleters, optional manages objects on stack with empty state indicating absence
B
They are identical containers
C
optional manages dynamic objects
D
unique_ptr can be empty without allocation
35

Question 35

What is the difference between shared_ptr atomic operations and mutex protection?

A
Atomic operations provide lock-free reference counting, mutex protection requires OS-level synchronization for thread safety
B
They are identical synchronization methods
C
Mutex protection is faster
D
Atomic operations require mutexes
36

Question 36

What is the difference between weak_ptr and dangling pointer detection?

A
weak_ptr provides built-in dangling detection through lock() returning empty shared_ptr, raw pointers require external tracking
B
They are identical safety mechanisms
C
Raw pointers provide dangling detection
D
weak_ptr can dangle without detection
37

Question 37

What is the difference between smart pointer inheritance and polymorphism?

cpp
std::shared_ptr<Base> ptr = std::make_shared<Derived>();
// Polymorphic access through base class pointer
A
Smart pointers support polymorphism through base class pointers, inheritance defines the class hierarchy that enables polymorphic behavior
B
They are identical concepts
C
Inheritance requires smart pointers
D
Smart pointers define class hierarchies
38

Question 38

What is the difference between unique_ptr release and reset?

cpp
std::unique_ptr<int> ptr = std::make_unique<int>(42);
int* raw = ptr.release(); // Release ownership, get raw pointer
// Must manually delete raw

delete raw;

ptr.reset(); // Delete owned object, set to nullptr
A
release() transfers ownership to caller (returns raw pointer), reset() deletes owned object and sets pointer to nullptr
B
They are identical operations
C
reset() transfers ownership
D
release() deletes the object
39

Question 39

What is the difference between shared_ptr and reference counting overhead?

A
shared_ptr implements reference counting for automatic cleanup, overhead includes atomic operations for thread safety and control block management
B
They are identical concepts
C
Reference counting has no overhead
D
shared_ptr doesn't use reference counting
40

Question 40

What are the fundamental principles for effective smart pointer usage in C++?

A
Use unique_ptr for exclusive ownership, shared_ptr for shared ownership, weak_ptr to break cycles, prefer make_* functions, avoid raw pointers for ownership, and understand ownership semantics clearly
B
Never use smart pointers in C++
C
Use raw pointers for all memory management
D
Mix smart and raw pointers freely

QUIZZES IN C++