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 A { A() { cout << "A"; } };
struct B { B() { cout << "B"; } };
struct C : B, A {
    A a; B b;
    C() { cout << "C"; }
};
int main() { C c; }
  1. A. ABABC
  2. B. ABBAC
  3. C. CBAAB
  4. D. BAABC

Correct answer: D. BAABC

Bases are built in base-list order (B, A), then members in declaration order (a, b), then the body: B A A B C.

More OOP with C++ and Java questions

Exams that ask this topic