ProgrammingVery hardWipro Elite · TCS NQT Prime

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 C {
    static int n; int id;
    C() : id(++n) {}
    ~C() { cout << id; }
};
int C::n = 0;
int main() {
    C a;
    { C b; C c; }
    C d;
    cout << "|";
}
  1. A. 23|14
  2. B. 32|14
  3. C. 23|41
  4. D. 32|41

Correct answer: D. 32|41

The inner block ends first, destroying c(3) then b(2); after the bar, d(4) and a(1) are destroyed in reverse order of construction.

More OOP with C++ and Java questions

Exams that ask this topic