ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def find(xs, t):
for i, x in enumerate(xs):
if x == t:
return i
else:
return -1
return "unreachable"
print(find([4, 5], 5), find([], 1), find([1], 2))- A. 1 unreachable -1
- B. 1 -1 -1
- C. 1 -1 unreachable
- D. 1 None -1
Correct answer: B. 1 -1 -1
for-else runs the else block whenever the loop completes without break, including when the iterable is empty. Here every non-found path hits `return -1`.
More Python Programming questions
- What is printed? python def kind(v): match v: case 0 | 1: return "bit" case int(n) if n < 0: return "neg" case [x, rest]: return…
- What is printed? python RED = "r" def f(c): match c: case RED: return "red:" + RED print(f("g"), RED)
- What is printed? python data = [3, 8, 2, 9, 4] if (n := len(data)) > 4: print(n, end=" ") print([y for x in data if (y := x 2) > 10], y)
- What is printed? python a = [5, 3, 5, 1] a.remove(5) a.insert(10, 7) a.insert(-10, 0) print(a, a.index(5), a.pop(1))
- What is printed? python a = [1, 2, 3, 4] b = a a = a.extend([5]) or a print(a is b, b[-1], [].extend([1]))
- What is printed? python s = "Mississippi" print(s.count("ss"), s.count("issi"), s.replace("ss", "S"), s.lower().index("s", 4))