List Comprehensions Syntax
Which of the following generates a list of squares from 0 to 4 using a Python list comprehension?
- [x**2 for x in range(5)]
- [x^2 for x in range(5)]
- list(x**2 for x in range(5))
- {x**2 for x in range(5)}
- (x**2 for x in range(5))
List vs Generator Comprehension
What is the key difference between [x for x in range(10)] and (x for x in range(10))?
- Both create lists but with different syntax
- They are completely identical
- One creates a list, the other creates a generator
- One creates a tuple, the other a generator
- Both create generators with different performance
Dictionary Comprehensions
How do you create a dictionary mapping numbers to their cubes for numbers 1 to 3 using a comprehension?
- {x, x**3 for x in range(1, 4)}
- {x: x**3 for x in range(1, 4)}
- (x: x**3 for x in range(1, 4))
- [x: x**3 for x in range(1, 4)]
- {x = x**3 for x in range(1, 4)}
Performance: List Comprehension vs Loop
Why are list comprehensions generally faster than using a for loop to append elements to a list?
- They always run in parallel
- They are implemented in C for efficiency
- They ignore memory allocation
- They use global variables by default
- They automatically sort the data
Generator Lazy Evaluation
Which of the following is TRUE about generators in Python?
- Generators cannot be iterated
- Generators are slower than normal functions
- Generators are only used for sorting
- Generators compute values only when needed (lazily)
- Generators store all results in memory immediately
Using next()
What does the next() function do when used with a generator iterator?
- It reverses the generator’s order
- It retrieves the next value from the generator
- It restarts the generator from the beginning
- It closes the generator permanently
- It returns the last value of the generator
Filtering with List Comprehensions
Which comprehension filters the even numbers from a list nums?
- [x if x % 2 == 0 for x in nums]
- [x for nums in x if x % 2 == 0]
- [x for x in nums where x % 2 == 0]
- [x for x in nums if x % 2 == 0]
- [x for x in nums if x / 2]
Set Comprehensions
Which of the following syntax correctly creates a set of squares from 0 to 2?
- {x**2 : x in range(3)}
- set[x**2 for x in range(3)]
- {x**2 for x in range(3)}
- (x**2 for x in range(3))
- [x**2 for x in range(3)]
Generator Expression Usage
Which code efficiently computes the sum of squares from 1 to 10 without building a list in memory?
- sum(*[x**2 for x in range(1, 11)])
- sum({x: x**2 for x in range(1, 11)})
- sum({x**2 for x in range(1, 11)})
- sum(x**2 for x in range(1, 11))
- sum([x**2 for x in range(1, 11)])
List Comprehension with Multiple Loops
Which list comprehension creates all pairs (x, y) where x in [1,2] and y in [3,4]?
- [(x, y) for y in [3,4] for x in [1,2]]
- list((x, y) for x in [1,2] for y in [3,4])
- [x, y for x in [1,2] where y in [3,4]]
- {(x, y) for x in [1,2] for y in [3,4]}
- [(x, y) for x in [1,2] for y in [3,4]]
Comprehensions Output Type
What type of object does (x for x in range(5)) create?
- A set
- A list
- A tuple
- A dictionary
- A generator object
Modifying Dict Comprehensions
What is the result of {x: x**2 for x in [2, 4]}?
- {2: 4, 4: 16}
- {2, 4: 4, 16}
- [2: 4, 4: 16]
- {x: x**2}
- {2: 2, 4: 4}
Advantage of Generators with Large Data
Why are generators preferred over lists when processing very large datasets?
- They use less memory via lazy evaluation
- They convert all data to strings
- They sort data automatically
- They make data access random
- They combine data types seamlessly
Iterating Over Dictionary Items
What will dict.items() return when called on a dictionary object?
- An iterable view of key-value pairs
- A list of keys only
- A generator of values
- A tuple of items
- A set of keys
List Comprehension with Conditionals
Which comprehension creates a list replacing negative numbers in nums with 0?
- [x if x u003E= 0 else 0 for x in nums]
- [x else 0 for x in nums if x u003E= 0]
- [x for x u003E= 0 else 0 in nums]
- [x if x u003C= 0 or 0 for x in nums]
- [x if x u003C 0 else 0 for x in nums]
Exhausting a Generator
What happens after a generator is exhausted in Python?
- It repeats values from the start
- It raises StopIteration on further next() calls
- It returns None on next() calls
- It restarts automatically
- It skips to the last value