🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Multiple Inheritance and the Diamond Problem: all 10 questions

Quiz: Multiple Inheritance and the Diamond Problem · Question 7 of 10

What does this print? (code)

What does this print?

#include <iostream>
using namespace std;
struct D { D() { cout << "D"; } };
struct P : virtual D { P() { cout << "P"; } };
struct S : virtual D { S() { cout << "S"; } };
struct X : P, S { X() { cout << "X"; } };
int main() {
    X x;
    return 0;
}
  1. A.DPDSX
  2. B.DPSX
  3. C.PSDX
  4. D.PSX
Answer & explanation

Correct answer: DPSX

D is a virtual base, so it is built once, first; then P, S and finally X.

Can you get the rest right without peeking?

Take the timed Quiz: Multiple Inheritance and the Diamond Problem quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →