ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class K:
n = 3
a = [i for i in range(n)]
try:
b = [n for i in range(2)]
except NameError:
b = "NE"
print(K.a, K.b)- A. NE NE
- B. [0, 1, 2] [3, 3]
- C. [0, 1, 2] NE
- D. [0, 1, 2] []
Correct answer: C. [0, 1, 2] NE
The outermost iterable range(n) is evaluated in the class scope, so it works. But n used inside the comprehension body is looked up through the function-like scope, which skips the class body, so it raises NameError.
More Python Programming questions
- What is printed? python total = 0 squares = [total := total + v for v in [1, 2, 3]] print(squares, total)
- What is printed? python print([[r c for c in range(r)] for r in range(1, 4)])
- What is printed? python pairs = [(i, j) for i in range(3) if i for j in range(i) if j != 1] print(pairs)
- What is printed? python i = "outer" r = [i for i in range(2)] s = {i: i for i in "ab"} for i in range(5, 6): pass print(i, r, s)
- What is printed? python s = "programming" print(s[-3:-8:-2], s[100:], s[-100:3])
- What is printed? python a = list(range(6)) try: a[::2] = [9, 9] except ValueError as e: print("VE", end=" ") a[1:2] = [] a[:0] = [7, 7]…