ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
class A:
def __len__(self):
return 0
a = A()
print(bool(a))
if a:
print("truthy")
else:
print("falsy")- A. True truthy
- B. False falsy
- C. True falsy
- D. False truthy
Correct answer: B. False falsy
Without a __bool__ method, Python falls back to __len__ for truthiness: a length of 0 is treated as falsy, so bool(a) is False and the if-statement takes the else branch.
More Python Programming questions
- What is printed? python x = 10 class C: x = 20 def m(self): return x c = C() print(c.m(), C.x)
- What is printed? python def f(x, y=10): def g(): return x + y y = 20 return g() print(f(1))
- What is printed? python nums = [1, 2, 3] doubled = list(map(lambda x: x 2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums))…
- What is printed? python class A: def call(self, x): return x 2 a = A() print(a(5), callable(a))
- 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))