CS FundamentalsHardProduct-company placement · Python

What does this Template Method code print?

What does this Template Method code print?
class Base:
    def run(self): return self.a() + self.b()
    def a(self): return 'A'
    def b(self): return 'B'
class Child(Base):
    def a(self): return 'a' + super().a()
    def b(self): return 'b'
class Grand(Child):
    def b(self): return super().b() * 2

print(Grand().run())
  1. A. aAB
  2. B. aAbb
  3. C. AbB
  4. D. bbaA

Correct answer: B. aAbb

run is inherited, but self is a Grand. a resolves to Child.a, giving 'a' + 'A'; b resolves to Grand.b, which calls Child.b ('b') and doubles it. Result: 'aA' + 'bb'.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic