ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class Employee {
    double bonus() { return 1000; }
    double pay() { return 50000 + bonus(); }
}
class Manager extends Employee {
    @Override
    double bonus() { return 5000; }
}
// in main:
Employee e = new Manager();
System.out.println(e.pay());
  1. A. 51000.0
  2. B. 55000.0
  3. C. Compile-time error: pay() cannot see Manager's bonus()
  4. D. 50000.0

Correct answer: B. 55000.0

pay() is inherited by Manager unchanged, but its call to bonus() is dynamically dispatched against the real object. Since e is actually a Manager, bonus() returns 5000, making pay() return 50000 + 5000 = 55000.0.

More OOP with C++ and Java questions

Exams that ask this topic