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()))- A. A() [B()] B! B! A()
- B. A() [A()] A() B! A()
- C. A() [A()] B! B! A()
- 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
- What is printed? python x = 10 def f(x, y=x): x += 1 return x, y x = 20 print(f(5), f(y=1, x=2), x)
- What is printed? python for i in range(3): if i == 5: break else: print("else", i, end=" ") n = 0 while n < 3: n += 1 if n == 2: break…
- What is printed? python def find(xs, t): for i, x in enumerate(xs): if x == t: return i else: return -1 return "unreachable"…
- What is printed? python def kind(v): match v: case 0 | 1: return "bit" case int(n) if n < 0: return "neg" case [x, rest]: return…
- What is printed? python RED = "r" def f(c): match c: case RED: return "red:" + RED print(f("g"), RED)
- What is printed? python data = [3, 8, 2, 9, 4] if (n := len(data)) > 4: print(n, end=" ") print([y for x in data if (y := x 2) > 10], y)