ProgrammingHardAccenture pattern
What is printed?
What is printed?
x = None print(x or "default") y = 0 print(y or "default") z = "" print(z or "fallback")
- A. None 0 True
- B. default default fallback
- C. default 0 fallback
- D. None default fallback
Correct answer: B. default default fallback
or returns its first truthy operand, or the last one if both are falsy. None, 0, and '' are all falsy, so each expression falls through to the fallback string on the right.
More Python Programming questions
- What is printed? python def gen(): try: x = yield 1 except GeneratorExit: print("closing") raise g = gen() next(g) g.close()
- What is printed? python a = 5 def outer(): x = 10 def inner(): nonlocal x x += a return x return inner() print(outer())
- What is printed? python class Temperature: def init(self, celsius=0): self.celsius = celsius @property def fahrenheit(self): return…
- What is printed? python def f(a, b, , c=10, kwargs): return a + b + c + sum(kwargs.values()) print(f(1, 2, d=3, e=4)) print(f(1, 2, c=100))
- What is printed? python class Animal: sound = "..." def makesound(self): return f"{type(self).name} says {self.sound}" class Dog(Animal)…
- What is printed? python def outer(): x = [] def appendVal(v): x.append(v) return x return appendVal f1 = outer() f2 = outer() print(f1(1)…