Question 2Easy
What makes a function a generator function?
- It returns a list
- It is decorated with @generator
- It uses a for loop
- It contains a yield expression
Answer: D. It contains a yield expression
Explanation
A function whose body contains yield returns a generator object when called. Its code runs lazily as values are requested.
Step-by-step solution
- A generator function is marked by a yield expression in its body.
- Calling it returns a generator object, and the body runs step by step as values are asked for.
- So the key feature is a yield expression, the correct answer.
Why the other options are wrong
- A. A generator function returns a generator object, not a list.
- B. Python has no @generator decorator for this.
- C. A for loop can be used in any function, and it does not make a generator.
Tip: A function with yield is a generator function. A plain return ends the function at once.
Question 3Medium
What is the output?
print({k: v for k, v in zip("ab", [1, 2, 3])})
- {'a': 1, 'b': 2}
- {'a': 1, 'b': 2, 'c': 3}
- {'a': 1, 'b': 2, None: 3}
- ValueError
Answer: A. {'a': 1, 'b': 2}
Explanation
zip stops at the shortest input, so only two pairs are produced.
Step-by-step solution
- zip('ab', [1, 2, 3]) pairs the items as ('a', 1) and ('b', 2).
- Zip stops at the shorter input, so the third item 3 is dropped.
- The dict comprehension gives {'a': 1, 'b': 2}, the correct answer.
Why the other options are wrong
- B. The key c is never made, because the string 'ab' has only two letters.
- C. None is never used as a key here; zip does not pair 3 with anything.
- D. No error happens; zip just stops at the shorter input.
Tip: Zip stops at the shortest input, so extra items are dropped without an error.
Question 4Medium
What is the output?
g = (x for x in range(3))
print(sum(g), sum(g))
- 3 0
- 3 None
- 0 3
- 3 3
Answer: A. 3 0
Explanation
A generator can be consumed only once. The first sum uses up all values (0 + 1 + 2 = 3), so the second sum sees an empty iterator and returns 0.
Step-by-step solution
- G is a generator, so its values can be used only once.
- The first sum takes 0, 1 and 2, which adds to 3.
- The second sum finds the generator empty and gives 0, so the output is 3 0, the correct answer.
Why the other options are wrong
- B. 3 None is wrong; the sum of an empty generator is 0, not None.
- C. 0 3 is wrong; the first sum runs first and gives 3.
- D. 3 3 is wrong; a generator can be used only once.
Tip: Generators can be used only once. To reuse the values, store them in a list or create the generator again.
Question 5Medium
What is the output?
def gen():
yield 1
yield 2
g = gen()
print(next(g), next(g), next(g, "done"))
- 1 2 done
- 1 2 StopIteration
- 1 1 1
- 1 2 None
Answer: A. 1 2 done
Explanation
After both values are produced, next() with a default argument returns the default instead of raising StopIteration.
Step-by-step solution
- The first next(g) gives 1, and the second next(g) gives 2.
- The third call, next(g, 'done'), finds no value left and returns the default 'done'.
- So the output is 1 2 done, the correct answer.
Why the other options are wrong
- B. StopIteration is not printed, because the default argument stops that error.
- C. 1 1 1 is wrong; each next call moves on, so the values are 1 then 2.
- D. None is wrong; the default given is the string 'done'.
Tip: next(it, default) returns the default instead of raising StopIteration when the iterator is used up.
Question 6Hard
What is the output?
print([(i, j) for i in range(3) for j in range(i)])
- [(0, 1), (0, 2), (1, 2)]
- [(1, 0), (2, 0), (2, 1)]
- []
- [(0, 0), (1, 0), (2, 0)]
Answer: B. [(1, 0), (2, 0), (2, 1)]
Explanation
The for clauses nest left to right.
For i = 0, range(0) is empty.
i = 1 gives (1, 0), and i = 2 gives (2, 0) and (2, 1).
Step-by-step solution
- The first for is the outer loop with i = 0, 1, 2. The inner loop runs j over range(i), from 0 up to i minus 1.
- For i = 0, range(0) is empty. For i = 1, j is 0. For i = 2, j is 0 and 1.
- The pairs are (1, 0), (2, 0) and (2, 1), the correct answer.
Why the other options are wrong
- A. (0, 1) and the other pairs are wrong; j is always less than i here.
- C. [] is wrong; pairs exist for i = 1 and i = 2.
- D. (0, 0) is wrong; for i = 0 the inner range is empty, so no pair starts with 0.
Tip: The first for is the outer loop and the second is the inner loop. The inner range depends on the outer value.