ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
struct Base { Base(){ std::cout << "Base "; } };
struct Mem { Mem(){ std::cout << "Mem "; } };
struct Derived : Base {
Mem m;
Derived(){ std::cout << "Derived "; }
};
int main(){ Derived d; }- A. Mem Base Derived
- B. Derived Base Mem
- C. Base Mem Derived
- D. Base Derived Mem
Correct answer: C. Base Mem Derived
Base class sub-objects always construct before members, and members construct before the derived constructor's own body runs, regardless of how or whether they appear in the initializer list.
More OOP with C++ and Java questions
- What is printed? cpp struct A { virtual void f() { std::cout << "A::f "; } }; struct B : A { void f() override { std::cout << "B::f "; }…
- Which statement about this program's behaviour is true? cpp struct S { int b; int a; S() : a(1), b(a + 1) { } }; int main() { S s…
- What is printed? java class A { protected int val() { return 1; } } class B extends A { protected int val() { return 2; } } class C…
- A Java class's constructor runs this.handle = acquire(); and then later throws an exception before returning. Which statement is correct?
- What is printed? cpp struct N { int v; N(int x) : v(x) { std::cout << "N" << v << " "; } ~N() { std::cout << "~N" << v << " "; } }; int…
- What is printed? java class A { public String toString() { return "A"; } } class B extends A { public String toString() { return "B-" +…