ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
int main() {
    auto a = make_shared<int>(5);
    auto b = a;
    { auto c = a; cout << a.use_count(); }
    cout << a.use_count();
    b.reset();
    cout << a.use_count();
}
  1. A. 321
  2. B. 333
  3. C. 323
  4. D. 211

Correct answer: A. 321

Each shared_ptr copy increments the shared reference count: a, b, c → 3. c is destroyed at the end of the block (2) and b.reset() releases b's share (1).

More OOP with C++ and Java questions

Exams that ask this topic