ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))
- A. 2 [3]
- B. 1 [2, 3]
- C. 0 [1, 2, 3]
- D. 1 [3]
Correct answer: B. 1 [2, 3]
A list iterator just keeps an index. After yielding x[0] its index is 1; the insertion shifts items right, so x[1] is now 1 (yielded again), followed by 2 and 3.
More Python Programming questions
- What is printed? python words = ["apple", "Banana", "cherry", "date"] bylen = {} for w in words: bylen.setdefault(len(w), []).append(w)…
- What is printed? python def outer(): results = [] def rec(n): if n == 0: results.append("x") return for in range(n): rec(n - 1) rec(3)…
- What is printed? python a = "abc" b = "".join(["a", "b", "c"]) print(a == b, a is b or True, sorted(set(a + b)), a.find("c") ==…
- What is printed? python temps = {"mon": 30, "tue": 25, "wed": 35} hot = [d for d, t in temps.items() if t > 28] coolest = min(temps…
- What is printed? python s = "level" t = "".join(reversed(s)) u = s[::-1] print(s == t == u, t is s, list(reversed([1, 2])) == [2, 1]…
- What is printed? python marks = [72, 85, 64, 85, 90] print(marks.index(85), marks.count(85), sorted(marks)[len(marks) // 2]…