ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class Base:
    def __repr__(self):
        return "Base"
class Sub(Base):
    pass
print([Sub(), Sub()])
  1. A. [Sub, Sub]
  2. B. [Base, Base]
  3. C. TypeError
  4. D. [<Sub object>, <Sub object>]

Correct answer: B. [Base, Base]

Sub does not define its own __repr__, so it inherits Base.__repr__, which always returns the literal string 'Base' regardless of the actual subclass, so the list prints [Base, Base].

More Python Programming questions

Exams that ask this topic