ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
struct Base {
Base() { cout << "Base "; }
};
struct Mixin1 {
Mixin1() { cout << "Mixin1 "; }
};
struct Mixin2 {
Mixin2() { cout << "Mixin2 "; }
};
struct Combo : Base, Mixin1, Mixin2 {
Combo() : Mixin2(), Mixin1(), Base() { cout << "Combo "; }
};
int main() { Combo c; }- A. Mixin2 Mixin1 Base Combo
- B. Base Mixin1 Mixin2 Combo
- C. Combo Base Mixin1 Mixin2
- D. Base Mixin2 Mixin1 Combo
Correct answer: B. Base Mixin1 Mixin2 Combo
Base classes always construct in the order they are declared in the class's inheritance list (Base, Mixin1, Mixin2), never in the order written in the member-initializer list; most compilers warn about the mismatched initializer-list order shown here but it still compiles this way.
More OOP with C++ and Java questions
- What is printed? java class Base { public String describe() { return "Base"; } } interface Describable { String describe(); } class Sub…
- What is printed? java class Resource { String name; Resource(String n) { name = n; System.out.print("open:" + n + " "); } void use() {…
- What is printed? cpp class Base { public: Base() { cout << "Base() "; } Base(int x) : Base() { cout << "Base(int) "; } }; int main() {…
- What is printed? java class Counter { int value; Counter(int v) { value = v; } Counter increment() { return new Counter(value + 1); }…
- What is printed? cpp struct Animal { virtual ~Animal() {} virtual void speak() const { cout << "..."; } }; struct Dog : Animal { void…
- What is printed? java class A { A() { System.out.print(id() + " "); } int id() { return 0; } } class B extends A { int val; B(int v) { val…