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();
}- A. 321
- B. 333
- C. 323
- 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
- What is printed? cpp int main() { weakptr<int> w; { auto s = makeshared<int>(1); w = s; cout << w.expired(); } cout << w.expired(); }
- 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…