ProgrammingHardAccenture pattern
What is printed?
What is printed?
def outer():
data = [1, 2, 3]
def modify():
data.append(4)
modify()
return data
print(outer())- A. [1, 2, 3]
- B. [1, 2, 3, 4]
- C. UnboundLocalError
- D. [4]
Correct answer: B. [1, 2, 3, 4]
modify mutates the enclosing list in place via .append (no reassignment of the name data is needed, so no nonlocal declaration is required). The closure's append is visible when outer returns data.
More Python Programming questions
- What is printed? python a, b = 1, 2 a, b = b, a + b print(a, b)
- What is printed? python from dataclasses import dataclass @dataclass class Point: x: int y: int = 0 p1 = Point(1, 2) p2 = Point(1, 2)…
- What is printed? python x, y = "hello" print(x, y)
- What is printed? python print([x for x in range(20) if x % 2 == 0 if x % 3 == 0])
- What is printed? python print([x if x % 2 == 0 else -x for x in range(5)])
- What is printed? python words = ["banana", "kiwi", "apple", "fig"] print(sorted(words, key=len)) print(sorted(words, key=lambda w: w[-1]))