Python List Comprehensions, Dictionaries, and Lazy Iterators Quiz

  1. List Comprehensions Syntax

    Which of the following generates a list of squares from 0 to 4 using a Python list comprehension?

    1. [x**2 for x in range(5)]
    2. [x^2 for x in range(5)]
    3. list(x**2 for x in range(5))
    4. {x**2 for x in range(5)}
    5. (x**2 for x in range(5))
  2. List vs Generator Comprehension

    What is the key difference between [x for x in range(10)] and (x for x in range(10))?

    1. Both create lists but with different syntax
    2. They are completely identical
    3. One creates a list, the other creates a generator
    4. One creates a tuple, the other a generator
    5. Both create generators with different performance
  3. Dictionary Comprehensions

    How do you create a dictionary mapping numbers to their cubes for numbers 1 to 3 using a comprehension?

    1. {x, x**3 for x in range(1, 4)}
    2. {x: x**3 for x in range(1, 4)}
    3. (x: x**3 for x in range(1, 4))
    4. [x: x**3 for x in range(1, 4)]
    5. {x = x**3 for x in range(1, 4)}
  4. Performance: List Comprehension vs Loop

    Why are list comprehensions generally faster than using a for loop to append elements to a list?

    1. They always run in parallel
    2. They are implemented in C for efficiency
    3. They ignore memory allocation
    4. They use global variables by default
    5. They automatically sort the data
  5. Generator Lazy Evaluation

    Which of the following is TRUE about generators in Python?

    1. Generators cannot be iterated
    2. Generators are slower than normal functions
    3. Generators are only used for sorting
    4. Generators compute values only when needed (lazily)
    5. Generators store all results in memory immediately
  6. Using next()

    What does the next() function do when used with a generator iterator?

    1. It reverses the generator’s order
    2. It retrieves the next value from the generator
    3. It restarts the generator from the beginning
    4. It closes the generator permanently
    5. It returns the last value of the generator
  7. Filtering with List Comprehensions

    Which comprehension filters the even numbers from a list nums?

    1. [x if x % 2 == 0 for x in nums]
    2. [x for nums in x if x % 2 == 0]
    3. [x for x in nums where x % 2 == 0]
    4. [x for x in nums if x % 2 == 0]
    5. [x for x in nums if x / 2]
  8. Set Comprehensions

    Which of the following syntax correctly creates a set of squares from 0 to 2?

    1. {x**2 : x in range(3)}
    2. set[x**2 for x in range(3)]
    3. {x**2 for x in range(3)}
    4. (x**2 for x in range(3))
    5. [x**2 for x in range(3)]
  9. Generator Expression Usage

    Which code efficiently computes the sum of squares from 1 to 10 without building a list in memory?

    1. sum(*[x**2 for x in range(1, 11)])
    2. sum({x: x**2 for x in range(1, 11)})
    3. sum({x**2 for x in range(1, 11)})
    4. sum(x**2 for x in range(1, 11))
    5. sum([x**2 for x in range(1, 11)])
  10. List Comprehension with Multiple Loops

    Which list comprehension creates all pairs (x, y) where x in [1,2] and y in [3,4]?

    1. [(x, y) for y in [3,4] for x in [1,2]]
    2. list((x, y) for x in [1,2] for y in [3,4])
    3. [x, y for x in [1,2] where y in [3,4]]
    4. {(x, y) for x in [1,2] for y in [3,4]}
    5. [(x, y) for x in [1,2] for y in [3,4]]
  11. Comprehensions Output Type

    What type of object does (x for x in range(5)) create?

    1. A set
    2. A list
    3. A tuple
    4. A dictionary
    5. A generator object
  12. Modifying Dict Comprehensions

    What is the result of {x: x**2 for x in [2, 4]}?

    1. {2: 4, 4: 16}
    2. {2, 4: 4, 16}
    3. [2: 4, 4: 16]
    4. {x: x**2}
    5. {2: 2, 4: 4}
  13. Advantage of Generators with Large Data

    Why are generators preferred over lists when processing very large datasets?

    1. They use less memory via lazy evaluation
    2. They convert all data to strings
    3. They sort data automatically
    4. They make data access random
    5. They combine data types seamlessly
  14. Iterating Over Dictionary Items

    What will dict.items() return when called on a dictionary object?

    1. An iterable view of key-value pairs
    2. A list of keys only
    3. A generator of values
    4. A tuple of items
    5. A set of keys
  15. List Comprehension with Conditionals

    Which comprehension creates a list replacing negative numbers in nums with 0?

    1. [x if x u003E= 0 else 0 for x in nums]
    2. [x else 0 for x in nums if x u003E= 0]
    3. [x for x u003E= 0 else 0 in nums]
    4. [x if x u003C= 0 or 0 for x in nums]
    5. [x if x u003C 0 else 0 for x in nums]
  16. Exhausting a Generator

    What happens after a generator is exhausted in Python?

    1. It repeats values from the start
    2. It raises StopIteration on further next() calls
    3. It returns None on next() calls
    4. It restarts automatically
    5. It skips to the last value