1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Which statement about std::unique_ptr is correct?

Added 30 Nov -0001

Which statement about std::unique_ptr is correct?

  1. It is copyable, and copying it duplicates the object with a fresh allocation
  2. It is move-only: copying is deleted, and ownership is transferred with std::move Correct answer
  3. It may be copied, but both pointers then share ownership through a hidden reference count
  4. It requires a reference count stored in a control block allocated beside the managed object

Correct answer

B. It is move-only: copying is deleted, and ownership is transferred with std::move

15 views

MCQSEXAM

Explanation

std::unique_ptr models exclusive ownership with no overhead over a raw pointer, so its copy operations are deleted and only moves exist, typically produced by std::make_unique in C++14. Returning one by value relies on move semantics, which is why factory functions return unique_ptr freely. Shared ownership needs std::shared_ptr and its reference-counted control block.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? 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