ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
struct A { A() { std::cout << "A "; } };
struct B { B() { std::cout << "B "; } };
struct C : A, B {
C() { std::cout << "C "; }
};
int main() { C c; }- A. B A C
- B. C A B
- C. A C B
- D. A B C
Correct answer: D. A B C
With multiple inheritance, base classes construct in the order they are LISTED in the derived class's inheritance list (A, B), not the order their constructors happen to appear or anything related to the member-initializer list's order. So A constructs, then B, then C's own body.
More OOP with C++ and Java questions
- What is printed? java class A { A() { System.out.print("A "); init(); } void init() { System.out.print("A.init "); } } class B extends A {…
- What is printed? java public int test() { try { return 1; } finally { System.out.println("finally"); } } // caller…
- What is printed? java public int test() { try { return 1; } finally { return 2; } } // caller prints test()
- What is printed? java class MyChecked extends Exception {} class Test { static void risky() throws MyChecked { throw new MyChecked(); }…
- What is printed? java try { try { throw new RuntimeException("inner"); } finally { System.out.print("inner-finally "); } } catch…
- What is printed? java class A { } class B extends A { } class Test { public static void main(String[] args) { A a = new B(); try { B b =…