ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
import functools @functools.singledispatch def show(x): return "obj" @show.register def _(x: int): return "int" @show.register def _(x: list): return "list" print(show(1), show(True), show([1]), show(1.5), show((1,)))
- A. int int list obj obj
- B. int obj list obj list
- C. int int list int obj
- D. int bool list obj obj
Correct answer: A. int int list obj obj
singledispatch dispatches on the type of the first argument using the MRO, so bool (a subclass of int) uses the int version. float and tuple have no registration and fall back to the base function.
More Python Programming questions
- What is printed? python def f(): x = 0 def inc(): nonlocal x x += 1 return x return [inc() for in range(3)], x print(f())
- What is printed? python def f(kw): return list(kw) print(f(b=1, a=2), f(dict(z=0), y=1), f({"1": 1}))
- What is printed? python class A: def getitem(self, k): return k a = A() print(a[1], a[1:3], a[::2], a[1, 2:3], a[...])
- What is printed? python print([int(t, 0) for t in " 12000 t-0b101 +7 ".split()])
- What is printed? python import sys print(sys.getintmaxstrdigits()) try: s = str(10 4300) except ValueError: print("too long")
- What is printed? python print(len(str(2 1000 5 1000)))