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();
}- A. 00
- B. 11
- C. 01
- 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
- What is printed? cpp struct P { int a, b; P() : P(1) { cout << "x"; } P(int a) : P(a, 2) { cout << "y"; } P(int a, int b) : a(a), b(b) {…
- Which lines fail to compile? cpp struct F { explicit operator bool() const { return true; } }; int main() { F f; if (f) cout << "Y"; // L1…
- What is printed? cpp struct M { operator int() const { return 7; } }; int main() { M m; cout << m + 1; }
- What is printed? cpp // C++17 struct Counter { int n = 0; int operator()() { return ++n; } }; int main() { Counter c; c(); c(); Counter d…
- Which line fails to compile? cpp class B { public: void f() { cout << "f"; } void g() { cout << "g"; } }; class D : private B { public…
- What is printed? cpp struct A { void hi() { cout << "A"; } }; struct B { void hi(int) { cout << "B"; } }; struct C : A, B { }; int main()…