🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: C++ Classes and Objects: all 10 questions

Quiz: C++ Classes and Objects · Question 6 of 10

What does this print? (code)

What does this print?

#include <iostream>
using namespace std;
struct T {
    int id;
    T(int i) : id(i) { cout << "C" << id; }
    ~T() { cout << "D" << id; }
};
int main() {
    T a(1);
    T b(2);
    return 0;
}
  1. A.C1C2D1D2
  2. B.C1C2D2D1
  3. C.C1D1C2D2
  4. D.C2C1D1D2
Answer & explanation

Correct answer: C1C2D2D1

Local objects are destroyed in reverse order of construction, so b is destroyed before a.

Can you get the rest right without peeking?

Take the timed Quiz: C++ Classes and Objects quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →