ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
int main() {
auto a = make_shared<int>(1);
auto b = a;
{ auto c = b; cout << a.use_count(); }
weak_ptr<int> w = a;
cout << a.use_count();
b.reset();
cout << w.use_count();
}- A. 332
- B. 331
- C. 321
- D. 322
Correct answer: C. 321
Inside the block a, b and c share ownership (3). After it c is gone (2), and a weak_ptr does not add to use_count. b.reset() leaves a as the only owner, so w.use_count() is 1.
More OOP with C++ and Java questions
- What is printed? cpp void f(int) { cout << "i"; } void f(char) { cout << "c"; } void f(double) { cout << "d"; } int main() { char a = 'a'…
- Which statement about this code is correct? cpp struct B { virtual void f() const { } }; struct D : B { void f() override { } };
- What is printed? cpp struct B { int x = 5; }; struct D : B { int x = 7; }; int main() { D d; B& b = d; cout << d.x << b.x << d.B::x; }
- What is printed? java class P { String f = "P"; static String s() { return "P"; } String i() { return "P"; } } class C extends P { String…
- What is printed? java // static methods of the main class: static void f(long x) { System.out.print("L"); } static void f(Integer x) {…
- What is printed? java String x = "a"; final String y = "a"; System.out.print(("ab" == x + "b") + " "); System.out.print(("ab" == y + "b")…