ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
class Base {
public:
virtual void op() { cout << "Base::op "; }
void callOp() { this->op(); }
};
class Derived : public Base {
public:
void op() override { cout << "Derived::op "; }
void callBaseOp() { Base::op(); }
};
int main() {
Derived d;
d.callOp();
d.callBaseOp();
}- A. Derived::op Derived::op
- B. Base::op Base::op
- C. Derived::op Base::op
- D. Base::op Derived::op
Correct answer: C. Derived::op Base::op
callOp() calls this->op() virtually, dispatching to Derived::op on a real Derived object: "Derived::op ". callBaseOp() explicitly qualifies the call as Base::op(), bypassing virtual dispatch entirely and calling the base version directly: "Base::op ".
More OOP with C++ and Java questions
- What is printed? cpp struct Base { virtual std::string name() const { return "Base"; } }; struct Derived : Base { std::string name() const…
- What is printed? java class Resource { String name; Resource(String n) { name = n; } @Override protected void finalize() {…
- What is printed? cpp class Logger { public: static int instances; Logger() { instances++; } ~Logger() { instances--; } }; int…
- What is printed? cpp struct Event { virtual void handle() const { cout << "generic-event "; } }; struct ClickEvent : Event { void handle()…
- A lambda expression captures a local variable from its enclosing method. What restriction does Java place on that variable?
- In Java, instance method calls (non-static, non-private, non-final) use dynamic dispatch based on the object's actual run-time class…