ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS
What is printed (C++17)?
What is printed (C++17)?
struct T {
T() { }
T(const T&) { cout << "C"; }
T(T&&) { cout << "M"; }
};
int main() {
const T a;
T b = std::move(a);
T c = std::move(b);
T d = T();
}- A. MMM
- B. CMM
- C. MM
- D. CM
Correct answer: D. CM
std::move(a) yields a const T&&, which cannot bind to T&&, so the copy constructor is chosen (C). std::move(b) moves (M). T d = T() is a prvalue initialising d directly, so nothing is printed.
More OOP with C++ and Java questions
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } }; struct W { T t; }; int main() { W a; W b = std::move(a); }
- What is printed? cpp struct A { virtual void f() = 0; }; void A::f() { cout << "A"; } struct B : A { void f() override { A::f(); cout <<…
- What is printed? cpp struct B { virtual ~B() { } }; struct D1 : B { }; struct D2 : B { }; int main() { B p = new D1; cout <<…
- What is printed? cpp struct B { }; // no virtual functions struct D : B { }; struct VB { virtual ~VB() { } }; struct VD : VB { }; int…
- What is printed (C++17)? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); auto g = f; f(); cout << f() << g() <<…
- What is printed? cpp struct B { }; struct D : B { }; void f(B&) { cout << "B"; } void f(D&) { cout << "D"; } int main() { D d; B& b = d…