ProgrammingVery hardInfosys DSE · Wipro Elite

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

What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct T {
    T() { cout << "d"; }
    T(const T&) { cout << "c"; }
    T& operator=(const T&) { cout << "a"; return *this; }
};
int main() { T x; T y = x; T z; z = y; T w(z); }
  1. A. dcdaa
  2. B. dcdac
  3. C. ddcac
  4. D. dcdcc

Correct answer: B. dcdac

T y = x and T w(z) are copy-constructions (c); z = y is copy assignment (a); x and z use the default constructor (d): d c d a c.

More OOP with C++ and Java questions

Exams that ask this topic