AlgoMaster Logo

Iterators & Generators - Quiz

Last Updated: December 6, 2025

Iterators & Generators Exercises

29 quizzes

1
Code Completion

Complete the expression to get an iterator over the values of the dictionary user_data

python
1
value_iter = user_data.()

Click an option to fill the blank:

2
Code Completion

Create an iterator from the list records so that it can be advanced with next() in a processing loop

python
1
record_iter = (records)

Click an option to fill the blank:

3
Code Completion

Complete the expression to build a generator that produces squared values from the existing iterable sensor_readings

python
1
squared = (reading ** 2 for reading in )

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes an iterator in Python?

5
Multiple Choice

What does the __iter__ method of an iterable typically return?

6
Multiple Choice

In a custom iterator class, what should __next__ do when there are no more items to produce?

7
Multiple Choice

Which of the following is TRUE about generator functions?

8
Multiple Choice

What is a key advantage of using generator expressions instead of list comprehensions for large datasets?

9
Multiple Choice

Which itertools function creates an infinite sequence of evenly spaced numbers?

10
Multiple Choice

What does the yield statement do inside a function?

11
Multiple Choice

Which expression creates a generator that yields the first 100 even numbers starting from 0?

12
Multiple Choice

What does itertools.cycle(colors) do when colors is a finite list?

13
Multiple Choice

You have a custom class LogLines that implements __iter__ but not __next__. Which is TRUE?

14
Multiple Choice

Which statement about custom iterator classes is correct?

15
Sequencing

Order the steps to create a custom iterator class that yields squares from 0 up to a given limit and then use it in a loop.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to build a generator-based data pipeline that reads lines from a file and filters only error messages.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code using a custom iterator?

1class CountUp:
2    def __init__(self, limit):
3        self.current = 0
4        self.limit = limit
5    def __iter__(self):
6        return self
7    def __next__(self):
8        if self.current >= self.limit:
9            raise StopIteration
10        value = self.current
11        self.current += 2
12        return value
13counter = CountUp(5)
14print(list(counter))
18
Output Prediction

What is the output of this generator-based Fibonacci code?

1def fib(n):
2    a, b = 0, 1
3    for _ in range(n):
4        yield a
5        a, b = b, a + b
6series = fib(5)
7print(list(series))
19
Output Prediction

What is the output when using a generator expression with sum?

1data = [1, 2, 3, 4]
2result = sum(x * x for x in data)
3print(result)
20
Output Prediction

What is the output of this itertools.islice usage?

1import itertools
2numbers = itertools.count(start=10, step=5)
3first_three = list(itertools.islice(numbers, 3))
4print(first_three)
21
Output Prediction

What is the output of this code mixing iter() and next()?

1items = ['a', 'b', 'c']
2iterator = iter(items)
3next(iterator)
4value = next(iterator)
5print(value)
22
Bug Spotting

Find the bug in this custom iterator that should yield lines from a file one by one.

Click on the line(s) that contain the bug.

python
1
class LineReader:
2
    def __init__(self, path):
3
        self.file = open(path, 'r')
4
    def __iter__(self):
5
        return self.file
6
    def __next__(self):
7
        line = self.file.readline()
8
        if not line:
9
            self.file.close()
10
            raise StopIteration
11
        return line.strip()
23
Bug Spotting

Find the bug in this generator intended to yield even numbers up to a limit.

Click on the line(s) that contain the bug.

python
1
def even_numbers(limit):
2
    number = 0
3
    while number < limit:
4
        if number % 2 == 0:
5
            yield number
6
        else:
7
            continue
8
        number += 2
24
Matching

Match each itertools function with its correct description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the iterator-related concept with its appropriate example.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the generator function to lazily read and parse integers from a text file, one per line.

python
1
def read_ints(file_path):
2
with open(file_path) as source:
3
for raw_line in source:
4
text = raw_line.() # remove surrounding whitespace
5
if not text:
6
continue
7
yield (text)
8

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to build a memory-efficient pipeline that sums only the positive values from a stream of readings.

python
1
def sum_positive(readings):
2
positives = (value for value in readings if value > 0)
3
return (positives)
4
5
data = [10, -5, 3, -1]
6
result = sum_positive(data)
7
(result)
8

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that starts iteration over the generator created by generator_expression.

Click on the line to select.

python
1
numbers = [1, 2, 3, 4]
2
generator_expression = (n * 10 for n in numbers)
3
first_value = next(generator_expression)
4
remaining_values = list(generator_expression)
29
Hotspot Selection

Click the line where StopIteration will be raised during this manual iteration.

Click on the line to select.

python
1
items = iter([10, 20])
2
print(next(items))
3
print(next(items))
4
print(next(items))

Premium Content

Subscribe to unlock full access to this content and more premium articles.