1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. What ownership rule does std::unique_ptr enforce in C++?

Added 30 Nov -0001

What ownership rule does std::unique_ptr enforce in C++?

  1. Exactly one owner, with ownership transferred by move Correct answer
  2. Multiple owners, freed when the last owner releases its hold
  3. No owner, with the pointer never freed
  4. Shared ownership counted by a control block

Correct answer

A. Exactly one owner, with ownership transferred by move

14 views

MCQSEXAM

Explanation

A unique_ptr models exclusive ownership: copying is deleted, so the pointer must be transferred with std::move, and the deleter runs when that single owner is destroyed. shared_ptr, by contrast, keeps a reference count so several owners can share the same object. The unique form has no counting overhead and is the default choice when ownership is not genuinely shared.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs