ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
struct T { ~T() { cout << "d"; } };
T make() { return T(); }
int main() {
const T& r = make();
cout << "x";
}- A. dx
- B. xd
- C. dxd
- D. x
Correct answer: B. xd
Binding the temporary returned by make() to a const reference extends its lifetime to that of r, so it is destroyed at the end of main, after x is printed. In C++17 no extra copies are made.
More OOP with C++ and Java questions
- What is printed? cpp struct K { int g() { static int c = 0; return ++c; } }; int main() { K a, b; a.g(); b.g(); cout << a.g(); }
- Which line fails to compile? cpp class B { protected: int v = 1; }; class D : public B { public: int me() { return v; } // line 1 int…
- What is printed? cpp int main() { auto a = makeshared<int>(1); auto b = a; { auto c = b; cout << a.usecount(); } weakptr<int> w = a; cout…
- 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; }