ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
def f(x):
out = []
try:
out.append('t')
if x:
raise ValueError
except ValueError:
out.append('e')
else:
out.append('l')
finally:
out.append('f')
return ''.join(out)
print(f(0), f(1))- A. tlf tlf
- B. tlf tef
- C. tle tef
- D. tef tlf
Correct answer: B. tlf tef
The else clause runs only when no exception occurred, and finally always runs last. f(0) gives t, l, f and f(1) gives t, e, f.
More Python Programming questions
- What is printed? python def find(g, t): for i, row in enumerate(g): for j, v in enumerate(row): if v == t: break else: continue return (i…
- What is printed? python from functools import totalordering @totalordering class V: def init(self, n): self.n = n def eq(self, o): return…
- What is printed? python def make(): memo = {} def fib(n): if n in memo: return memo[n] memo[n] = n if n < 2 else fib(n - 1) + fib(n - 2)…
- What is printed? python words = 'the cat and the dog and the end'.split() cnt = {} for w in words: cnt[w] = cnt.get(w, 0) + 1 top =…
- What is printed? python def merge(iv): iv.sort() out = [iv[0]] for s, e in iv[1:]: if s <= out[-1][1]: out[-1][1] = max(out[-1][1], e)…
- What is printed? python import heapq def dijk(g, s): d = {s: 0} h = [(0, s)] while h: du, u = heapq.heappop(h) if du > d[u]: continue for…