ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
class C:
k = 10
try:
a = [k + x for x in range(3)]
except NameError:
a = 'NameError'
print(C.a)- A. NameError
- B. [10, 11, 12]
- C. [0, 1, 2]
- D. TypeError
Correct answer: A. NameError
A comprehension has its own scope, and it cannot see names from the class body. So k is undefined inside the list comprehension and raises NameError.
More Python Programming questions
- What is printed? python a = [1, 2, 3, 4] a[1:3] = [9] print(a) b = [1, 2, 3, 4] try: b[::2] = [0] except ValueError: print('ValueError')
- What is printed? python from collections import Counter def ways(a, t): dp = Counter({0: 1}) for x in a: nd = Counter(dp) for s, c in…
- What is printed? python print(bin(5 ^ 3), int('101', 2) << 2, ~5, -~5)
- What is printed? python import math print(math.prod([]), math.prod([2, 3.0]))
- What is printed? python import heapq h = [1, 3] heapq.heapify(h) print(heapq.heapreplace(h, 0), sorted(h), heapq.heappushpop(h, 0)…
- What is printed? python try: print(list(zip([1, 2], [3], strict=True))) except ValueError: print('strict')