ProgrammingHardInfosys · Python · Advanced
What is printed?
What is printed?
def f(n):
try:
if n == 0:
raise ValueError
return 10 // (n - 1)
except ZeroDivisionError:
return -1
except ValueError:
return -2
finally:
print('F', n, end=' ')
print([f(i) for i in range(3)])- A. [-2, -1, 10] F 0 F 1 F 2
- B. F 0 F 1 F 2 [-1, -1, 10]
- C. F 0 F 1 F 2 [-2, -1, 10]
- D. F 0 F 1 F 2 [-2, 10, 10]
Correct answer: C. F 0 F 1 F 2 [-2, -1, 10]
The comprehension runs fully (each finally prints) before print shows the list; n=0 raises ValueError (-2), n=1 divides by zero (-1), n=2 gives 10 // 1.
More Python Programming questions
- What is printed? python a, b, c = range(5) x, (y, z) = 1, 'hey' print(a, b, c, x, y, z)
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2), c['z'], sorted(c.items())[:2])
- What is printed? python pts = [(2, 1), (1, 5), (2, 0), (1, 2)] print(sorted(pts, key=lambda p: (-p[0], p[1])), max(pts, key=lambda p: p[1]))
- What is printed? python import copy a = {'k': [1, 2]} b = a.copy() c = copy.deepcopy(a) a['k'].append(3) a['j'] = 0 print(b, c)
- What is printed? python m = map(lambda x: x 2, [1, 2, 3]) print(list(m), list(m), sum(map(int, '123')))
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v try: s = {P(1), P(1)} print(len(s))…