ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
struct Base {
Base() { cout << "Base() "; }
};
struct Mixin1 {
Mixin1() { cout << "Mixin1() "; }
};
struct Mixin2 {
Mixin2() { cout << "Mixin2() "; }
};
struct Derived : Base, Mixin1, Mixin2 {
Derived() { cout << "Derived() "; }
};
int main() { Derived d; }- A. Base() Mixin1() Mixin2() Derived()
- B. Mixin1() Mixin2() Base() Derived()
- C. Derived() Base() Mixin1() Mixin2()
- D. Base() Derived() Mixin1() Mixin2()
Correct answer: A. Base() Mixin1() Mixin2() Derived()
With multiple (non-virtual) base classes, construction order follows the order bases are listed in the class's own derivation list (Base, Mixin1, Mixin2), NOT the order of any initializer list, followed last by the derived class's own constructor body.
More OOP with C++ and Java questions
- What is printed? java enum Operation { ADD { public int apply(int a, int b) { return a + b; } }, MULTIPLY { public int apply(int a, int b)…
- What is printed? cpp class Shape { public: Shape& operator=(const Shape&) = delete; Shape() = default; }; int main() { Shape a, b; a = b; }
- What is printed? java class Parent { protected int score = 50; int getScore() { return score; } } class Child extends Parent { Child() {…
- What is printed? java class Base { void greet() { System.out.print("Base "); helper(); } private void helper() {…
- What is printed? cpp struct Base { virtual void save() { cout << "BaseSave "; } }; struct Derived : Base { void save() override { cout <<…
- What is printed? java class Animal { String sound() { return "..."; } } class Dog extends Animal { @Override String sound() { return…