ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class A:
def __call__(self, x):
return x * 2
a = A()
print(a(5), callable(a))- A. 10 True
- B. 5 True
- C. 10 False
- D. TypeError
Correct answer: A. 10 True
Defining __call__ makes instances of A callable like functions: a(5) invokes __call__(a, 5), returning 10. callable() reports True for any object with __call__.
More Python Programming questions
- What is printed? python class Node: def init(self, val, children=None): self.val = val self.children = children if children is not None…
- What does the following Python 3 code print? lst = [x x for x in range(5)] gen = (x x for x in range(5)) print(type(lst), type(gen))
- What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)
- What does the following Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(a, b)
- What does the following Python 3 code print? def f(a, b, args): return a, b, args print(f(1, 2, 3, 4, 5))
- What does the following Python 3 code print? def f(kwargs): return sorted(kwargs.items()) print(f(b=2, a=1, c=3))