Question 1
In list comprehension, what does the expression represent?
The iterable being traversed.
The condition for filtering elements.
The value to be included in the new list.
The index of the current element.
Question 2
Can list comprehension be used with other data types besides lists?
No, it can only be used with lists.
Yes, it can be used with any iterable.
Yes, but only with strings.
No, it is limited to numerical data types.
Question 3
Explain the concept of filtering in list comprehension.
It involves transforming elements based on a specified condition.
It refers to the process of excluding elements based on a condition.
It is a technique for optimizing list comprehension.
It is not applicable to list comprehension.
Question 4
What will the following list comprehension output?
squares = [x**2 for x in range(5)]
print(squares)
[0, 1, 4, 9, 16]
[1, 4, 9, 16, 25]
[0, 2, 4, 6, 8]
[1, 3, 5, 7, 9]
Question 5
What is the output of the following list comprehension?
vowels = ['a', 'e', 'i', 'o', 'u']
upper_vowels = [vowel.upper() for vowel in vowels]
print(upper_vowels)
['a', 'e', 'i', 'o', 'u']
['A', 'E', 'I', 'O', 'U']
['a', 'E', 'i', 'O', 'u']
['A', 'e', 'I', 'o', 'U']
Question 6
When using list comprehension, what happens if the iterable is empty?
It raises a ValueError.
It creates an empty list.
It raises a SyntaxError.
It results in an infinite loop.
Question 7
What is the purpose of the following list comprehension?
numbers = [1, -2, 3, -4, 5]
squared_positives = [x**2 if x > 0 else 0 for x in numbers]
print(squared_positives)
Squares all numbers and preserves negative results.
Squares positive numbers and replaces non-positive values with 0.
Squares only negative numbers.
Keeps only numbers greater than 3.
Question 8
Does list comprehension automatically remove duplicate values? Consider the following code.
values = [2, 2, 3, 3, 3]
result = [x for x in values if x > 2]
print(result)
[2, 2, 3, 3, 3]
[3]
[3, 3, 3]
[2, 3]
Question 9
How can list comprehension be used to create a list of tuples?
By converting the entire resulting list into a tuple using tuple().
By wrapping each output element in a list.
List comprehension cannot create tuple elements.
By returning a tuple expression, such as (x, y), for each iteration.
Question 10
What is the significance of the order of for clauses in nested list comprehensions?
The order has no effect on the result.
It determines the order in which elements and combinations are generated.
It is not possible to have multiple for clauses in list comprehension.
It only affects the readability of the code.
There are 25 questions to complete.