ProgrammingHardAccenture pattern
What is printed?
What is printed?
def f(x, y=10):
def g():
return x + y
y = 20
return g()
print(f(1))- A. 11
- B. 21
- C. 31
- D. TypeError
Correct answer: B. 21
g closes over the enclosing variable y, looked up when g() is actually CALLED, by which time y has been reassigned to 20. So g() computes x + y = 1 + 20 = 21.
More Python Programming questions
- What is printed? python nums = [1, 2, 3] doubled = list(map(lambda x: x 2, nums)) evens = list(filter(lambda x: x % 2 == 0, nums))…
- What is printed? python class A: def call(self, x): return x 2 a = A() print(a(5), callable(a))
- What is printed? python class Node: def init(self, val, children=None): self.val = val self.children = children if children is not None…
- What does the following Python 3 code print? lst = [x x for x in range(5)] gen = (x x for x in range(5)) print(type(lst), type(gen))
- What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)
- What does the following Python 3 code print? a = [1, 2, 3] b = a[:] b.append(4) print(a, b)