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);
}- A. Base*
- B. Derived*
- C. Compile-time error: call to f(dp) is ambiguous
- 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
- What is printed? cpp struct Shape { virtual double area() const = 0; virtual ~Shape() = default; }; struct Square : Shape { double s…
- What is printed? java abstract class Shape { abstract double area(); public String toString() { return "Shape(area=" + area() + ")"; } }…
- What is printed? java class Vehicle { void start() { System.out.print("Vehicle-start "); honk(); } void honk() { System.out.print("beep")…
- What is printed? cpp struct Base { virtual void f() const { cout << "Base "; } }; struct Derived : Base { void f() const override { cout…
- What is printed? cpp struct Base { virtual Base clone() const { cout << "Base::clone "; return new Base(this); } }; struct Derived : Base…
- What is printed? java interface Greeter { String greet(); } class Main { public static void main(String[] args) { Greeter g = () -> "hi…