ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
class Base:
    def run(self): return self.step()
    def step(self): return "base"
class Child(Base):
    def step(self): return "child"
class Grand(Child):
    def run(self): return "G" + super().run()
print(Grand().run(), Base.run(Child()))
  1. A. Gbase base
  2. B. Gchild base
  3. C. Gbase child
  4. D. Gchild child

Correct answer: D. Gchild child

Base.run calls self.step(), which is resolved on the actual object (dynamic dispatch). For a Grand or a Child instance that is Child.step, even when run is invoked through Base.

More Python Programming questions

Exams that ask this topic