ProgrammingVery hardTCS NQT Digital · Accenture Advanced

What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std)

What is printed? (no move constructor is declared) (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct T {
    T() { cout << "d"; }
    T(const T&) { cout << "c"; }
};
T g(T t) { return t; }
int main() { T x; T y = g(x); }
  1. A. dc
  2. B. dccc
  3. C. dcc
  4. D. dd

Correct answer: C. dcc

x is default-built (d); the by-value parameter is copied (c); returning the parameter copies it into the result object, which is y itself in C++17 (c).

More OOP with C++ and Java questions

Exams that ask this topic