ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? def mk(): c = 0 def inc(): nonlocal c c += 2 return c return inc a, b = mk(), mk() for _ in range(5): a() print(a(), b())
- A. 12 2
- B. 12 10
- C. 12 14
- D. 2 2
Correct answer: A. 12 2
Each call to mk() creates a separate closure cell, so a and b keep independent counters. Output: 12 2.
More Python Programming questions
- What does the following Python 3 code print? a, b, c = 25, 3, 17 print((a << b) ^ c, a & ~c, (a | c) >> b)
- What does the following Python 3 code print? def f(): try: return 7 finally: print("f", end=" ") x = f() + 13 print(x)
- What does the following Python 3 code print? a, b = [3,1,8,5,4,7], [7,4,2,5] print(list(map(lambda p: p[0] p[1], zip(a, b)))…
- What does the following Python 3 code print? d = {} for w in ["kheer","ghee","bun","nut","cake","jam"]: d[w[0]] = d.get(w[0], 0) + 1…
- What does the following Python 3 code print? a, b = 18, 5 print(a // b, a % b, -a // b, -a % b)
- What does the following Python 3 code print? a = [[0] 2] 2 a[0][0] = 7 print(a)