ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class K:
def __new__(cls):
print("new", end=" ")
return 42
def __init__(self):
print("init", end=" ")
r = K()
print(r)- A. new init 42
- B. new init <__main__.K object>
- C. new 42
- D. init 42
Correct answer: C. new 42
__init__ is called only if __new__ returns an instance of the class. Here __new__ returns the int 42, so __init__ is skipped and K() evaluates to 42.
More Python Programming questions
- What is printed? python def f(args): args += (99,) return args t = (1, 2) print(f(t), t, f(t))
- What is printed? python first, mid, last = "abcde" (a, b), rest = [(1, 2), 3, 4] init, = range(3) print(mid, last, a + b, rest, init)
- What is printed? python def f(a, b, args, c=0, kw): return a, b, args, c, kw print(f(1, [2, 3, 4], c=5, d=6))
- What is printed? python def f(): for i in range(3): try: return i finally: if i == 0: continue print(f())
- What is printed? python def f(): try: raise KeyError("k") finally: return "swallowed" try: print(f(), end=" ") except KeyError…
- What is printed? python try: try: 1 / 0 except ZeroDivisionError: raise ValueError("v") except ValueError as e…