ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
e = 5
try:
1 / 0
except ZeroDivisionError as e:
pass
try:
print(e)
except NameError:
print("NameError")- A. None
- B. division by zero
- C. NameError
- D. 5
Correct answer: C. NameError
At the end of an 'except ... as e' block Python deletes the name e (to break a reference cycle with the traceback). This removes the earlier e = 5 too, so print(e) raises NameError.
More Python Programming questions
- How many of the following statements about Python are correct? (i) A tuple that contains a list can be used as a dictionary key. (ii) A…
- What is printed? python a = [1, 2, 3] a += [4] a = a + [5] a.extend("67") print(a)
- What is printed? python x = [[0] 3] 2 y = [[0] 3 for in range(2)] x[0][1] = 5 y[0][1] = 5 print(x, y)
- What is printed? python def add(item, bucket=[]): bucket.append(item) return bucket a = add(1) b = add(2) c = add(3, []) print(a, b, c, a…
- What is printed? python fs = [lambda: i i for i in range(4)] gs = [lambda i=i: i i for i in range(4)] print([f() for f in fs], [g() for g…
- What is printed? python s = "abcdefgh" print(s[1:6:2], s[::-3], s[-3:-1], len(s[5:2]))