ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
int main() {
    weak_ptr<int> w;
    {
        auto s = make_shared<int>(1);
        w = s;
        cout << w.expired();
    }
    cout << w.expired();
}
  1. A. 00
  2. B. 11
  3. C. 01
  4. D. 10

Correct answer: C. 01

A weak_ptr observes without owning. While s exists the object is alive (expired() is false = 0); when the last shared_ptr dies the object is destroyed and the weak_ptr becomes expired (1).

More OOP with C++ and Java questions

Exams that ask this topic