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))
- A. True 0 True [] True
- B. True 6 False [7, 8, 9] True
- C. True 6 False [] True
- 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
- What is printed? python calls = [] def chk(x): calls.append(x) return x > 2 r1 = any(chk(x) for x in [1, 3, 5]) r2 = all([chk(x) for x in…
- What is printed? python a = [1, 2] b = [a] 3 a = 2 b[0].append(9) print(b[2], len(b))
- What is printed? python a = [1, 2] b = [a] 3 a = a 2 b[0].append(9) print(a, b[1])
- What is printed? python t = ([1],) try: t[0] += [2] except TypeError: pass try: t[0] = t[0] + [3] except TypeError: pass print(t)
- What is printed? python x = [1, 2, 3] y = x x[:] = [7] x = x + [8] print(y, x)
- What is printed? python m = [[0] 2 for in range(2)] n = m.copy() n[0][0] = 5 n[1] = [6, 6] print(m)