ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def g():
try:
return 1
finally:
return 2
print(g())- A. 2
- B. 1
- C. SyntaxError
- D. None
Correct answer: A. 2
A return in a finally block replaces the pending return value, so the function returns 2. Python allows this, though linters warn against it.
More Python Programming questions
- What is printed? python def bracketok(s): pairs = {')': '(', ']': '[', '}': '{'} st = [] for ch in s: if ch in '([{': st.append(ch) elif…
- What is printed? python import sys data = 'first line nsecondn' it = iter(data.splitlines()) print(repr(next(it)), repr(next(it).strip())…
- What is printed? python from collections import deque q = deque() for ch in 'abcd': q.append(ch) if len(q) > 2: q.popleft()…
- What is printed? python a = [1, 2, 3, 4, 5, 6, 7, 8] print(a[::3], a[2::-1], a[-3::2])
- What is printed? python x = [i for i in range(3)] g = (i for i in range(3)) print(x == list(g), list(g), len(x))
- What is printed? python print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))