ProgrammingHardCognizant GenC · Python
What is printed?
What is printed?
def mk():
n = 0
def inc():
nonlocal n
n += 1
return n
return inc
a, b = mk(), mk()
print(a(), a(), b(), a())- A. 1 2 1 3
- B. 1 2 3 4
- C. 1 1 1 1
- D. 1 2 2 3
Correct answer: A. 1 2 1 3
Each mk() call creates its own cell for n; a and b count independently, so a gives 1, 2, then 3 while b gives 1.
More Python Programming questions
- What is printed? python class C: v = [] n = 0 def add(self, x): self.v.append(x) self.n += 1 a, b = C(), C() a.add(1); b.add(2); b.add(3)…
- What is printed? python print(1 < 2 < 3, 3 > 2 > 2, 1 == 1 in [1], not 1 == 2, 2 3 2)
- What is printed? python a = [[0] 2] 2 a[0][0] = 1 b = [[0] 2 for in range(2)] b[0][0] = 1 print(a, b)
- What is printed? python def f(n): try: if n == 0: raise ValueError return 10 // (n - 1) except ZeroDivisionError: return -1 except…
- What is printed? python a, b, c = range(5) x, (y, z) = 1, 'hey' print(a, b, c, x, y, z)
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2), c['z'], sorted(c.items())[:2])