ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class Base:
def __repr__(self):
return "Base"
class Sub(Base):
pass
print([Sub(), Sub()])- A. [Sub, Sub]
- B. [Base, Base]
- C. TypeError
- 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
- What is printed? python class CM3: def enter(self): return 42 def exit(self, et, ev, tb): return False with CM3() as val: print(val)
- What is printed? python from contextlib import contextmanager @contextmanager def cm(): print("setup") yield 1 print("teardown") with cm()…
- What is printed? python class A: def enter(self): print("A enter"); return self def exit(self, a): print("A exit") class B: def…
- What is printed? python class A: def who(self): return "A" class B(A): def who(self): return "B" class C(A): def who(self): return "C"…
- What is printed? python class A: def f(self): return 1 class B(A): def f(self): return 2 + super().f() class C(B): def f(self): return 3 +…
- What is printed? python class F: @staticmethod def add(a, b): return a + b @classmethod def create(cls): return cls() print(F.add(2, 3)) f…