ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
x = 10
def outer():
x = 20
def inner():
nonlocal x
x += 1
return x
return inner() + x
print(outer(), x)- A. 41 10
- B. 42 10
- C. 42 21
- D. 31 10
Correct answer: B. 42 10
nonlocal makes inner modify outer's x (20 → 21). inner() returns 21 and is evaluated before x is read in the sum, so 21 + 21 = 42. The global x is untouched (10).
More Python Programming questions
- What is printed? python d = {} d[1] = "a" d[1.0] = "b" d[True] = "c" print(d, len(d))
- What is printed? python a = [1, 2, 3, 4, 5, 6] for x in a: if x % 2 == 0: a.remove(x) print(a)
- What is printed? python print(round(2.5), round(3.5), round(-0.5), int(-2.7), -7 // 2, -7 % 2)
- What is printed? python from collections import Counter c = Counter("mississippi") print(c.mostcommon(2), c["z"], sum(c.values()))
- What is printed? python a, b, c = range(6) x, y = y, x = 1, 2 print(a, b, c, x, y)
- What is printed? python words = ["banana", "Apple", "cherry", "apple"] print(sorted(words), sorted(words, key=str.lower)[:2], max(words…