Python List/Dict Comprehensions and Iterator Insights Quiz

Challenge your understanding of Python list and dictionary comprehensions as well as iterator and generator mechanics, with an emphasis on performance and lazy evaluation concepts.

  1. List Comprehension Syntax

    Which of the following is the correct syntax for a list comprehension that creates a list of squares of the numbers from 0 to 9?

    1. [x^2 for x in range(10)]
    2. (x**2 for x in range(10))
    3. [x**2 for x in range(10)]
    4. {x**2 for x in range(10)}
    5. [x*2 for x in range(10)]
  2. Dictionary Comprehension with Filter

    How do you construct a dictionary comprehension mapping integer x to x squared for even x in range 5?

    1. {x: x^2 for x in range(5) if x%2=0}
    2. {x: x**2 for x in range(5) if x%2==0}
    3. [x: x**2 for x in range(5) if x%2==0]
    4. {x: x*x if x%2==0 for x in range(5)}
    5. (x: x**2 for x in range(5) if x%2==0)
  3. Generator Expression vs. List Comprehension

    Which advantage does a generator expression (e.g., (x for x in range(1_000_000))) have over a list comprehension in terms of memory usage?

    1. It computes items lazily, using significantly less memory at a time
    2. It stores all items in memory like a list
    3. It cannot be iterated over multiple times
    4. It consumes memory proportional to the entire sequence
    5. It is faster because it precomputes the elements
  4. Iterator Protocol

    Which pair of methods must an object implement to act as an iterator in Python?

    1. __iter__ and __getitem__
    2. __getitem__ and __len__
    3. __next__ and __call__
    4. __next__ and __iter__
    5. __iter__ and __len__
  5. Comprehension Scope

    If a variable 'x' exists outside a list comprehension, what happens if the comprehension also uses 'x' as its loop variable?

    1. Python raises a syntax error due to scope collision
    2. Both variables refer to the same memory address
    3. The outer 'x' is always overwritten
    4. The meaning of 'x' inside and outside is always linked
    5. The comprehension's 'x' masks the outer 'x' only inside its own scope in Python 3+
  6. Generator Function Execution

    What happens when a generator function is called but no iteration is performed?

    1. The function returns an empty list
    2. A TypeError is raised
    3. All yield expressions are executed immediately
    4. The function runs to completion
    5. Nothing in the function body is executed until iteration begins
  7. Comprehensions and Performance

    Why are list comprehensions typically faster than equivalent for-loops constructing a list?

    1. They automatically parallelize iterations
    2. They use extra CPU cores
    3. They prevent all variable lookups
    4. They invoke C-implemented internal functions, reducing Python bytecode execution overhead
    5. They allocate memory only after building the list
  8. Generator Consumption

    If a generator expression is used to sum the cubes of numbers from 0 to 999, which action computes the values?

    1. Iterating with a for-loop or calling sum(generator)
    2. Printing the generator variable
    3. Assigning the generator to a list
    4. Creating the generator variable
    5. Calling len() on the generator
  9. Dict Comprehension and Key Collisions

    What is the result if a dict comprehension generates duplicate keys?

    1. A warning is generated but all values are stored
    2. Duplicates are silently ignored
    3. Python raises a KeyError
    4. The dict stores all values for each key as a list
    5. The last value for a given key overwrites any previous value
  10. Custom Iterators

    How is the 'StopIteration' exception used in custom iterator classes?

    1. It is caught to skip certain elements
    2. It is raised at the creation of the iterator
    3. It resumes iteration when caught by __iter__
    4. It signals the end of iteration when raised by the __next__ method
    5. It is used to indicate a computation error
  11. List Comprehension Side Effects

    Why is it discouraged to use a list comprehension only for its side effects, such as printing or modifying external state?

    1. List comprehensions always return a dictionary
    2. They are deprecated in Python 3
    3. They can be more memory-inefficient than for-loops for side effects
    4. They support only numeric operations
    5. Their syntax is reserved for nested loops only
  12. Nested Comprehensions

    Which expression generates all ordered pairs (x, y) where x is in [1,2,3] and y is in [4,5] using a list comprehension?

    1. [(x, y) for y in [4,5] for x in [1,2,3]]
    2. {(x, y) for x in [1,2,3] for y in [4,5]}
    3. [(x, y) for x in [1,2,3], y in [4,5]]
    4. (x, y for x in [1,2,3] for y in [4,5])
    5. [(x, y) for x in [1,2,3] for y in [4,5]]
  13. Lazy Evaluation

    Why do generator expressions support lazy evaluation, and when is this advantageous?

    1. They evaluate all items before use, ensuring speed for small datasets
    2. They store data for reuse, increasing performance for repeated access
    3. They create items only when requested, which is efficient for large or infinite data streams
    4. They type-check the generated objects at run time
    5. They automatically parallelize item creation
  14. Iterators and Reusability

    What happens if you try to iterate over a generator a second time after it has been exhausted?

    1. It raises a MemoryError
    2. The generator raises a ValueError
    3. A new generator is automatically created
    4. It restarts from the beginning
    5. It produces no values because its state is exhausted
  15. Difference Between map and Generator Comprehension

    What is one significant difference between using map(f, iterable) and a generator expression (f(x) for x in iterable)?

    1. Generator expressions evaluate eagerly, but map is lazy
    2. map returns a map object, while generator comprehensions return a generator object
    3. map always returns a list in Python 3
    4. map cannot be used with lambda functions
    5. Both always result in exactly the same performance