ProgrammingVery hardWipro · Accenture · Capgemini pattern
What does the following Python 3 code print? def outer(): x = [10] def inner(): x[0] = 20 inner() return x print(outer())
- A. [10]
- B. [10, 20]
- C. []
- D. [20]
Correct answer: D. [20]
Mutating x[0] through the shared list object changes the underlying data itself, unlike rebinding a plain variable; no nonlocal is needed for mutation.
More Python Programming questions
- What does the following Python 3 code print? x = 100 def f(): print(x) x = 5 try: f() except UnboundLocalError: print("UnboundLocalError")
- What does the following Python 3 code print? print("%d-%s-%.2f" % (5, "hi", 3.14159))
- What does the following Python 3 code print? print(list(filter(lambda x: x % 2 == 0, range(10))))
- What is printed? python print(type(10 / 5))
- Which of these built-in Python types is immutable?
- What is printed? python s = "placement" print(s[2:5])