ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
struct T { T() { cout << "c"; } ~T() { cout << "d"; } };
int main() {
T();
cout << "1";
const T& r = T();
cout << "2";
}- A. c1c2dd
- B. cd1cd2
- C. c1dc2d
- D. cd1c2d
Correct answer: D. cd1c2d
A temporary is destroyed at the end of the full expression that created it, so the first T dies before 1 is printed. Binding a temporary to a const reference extends its lifetime to the reference's scope, so it dies after 2.
More OOP with C++ and Java questions
- Which lines fail to compile? cpp struct U { U() { } U(const U&) = delete; }; void take(U u) { } int main() { U a; // L1 U b = a; // L2 U&…
- Which line fails to compile? cpp struct P { int v = 1; int get() { return v; } int cget() const { return v; } }; int main() { const P p{}…
- Which lines fail to compile? cpp struct S { int x = 1; static int y; static int f() { return y; } // L1 static int g() { return x; } // L2…
- Which lines fail to compile? cpp struct B { virtual void f(int) { } virtual void g() const { } void h() { } }; struct D : B { void f(int)…
- Which lines fail to compile? cpp struct B { virtual void f() final { } virtual void g() { } }; struct D : B { void f() { } // L1 void g()…
- What is printed? cpp struct B { virtual ~B() { } }; struct D : B { }; struct E : B { }; int main() { B p = new D; cout <<…