ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
int main() {
    unique_ptr<int> p = make_unique<int>(7);
    unique_ptr<int> q = std::move(p);
    cout << (p == nullptr) << *q;
}
  1. A. 17
  2. B. 07
  3. C. Compile-time error: unique_ptr cannot be moved
  4. D. 1 then undefined behaviour

Correct answer: A. 17

unique_ptr cannot be copied but can be moved; moving transfers ownership and the source is guaranteed to become null. q now owns the int 7.

More OOP with C++ and Java questions

Exams that ask this topic