ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def counter():
count = 0
def step():
nonlocal count
count += 2
return count
return step
c1 = counter()
c2 = counter()
print(c1(), c1(), c2())- A. 1 2 1
- B. 2 4 2
- C. 2 2 2
- D. 2 4 4
Correct answer: B. 2 4 2
Each call to counter creates its own count. c1 is called twice, giving 2 and then 4. c2 has its own count and returns 2 on the first call.
More Python Programming questions
- What is printed? python values = {'x': [1, 2], 'y': [3]} total = sum(len(v) for v in values.values()) keys = sorted(values, key=lambda k…
- What is printed? python print(int(' -0012 '), int('+5'), float('-.5'), float('5.'))
- What is printed? python x = 10 y = x y += 1 z = [x] z[0] += 5 print(x, y, z)
- What is printed? python a = list('abc') print(a.index('b'), ''.join(a[::-1]), a.pop(0), a)
- What is printed? python x = 5 print(f'{x=}', f'{x 2 = }', f'{"a" 2!r}')
- What is printed? python from math import ceil print(ceil(7 / 2), -(-7 // 2), (7 + 2 - 1) // 2)