ProgrammingVery hardProduct company OA

What is printed?

What is printed?
it = iter(range(10))
print(5 in it, next(it), 2 in it, list(it), 9 in range(10))
  1. A. True 0 True [] True
  2. B. True 6 False [7, 8, 9] True
  3. C. True 6 False [] True
  4. D. True 5 False [] True

Correct answer: C. True 6 False [] True

in on an iterator consumes items until it finds a match. 5 in it eats 0..5 and returns True, so next(it) is 6. 2 in it searches the rest (7, 8, 9), never finds 2 and exhausts the iterator, so list(it) is []. A range is not an iterator; 9 in range(10) is an O(1) arithmetic check and consumes nothing.

More Python Programming questions

Exams that ask this topic