LearnFrenzy
Sign in Take a free test

Smart Pointers · Exercise 1 of 4

Smart Pointers Smart Pointers MCQ

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Twenty multiple-choice questions on C++ smart pointers: unique_ptr, shared_ptr, weak_ptr, make_unique and make_shared, reference counting, cycles, custom deleters and passing conventions. Pick an option to check your answer instantly.

Question 1 Easy

What is the main purpose of smart pointers?

A Faster pointer arithmetic
B Automatically freeing dynamically allocated objects, preventing leaks and double deletes
C Pointing to functions
D Replacing references
Question page & discussion
Question 2 Easy

What happens with auto b = a; when a is a std::unique_ptr?

A Both point to the object
B Compile error – unique_ptr is move-only; use std::move(a)
C The object is copied
D a is deleted
Question page & discussion
Question 3 Easy

After auto q = std::move(p); with unique_ptrs, what is p?

A Still owns the object
B Empty (nullptr)
C Dangling
D A copy of q
Question page & discussion
Question 4 Easy

When is the object managed by shared_ptrs deleted?

A When the first shared_ptr is destroyed
B When the last shared_ptr owning it is destroyed or reset
C At program end only
D When a weak_ptr expires
Question page & discussion
Question 5 Easy

Does creating a std::weak_ptr from a shared_ptr increase use_count()?

A Yes, by one
B No – weak_ptr does not own the object; it only increases the separate weak count
C Yes, by two
D Only if locked
Question page & discussion
Question 6 Medium

What does w.lock() return when the object has already been destroyed?

A A dangling pointer
B An empty shared_ptr
C It throws
D The last value
Question page & discussion
Question 7 Medium

A parent holds shared_ptr to its child and the child holds shared_ptr to its parent. How do you avoid the leak?

A Call delete on both
B Make the child's link to the parent a weak_ptr (or a raw observer pointer)
C Use unique_ptr for both
D Increase use_count
Question page & discussion
Question 8 Medium

What is an advantage of std::make_shared<T>() over std::shared_ptr<T>(new T)?

A It allows copying unique_ptr
B One allocation for the object and the control block, and better exception safety
C It avoids reference counting
D It works for arrays only
Question page & discussion
Question 9 Medium

What is the difference between p.release() and p.reset() for a unique_ptr?

A None
B release() gives up ownership without deleting and returns the raw pointer; reset() deletes the owned object
C reset() returns the pointer
D release() deletes the object
Question page & discussion
Question 10 Medium

What happens if you delete p.get(); for a unique_ptr p?

A Nothing special
B Double delete (undefined behaviour) when p later deletes the same object
C p becomes null
D Compile error
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links