ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    def __repr__(self): return "A()"
class B(A):
    def __str__(self): return "B!"
print(str(A()), [B()], f"{B()}", "%s %r" % (B(), B()))
  1. A. A() [B()] B! B! A()
  2. B. A() [A()] A() B! A()
  3. C. A() [A()] B! B! A()
  4. D. A() [A()] B! A() A()

Correct answer: C. A() [A()] B! B! A()

str() falls back to __repr__ when __str__ is missing. Containers show their items with repr, so [B()] prints A() (inherited __repr__). f-strings and %s use str, %r uses repr.

More Python Programming questions

Exams that ask this topic