ProgrammingModerateGATE CS · TCS NQT

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

What is printed? (C++, assume <iostream> and using namespace std)
struct A {
    A()          { cout << "c"; }
    A(const A&)  { cout << "k"; }
    ~A()         { cout << "d"; }
};
void f(A a) { }
int main() { A x; f(x); }
  1. A. ckdd
  2. B. cdd
  3. C. ckd
  4. D. cckdd

Correct answer: A. ckdd

x is built (c); passing it by value invokes the copy constructor (k); the parameter is destroyed at the end of f (d) and x at the end of main (d).

More OOP with C++ and Java questions

Exams that ask this topic