ProgrammingHardAccenture · Cognizant pattern

What is printed?

What is printed?
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. CKDKD
  2. B. CKD
  3. C. CKDD
  4. D. CDD

Correct answer: C. CKDD

A x runs the default constructor (C). Passing x by value copy-constructs the parameter (K), which is destroyed when the call ends (D). x is destroyed at the end of main (D).

More OOP with C++ and Java questions

Exams that ask this topic