ProgrammingModerateMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct T {
    T() { }
    T(const T&)            { cout << "C"; }
    T& operator=(const T&) { cout << "A"; return *this; }
};
int main() {
    T a;
    T b = a;
    T c(a);
    c = b;
    T d{a};
}
  1. A. CCAC
  2. B. ACAC
  3. C. CCAA
  4. D. CCCA

Correct answer: A. CCAC

T b = a, T c(a) and T d{a} all create new objects, so they call the copy constructor. Only c = b assigns to an existing object, calling operator=.

More OOP with C++ and Java questions

Exams that ask this topic