ProgrammingHardAccenture pattern
What is printed?
What is printed?
def f():
return 1, 2, 3
a, b, c = f()
print(a, b, c)
x = f()
print(type(x), x)- A. 1 2 3 <class 'list'> [1, 2, 3]
- B. 1 2 3 <class 'tuple'> (1, 2, 3)
- C. (1, 2, 3) <class 'tuple'> (1, 2, 3)
- D. 1 2 3 <class 'tuple'> (1, 2, 3, 3)
Correct answer: B. 1 2 3 <class 'tuple'> (1, 2, 3)
return 1, 2, 3 packages the values into a tuple. Unpacking it into a,b,c works normally. Without unpacking, x just receives that same tuple directly, type tuple.
More Python Programming questions
- What is printed? python d = {"a": 1, "b": 2} print({v: k for k, v in d.items()})
- What is printed? python s1 = {1, 2, 3} s2 = {2, 3, 4} print(s1 - s2, s1 | s2, s1 & s2)
- What is printed? python print(all([1, 2, 3]), all([1, 0, 3]), any([0, 0, 0]), any([0, 1, 0]))
- What is printed? python x = None print(x or "default") y = 0 print(y or "default") z = "" print(z or "fallback")
- 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())