ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
def gen():
yield 1
return 5
yield 2
print(list(gen()))- A. [1]
- B. [1, 5, 2]
- C. [1, 5]
- D. [1, 2]
Correct answer: A. [1]
return inside a generator ends the iteration (its value goes into StopIteration.value and list() ignores it). The code after return never runs, so only 1 is produced.
More Python Programming questions
- What is printed? python print("a,b,,c".split(","), len("a b c".split()))
- What is printed? python print(True + True 3, type(True + 1).name)
- What is printed? python a = [1, 2, 3, 4, 5] a[1:3] = [9] a[::2] = [0, 0] print(a)
- What is printed? python d = {} d[1] = 'a' d[1.0] = 'b' d[True] = 'c' print(d)
- What is printed? python x = [1, 2, 3] for i in x: x.remove(i) print(x)
- What is printed? python fs = [lambda: i for i in range(3)] print([f() for f in fs])