QuizLander
← All public quizzes

Python — Comprehensions, iterators & generators

by Jude · 23/05/2026 · 121 views · 0 shares

Comprehensions (list/dict/set), iterator protocol, yield, generator pipelines and itertools. 20 questions.
20
Questions
🇬🇧 English
Language
Hard
Difficulty
Start the quiz (as a guest or with an account)
Share as image

Questions in this quiz

  1. 1. What does [x * x for x in range(5) if x % 2] evaluate to?
    1. [0, 1, 4, 9, 16]
    2. [1, 9]
    3. [1, 4, 9]
    4. [1, 9, 25]
  2. 2. Difference between (x for x in r) and [x for x in r]?
    1. None
    2. First is a generator (lazy); second is a list (eager)
    3. First returns a tuple
    4. First is faster but uses more memory
  3. 3. What protocol must an iterator implement?
    1. __iter__ and __next__
    2. __getitem__ only
    3. __call__
    4. __index__
  4. 4. What does this print?
    def gen():
        yield 1
        yield 2
        yield 3
    
    print(sum(gen()))
    1. 3
    2. 6
    3. [1, 2, 3]
    4. TypeError
  5. 5. What is the output?
    def gen():
        x = yield 1
        yield x + 10
    
    g = gen()
    print(next(g))
    print(g.send(5))
    1. 1 then 15
    2. 1 then 11
    3. 5 then 15
    4. StopIteration
  6. 6. Which itertools function chains iterables together?
    1. chain
    2. zip_longest
    3. product
    4. tee
  7. 7. What does this print?
    g = (i*i for i in range(3))
    print(list(g))
    print(list(g))
    1. [0, 1, 4] then [0, 1, 4]
    2. [0, 1, 4] then []
    3. [] then []
    4. Error
  8. 8. How do you delegate to another iterable in a generator?
    1. yield from iterable
    2. yield *iterable
    3. return iterable
    4. raise iterable
  9. 9. Complexity of any(x < 0 for x in big_list)?
    1. Always O(n)
    2. Best O(1) if early match, worst O(n) — short-circuits
    3. O(n log n)
    4. O(1) memory, O(0) time
  10. 10. What does this print?
    d = {k: v for k, v in [('a',1),('b',2),('a',3)]}
    print(d)
    1. {'a': 1, 'b': 2}
    2. {'a': 3, 'b': 2}
    3. {'a': [1, 3], 'b': 2}
    4. Error
  11. 11. What does list(zip([1,2,3], ['a','b'])) return?
    1. [(1, 'a'), (2, 'b')]
    2. [(1, 'a'), (2, 'b'), (3, None)]
    3. Error
    4. [1, 2, 3, 'a', 'b']
  12. 12. What does this produce?
    from itertools import islice, count
    for x in islice(count(10, 2), 4):
        print(x, end=' ')
    1. 10 11 12 13
    2. 10 12 14 16
    3. 10 12 14
    4. Error
  13. 13. How do you implement a lazy chunks of n generator?
    1. for c in chunks: yield list(c) with itertools.islice in a loop until empty
    2. map(chunked, lst)
    3. list(zip(lst, lst, lst))
    4. Impossible without numpy
  14. 14. What is the output?
    def g():
        try:
            yield 1
        finally:
            print("end")
    
    for x in g():
        print(x)
        break
    1. 1
    2. 1 then end
    3. end then 1
    4. Generator does not call finally on break
  15. 15. Which expression is a set comprehension?
    1. {x for x in r}
    2. {x: 0 for x in r}
    3. (x for x in r)
    4. [x for x in r]
  16. 16. What does enumerate(seq, start=10) yield for seq=['a','b']?
    1. (0, 'a'), (1, 'b')
    2. (10, 'a'), (11, 'b')
    3. (1, 'a'), (2, 'b')
    4. Error
  17. 17. What is the output?
    from itertools import groupby
    data = sorted("AaBbAa", key=str.lower)
    result = [(k, len(list(g))) for k, g in groupby(data, key=str.lower)]
    print(result)
    1. [('a', 4), ('b', 2)]
    2. [('A', 2), ('a', 2), ('B', 1), ('b', 1)]
    3. [('a', 6)]
    4. Error
  18. 18. What is special about itertools.tee?
    1. Splits one iterator into n independent iterators (with memory cost if they advance at different rates)
    2. Reverses an iterator
    3. Joins two iterators
    4. Filters an iterator
  19. 19. What's the result of next(iter([]), 'def')?
    1. StopIteration
    2. None
    3. 'def'
    4. Error
  20. 20. Which generator pattern correctly streams a large file by line and uppercases each line lazily?
    1. (line.upper() for line in open(p))
    2. open(p).read().upper().splitlines()
    3. [l.upper() for l in open(p)]
    4. map(str.upper, open(p).readlines())
Answer the questions and see my score

Comments

Sign in to comment.

Loading…