ProgrammingHardTCS NQT · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? def outer(): x = 10 def inner(): x = 20 return x inner() return x print(outer())
- A. None
- B. 20
- C. 0
- D. 10
Correct answer: D. 10
inner() creates its own local x=20, which shadows but never affects outer's x; outer's x remains 10 after inner() returns.
More Python Programming questions
- What does the following Python 3 code print? def outer(): x = [10] def inner(): x[0] = 20 inner() return x print(outer())
- 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?