ProgrammingVery hardHCL · Tech Mahindra pattern
What is printed?
What is printed?
class Base {
public:
void info() { cout << "Base::info "; helper(); }
void helper() { cout << "Base::helper"; }
};
class Derived : public Base {
public:
void helper() { cout << "Derived::helper"; }
};
int main() {
Derived d;
d.info();
}- A. Base::info Derived::helper
- B. Base::info Base::helper
- C. Derived::info Derived::helper
- D. Compilation error: helper redefinition requires override
Correct answer: B. Base::info Base::helper
Neither info() nor helper() is virtual, so even though info() is called on a Derived object, the unqualified call to helper() inside Base::info resolves at compile time to Base::helper (the one visible in Base's own scope), not Derived::helper.
More OOP with C++ and Java questions
- What is printed? cpp struct A { int v = 1; virtual void show() { cout << v; } }; struct B : A { int v = 2; void show() override { cout <<…
- What is printed? cpp struct Base { virtual void f() { cout << "Base-f "; g(); } void g() { cout << "Base-g"; } }; struct Derived : Base {…
- What is printed? java class A { static { System.out.print("A-static "); } } class B extends A { static { System.out.print("B-static "); }…
- What is printed? java class Parent { int bonus() { return 10; } } class Child extends Parent { static int bonus(int n) { return n + 1; } }…
- What is printed? java class A { { System.out.print("IB-A "); } A() { System.out.print("CTOR-A "); } } class B extends A { {…
- What is printed? cpp void f(int x) { cout << "int"; } void f(double x) { cout << "double"; } int main() { f('a'); }