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();
}
  1. A. MMM
  2. B. CMM
  3. C. MM
  4. 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

Exams that ask this topic