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))
  1. A. 1 unreachable -1
  2. B. 1 -1 -1
  3. C. 1 -1 unreachable
  4. 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

Exams that ask this topic