ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
struct Base {};
struct Derived : Base {};
void f(Base *b) { cout << "Base*"; }
void f(Derived *d) { cout << "Derived*"; }
int main() {
    Derived *dp = new Derived();
    f(dp);
}
  1. A. Base*
  2. B. Derived*
  3. C. Compile-time error: call to f(dp) is ambiguous
  4. D. Compile-time error: f cannot be overloaded on pointer type alone

Correct answer: B. Derived*

dp is exactly of type Derived*, an exact match for f(Derived*). Calling f(Base*) would require a derived-to-base pointer conversion, a lower-ranked match. Overload resolution always prefers the exact match, so f(Derived*) is chosen; there is no ambiguity.

More OOP with C++ and Java questions

Exams that ask this topic