0% found this document useful (0 votes)
9 views

Python Comprehensions

Contains Python Comprehensions MCQs with options and answers

Uploaded by

shreya0473
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Comprehensions

Contains Python Comprehensions MCQs with options and answers

Uploaded by

shreya0473
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Python: Comprehensions

Comprehensions in Python are concise and expressive ways to create data structures
like lists, dictionaries, and sets. They provide a more readable and efficient alternative
to traditional loops for generating such structures. Python comprehensions include list
comprehensions, dictionary comprehensions, and set comprehensions.

1. List Comprehensions: List comprehensions provide a compact way to generate lists. The basic syntax
is:

python

Copy code

[expression for item in iterable if condition]

 expression is the value you want to append to the list.


 item is the variable representing each element in the iterable.
 iterable is any iterable object like a list, tuple, string, etc.
 condition (optional) is an expression that filters elements.

Example:

python

Copy code

# Generate a list of squares of numbers from 0 to 9

squares = [x**2 for x in range(10)]

1. Dictionary Comprehensions: Dictionary comprehensions allow you to create dictionaries in a similar


concise manner. The syntax is:

python

Copy code

{key_expression: value_expression for item in iterable if condition}

 key_expression is the expression for the dictionary keys.


 value_expression is the expression for the dictionary values.
 item, iterable, and condition have the same meanings as in list comprehensions.

Example:

python

Copy code

# Create a dictionary mapping numbers to their squares

squares_dict = {x: x**2 for x in range(10)}


1. Set Comprehensions: Set comprehensions are similar to list comprehensions but generate sets
instead. The syntax is:

python

Copy code

{expression for item in iterable if condition}

 expression, item, iterable, and condition have the same meanings as in list
comprehensions.

Example:

python

Copy code

# Create a set of even numbers from 0 to 9

even_numbers = {x for x in range(10) if x % 2 == 0}

Comprehensions offer several advantages:

 Conciseness: They allow you to achieve the same result with less code, improving readability.
 Performance: Comprehensions are generally faster than traditional loops.
 Expressiveness: They make your code more expressive and Pythonic.

However, comprehensions might become less readable if they become too complex.
It's essential to strike a balance between conciseness and readability when using
comprehensions.
窗体顶端

窗体底端
MCQs

1. What is the purpose of using comprehensions in Python?


- A) To simplify the syntax of loops
- B) To reduce code redundancy
- C) To create data structures more concisely
- D) All of the above
- **Answer: D) All of the above**

2. Which of the following data structures can be created using comprehensions?


- A) Lists
- B) Dictionaries
- C) Sets
- D) All of the above
- **Answer: D) All of the above**

3. What is the syntax for list comprehensions in Python?


- A) `{expression for item in iterable}`
- B) `[expression for item in iterable]`
- C) `(expression for item in iterable)`
- D) None of the above
- **Answer: B) `[expression for item in iterable]`**

4. Which of the following is an optional part of a comprehension in Python?


- A) Expression
- B) Iterable
- C) Condition
- D) Item
- **Answer: C) Condition**

5. What does the following list comprehension do in Python?


```python
squares = [x**2 for x in range(5)]
```
- A) Creates a list of even numbers from 0 to 4
- B) Creates a list of squares of numbers from 0 to 4
- C) Creates a list of prime numbers from 0 to 4
- D) None of the above
- **Answer: B) Creates a list of squares of numbers from 0 to 4**

6. Which of the following comprehensions is used to create dictionaries?


- A) List comprehension
- B) Set comprehension
- C) Dictionary comprehension
- D) Tuple comprehension
- **Answer: C) Dictionary comprehension**

7. What is the output of the following dictionary comprehension?


```python
squares_dict = {x: x**2 for x in range(3)}
print(squares_dict)
```
- A) `{0: 0, 1: 1, 2: 4}`
- B) `{0: 1, 1: 4, 2: 9}`
- C) `{1: 0, 2: 1, 3: 4}`
- D) `{1: 1, 2: 4, 3: 9}`
- **Answer: B) `{0: 0, 1: 1, 2: 4}`**

8. What type of elements does a set comprehension generate?


- A) Unique elements
- B) Ordered elements
- C) Immutable elements
- D) None of the above
- **Answer: A) Unique elements**

9. What is the output of the following set comprehension?


```python
even_numbers = {x for x in range(10) if x % 2 == 0}
print(even_numbers)
```
- A) `{0, 2, 4, 6, 8}`
- B) `{1, 3, 5, 7, 9}`
- C) `{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}`
- D) `{0, 1, 4, 9, 16, 25, 36, 49, 64, 81}`
- **Answer: A) `{0, 2, 4, 6, 8}`**

10. Which of the following statements about comprehensions in Python is true?


- A) Comprehensions always lead to better performance compared to traditional loops.
- B) Comprehensions cannot be nested.
- C) Comprehensions can only be used with iterable objects.
- D) Comprehensions improve code readability by adding complexity.
- **Answer: C) Comprehensions can only be used with iterable objects.**

11. Which keyword is used for filtering elements in a comprehension?


- A) `filter`
- B) `where`
- C) `if`
- D) `when`
- **Answer: C) `if`**

12. What happens if a comprehension is used with a non-iterable object?


- A) It raises a SyntaxError.
- B) It raises a TypeError.
- C) It generates an empty data structure.
- D) It creates an infinite loop.
- **Answer: B) It raises a TypeError.**

13. Which of the following is a correct way to nest comprehensions in Python?


- A) `[x*y for x in range(5) for y in range(3)]`
- B) `{x*y for x in range(5) for y in range(3)}`
- C) `{x*y for x in range(5) if x > 1 for y in range(3) if y > 1}`
- D) All of the above
- **Answer: D) All of the above**

14. What is the output of the following list comprehension?


```python
numbers = [1, 2, 3, 4]
squares = [x**2 for x in numbers if x % 2 == 0]
print(squares)
```
- A) `[1, 9]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16]`
- D) `[2, 4]`
- **Answer: B) `[4, 16]`**

15. Which of the following comprehensions is used to create a list?


- A) Set comprehension
- B) Dictionary comprehension
- C) Tuple comprehension
- D) List comprehension
- **Answer: D) List comprehension**

16. Which keyword is used to specify the iteration variable in a comprehension?


- A) `in`
- B) `for`
- C) `each`
- D) `item`
- **Answer: B) `for`**

17. Which of the following is true about comprehensions in Python?


- A) They can only be used to create lists.
- B) They cannot contain conditional statements.
- C) They are evaluated lazily.
- D) They cannot be used with lambda functions.
- **Answer: C) They are evaluated lazily.**

18. What is the output of the following dictionary comprehension?


```python
even_squares_dict = {x: x**2 for x in range(10) if x % 2 == 0}
print(even_squares_dict)
```
- A) `{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5:

25, 6: 36, 7: 49, 8: 64, 9: 81}`


- B) `{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}`
- C) `{1: 0, 3: 4, 5: 16, 7: 36, 9: 64}`
- D) `{1: 1, 3: 9, 5: 25, 7: 49, 9: 81}`
- **Answer: B) `{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}`**

19. Which of the following is a valid way to use a comprehension with a tuple?
- A) `(x for x in range(5))`
- B) `{x for x in range(5)}`
- C) `[x for x in range(5)]`
- D) None of the above
- **Answer: D) None of the above**

20. What is the output of the following set comprehension?


```python
numbers = [1, 2, 3, 4, 5]
even_squares = {x**2 for x in numbers if x % 2 == 0}
print(even_squares)
```
- A) `{1, 4, 9, 16, 25}`
- B) `{4, 16}`
- C) `{1, 9, 25}`
- D) `{0, 4, 16}`
- **Answer: B) `{4, 16}`**

21. Which of the following is a correct way to write a comprehension that generates a list of strings?
- A) `[str(x) for x in range(5)]`
- B) `{str(x) for x in range(5)}`
- C) `(str(x) for x in range(5))`
- D) `str(x) for x in range(5)`
- **Answer: A) `[str(x) for x in range(5)]`**

22. What is the output of the following list comprehension?


```python
numbers = [1, 2, 3, 4, 5]
squares = [x**2 if x % 2 == 0 else x for x in numbers]
print(squares)
```
- A) `[1, 4, 3, 16, 5]`
- B) `[1, 4, 9, 16, 25]`
- C) `[1, 2, 3, 4, 5]`
- D) `[1, 4, 9, 16, 5]`
- **Answer: D) `[1, 4, 9, 16, 5]`**

23. Which of the following comprehensions will create a list of lowercase letters from 'a' to 'z'?
- A) `[chr(x) for x in range(ord('a'), ord('z')+1)]`
- B) `{chr(x) for x in range(ord('a'), ord('z')+1)}`
- C) `(chr(x) for x in range(ord('a'), ord('z')+1))`
- D) None of the above
- **Answer: A) `[chr(x) for x in range(ord('a'), ord('z')+1)]`**

24. What is the output of the following dictionary comprehension?


```python
numbers = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in numbers if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 9: 3, 25: 5}`
- B) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 3: 9, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

25. Which of the following comprehensions is used to create a generator object?


- A) List comprehension
- B) Set comprehension
- C) Dictionary comprehension
- D) Generator comprehension
- **Answer: D) Generator comprehension**

26. What is the output of the following generator comprehension?


```python
gen = (x**2 for x in range(5))
print(gen)
```
- A) `[0, 1, 4, 9, 16]`
- B) `{0, 1, 4, 9, 16}`
- C) `<generator object <genexpr> at 0x7f8a8049d970>`
- D) `0, 1, 4, 9, 16`
- **Answer: C) `<generator object <genexpr> at 0x7f8a8049d970>`**

27. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5, 6]
evens = {x for x in nums if x % 2 == 0}
print(evens)
```
- A) `{1, 3, 5}`
- B) `{2, 4, 6}`
- C) `{1, 2, 3, 4, 5, 6}`
- D) `{2, 4}`
- **Answer: B) `{2, 4, 6}`**

28. Which of the following statements about comprehensions is true?


- A) List comprehensions are faster than generator comprehensions.
- B) Comprehensions can only be used with built-in Python data types.
- C) Comprehensions support multiple levels of nesting.
- D) Comprehensions cannot contain conditional statements.
- **Answer: C) Comprehensions support multiple levels of nesting.**

29. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0 else x**3]
print(squares)
```
- A) `[1, 4, 9, 16, 25]`
- B) `[1, 4, 27, 16, 125]`
- C) `[1, 4, 9, 16, 5]`
- D) `[1, 4, 27, 16, 5]`
- **Answer: Syntax

Error: invalid syntax**

30. Which of the following comprehensions is used to create a dictionary?


- A) List comprehension
- B) Set comprehension
- C) Dictionary comprehension
- D) Generator comprehension
- **Answer: C) Dictionary comprehension**

31. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

32. Which of the following is true about generator comprehensions?


- A) They return a generator object.
- B) They eagerly evaluate all elements.
- C) They can be indexed like lists.
- D) They cannot contain conditional statements.
- **Answer: A) They return a generator object.**

33. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums}
print(squares_set)
```
- A) `{1, 4, 9, 16, 25}`
- B) `{0, 1, 4, 9, 16, 25}`
- C) `[1, 4, 9, 16, 25]`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: A) `{1, 4, 9, 16, 25}`**

34. Which of the following is not a valid comprehension in Python?


- A) `[x for x in range(10) if x > 5]`
- B) `{x: x**2 for x in range(5)}`
- C) `(x**2 for x in range(5))`
- D) `{x for x in range(5) if x % 2 == 0}`
- **Answer: C) `(x**2 for x in range(5))`**

35. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

36. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

37. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums}
print(squares_dict)
```
- A) `{1: 1, 4: 4, 9: 9, 16: 16, 25: 25}`
- B) `{0: 1, 1: 4, 2: 9, 3: 16, 4: 25}`
- C) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- D) `{1: 0, 2: 1, 3: 4, 4: 9, 5: 16}`
- **Answer: C) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`**

38. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

39. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

40. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

41. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

42. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
-B

) They return a list.


- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

43. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

44. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

45. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

46. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**
47. What is the output of the following dictionary comprehension?
```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

48. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

49. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

50. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

51. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

52. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**
53. What is the output of the following dictionary comprehension?
```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

54. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

55. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16,

25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

56. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

57. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

58. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

59. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

60. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

61. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

62. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

63. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

64. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

65. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

66. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

67. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

68. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They

can be used to create dictionaries.


- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

69. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

70. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

71. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

72. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

73. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

74. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

75. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

76. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

77. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

78. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

79. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

80. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

81. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`

- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

82. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

83. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

84. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

85. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

86. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

87. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

88. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

89. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

90. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

91. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

92. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

93. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**

94. Which of the following is not a valid comprehension in Python?


- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10

))`**

95. What is the output of the following dictionary comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_dict = {x: x**2 for x in nums if x % 2 == 0}
print(squares_dict)
```
- A) `{1: 1, 4: 16}`
- B) `{1: 1, 3: 9, 5: 25}`
- C) `{2: 4, 4: 16}`
- D) `{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}`
- **Answer: C) `{2: 4, 4: 16}`**

96. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They return a list.
- C) They support indexing and slicing.
- D) They consume less memory compared to lists.
- **Answer: D) They consume less memory compared to lists.**

97. What is the output of the following list comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares = [x**2 for x in nums if x % 2 == 0]
print(squares)
```
- A) `[1, 9, 25]`
- B) `[4, 16]`
- C) `[1, 4, 9, 16, 25]`
- D) `[4]`
- **Answer: B) `[4, 16]`**

98. Which of the following is true about generator comprehensions?


- A) They eagerly evaluate all elements.
- B) They can be used to create dictionaries.
- C) They support slicing operations.
- D) They return a generator object.
- **Answer: D) They return a generator object.**

99. What is the output of the following set comprehension?


```python
nums = [1, 2, 3, 4, 5]
squares_set = {x**2 for x in nums if x % 2 == 0}
print(squares_set)
```
- A) `{1, 9, 25}`
- B) `{4, 16}`
- C) `{1, 4, 9, 16, 25}`
- D) `{(1, 1), (2, 4), (3, 9), (4, 16), (5, 25)}`
- **Answer: B) `{4, 16}`**
100. Which of the following is not a valid comprehension in Python?
- A) `[x**2 for x in range(10)]`
- B) `{x**2 for x in range(10)}`
- C) `(x**2 for x in range(10))`
- D) `{x: x**2 for x in range(10)}`
- **Answer: C) `(x**2 for x in range(10))`**

You might also like