Python List Comprehension MCQs

1 min read

List comprehensions in Python provide a concise, readable, and highly optimized syntax for creating new lists based on existing iterables. By combining loops and conditional filters into a single line enclosed within square brackets, they often replace traditional for loops and map/filter functions. Understanding how to construct, read, and nest list comprehensions is vital for writing Pythonic, high-performance code. These practice MCQs test your ability to evaluate syntax, handle conditions, and analyze code outputs effectively.

1. What is the primary syntactic difference between a list comprehension and a generator expression?

a) List comprehensions use square brackets while generator expressions use parentheses
b) List comprehensions use curly braces while generator expressions use square brackets
c) Generator expressions require the yield keyword
d) List comprehensions are lazy-evaluated
Correct Answer: a) List comprehensions use square brackets while generator expressions use parentheses
Explanation:
List comprehensions are enclosed in square brackets [] and evaluate eagerly, whereas generator expressions use parentheses () and evaluate lazily.

2. Which of the following expressions creates a list of ASCII integer values for characters in 'abc'?

a) [ord(c) for c in 'abc']
b) [chr(c) for c in 'abc']
c) [ascii(c) for c in 'abc']
d) [int(c) for c in 'abc']
Correct Answer: a) [ord(c) for c in 'abc']
Explanation:
The ord() function returns the integer ordinal of a character, whereas chr() converts an integer to its character equivalent.

3. What will [x for x in range(5) if x > 2] evaluate to?

a) [3, 4]
b) [2, 3, 4]
c) [0, 1, 2]
d) [4]
Correct Answer: a) [3, 4]
Explanation:
range(5) yields 0, 1, 2, 3, 4. The filter 'if x > 2' keeps only numbers strictly greater than 2, resulting in [3, 4].

4. How are multiple filter conditions combined in a single list comprehension without an else clause?

a) By chaining multiple 'if' keywords consecutively
b) By separating conditions with commas
c) By using the bitwise OR operator |
d) By nesting separate list comprehensions
Correct Answer: a) By chaining multiple 'if' keywords consecutively
Explanation:
Chained 'if' clauses act with an implicit logical AND condition.

5. What is the output of [x if x > 0 else -x for x in [-2, -1, 0, 1, 2]]?

a) [2, 1, 0, 1, 2]
b) [-2, -1, 0, 1, 2]
c) [0, 1, 2]
d) [2, 1, 0]
Correct Answer: a) [2, 1, 0, 1, 2]
Explanation:
This is a ternary conditional expression mapping that computes the absolute value for each element.

6. Which statement is correct regarding the scope of loop variables in Python 3 list comprehensions?

a) Loop variables are local to the list comprehension and do not leak into the surrounding scope
b) Loop variables overwrite outer variables permanently
c) Loop variables raise a SyntaxError if they match an outer variable name
d) Loop variables persist in the enclosing function after execution
Correct Answer: a) Loop variables are local to the list comprehension and do not leak into the surrounding scope
Explanation:
Python 3 isolates list comprehension scopes, preventing loop variables from leaking into the parent block.

7. What does [x for x in range(3)] * 2 produce?

a) [0, 1, 2, 0, 1, 2]
b) [0, 2, 4]
c) [[0, 1, 2], [0, 1, 2]]
d) Error
Correct Answer: a) [0, 1, 2, 0, 1, 2]
Explanation:
Multiplying a list by 2 performs list concatenation, repeating its elements twice.

8. What is the result of [x for x in 'a b c'.split()]?

a) ['a', 'b', 'c']
b) ['a b c']
c) [('a', 'b', 'c')]
d) Error
Correct Answer: a) ['a', 'b', 'c']
Explanation:
The split() method splits the string by whitespace into a list of words, which are then iterated over.

9. Can you use the 'break' statement inside a list comprehension?

a) No, 'break' and 'continue' statements are syntax errors inside list comprehensions
b) Yes, to exit the loop early
c) Yes, when combined with try-except
d) Only inside nested comprehensions
Correct Answer: a) No, 'break' and 'continue' statements are syntax errors inside list comprehensions
Explanation:
Control flow statements like break and continue cannot be used directly inside list comprehension syntax.

10. What does [i for i in range(10) if i % 2 == 0][:3] return?

a) [0, 2, 4]
b) [2, 4, 6]
c) [0, 1, 2]
d) [0, 2, 4, 6]
Correct Answer: a) [0, 2, 4]
Explanation:
First, the list comprehension generates even numbers up to 9: [0, 2, 4, 6, 8]. Then slicing [:3] takes the first three elements.

11. What is the output of [bool(x) for x in [0, 1]]?

a) [False, True]
b) [True, False]
c) [True, True]
d) [False, False]
Correct Answer: a) [False, True]
Explanation:
0 evaluates to False and 1 evaluates to True in boolean contexts.

12. Which of the following is true regarding the execution speed of list comprehensions vs standard loops?

a) List comprehensions are generally faster because the loop is executed in C bytecode
b) Standard for loops are always faster
c) There is no performance difference
d) List comprehensions are slower due to overhead
Correct Answer: a) List comprehensions are generally faster because the loop is executed in C bytecode
Explanation:
Python's interpreter executes list comprehensions with optimized C-level iteration constructs.

13. What does [x.strip() for x in [' apple ', 'banana ']] evaluate to?

a) ['apple', 'banana']
b) [' apple ', 'banana ']
c) ['apple', 'banana ']
d) Error
Correct Answer: a) ['apple', 'banana']
Explanation:
The strip() method removes leading and trailing whitespaces from each string.

14. What is the output of [x for x in range(3) for y in range(1)]?

a) [0, 1, 2]
b) [[0], [1], [2]]
c) [0, 0, 1, 1]
d) []
Correct Answer: a) [0, 1, 2]
Explanation:
The inner loop runs once per outer iteration, yielding each item of range(3).

15. How do you construct a dictionary comprehension in Python?

a) {key: value for item in iterable}
b) [key: value for item in iterable]
c) (key: value for item in iterable)
d) dict[key: value for item in iterable]
Correct Answer: a) {key: value for item in iterable}
Explanation:
Dictionary comprehensions use curly braces {} with key-value pairs separated by a colon.

16. What is the output of [int(c) for c in '123']?

a) [1, 2, 3]
b) ['1', '2', '3']
c) [1.0, 2.0, 3.0]
d) Error
Correct Answer: a) [1, 2, 3]
Explanation:
The int() function converts each character string into an integer object.

17. What does [x**2 for x in range(4) if x % 2 != 0] return?

a) [1, 9]
b) [0, 4]
c) [1, 4, 9]
d) [9]
Correct Answer: a) [1, 9]
Explanation:
Odd numbers in range(4) are 1 and 3. Squaring them yields 1**2 = 1 and 3**2 = 9.

18. What is the result of [x for x in []]?

a) []
b) None
c) [None]
d) Error
Correct Answer: a) []
Explanation:
Iterating over an empty iterable produces an empty list.

19. Which of the following creates a set comprehension of unique remainders when divided by 3?

a) {x % 3 for x in range(10)}
b) [x % 3 for x in range(10)]
c) (x % 3 for x in range(10))
d) set[x % 3 for x in range(10)]
Correct Answer: a) {x % 3 for x in range(10)}
Explanation:
Curly braces with an expression and iteration clause construct a set comprehension, which automatically removes duplicates.

20. What does [len(w) for w in ['python', 'code']] output?

a) [6, 4]
b) [4, 6]
c) [6, 5]
d) [5, 4]
Correct Answer: a) [6, 4]
Explanation:
Length of 'python' is 6 and length of 'code' is 4.

21. What is the output of [x for x in 'abc']?

a) ['a', 'b', 'c']
b) ['abc']
c) ('a', 'b', 'c')
d) abc
Correct Answer: a) ['a', 'b', 'c']
Explanation:
Iterating over a string yields its individual characters as single-character strings.

22. Can list comprehensions be nested more than two levels deep?

a) Yes, arbitrarily nested comprehensions are syntactically valid
b) No, Python restricts nesting to 2 levels maximum
c) No, nesting causes recursion depth errors
d) Only when using map()
Correct Answer: a) Yes, arbitrarily nested comprehensions are syntactically valid
Explanation:
List comprehensions can be nested as deeply as needed, though deep nesting harms code readability.

23. What is the result of [x + y for x, y in zip([1, 2], [3, 4])]?

a) [4, 6]
b) [1, 2, 3, 4]
c) [[1, 3], [2, 4]]
d) [5, 6]
Correct Answer: a) [4, 6]
Explanation:
zip() pairs elements in parallel: 1+3=4 and 2+4=6.

24. What does [x.upper() for x in ['hi', 'there']] return?

a) ['HI', 'THERE']
b) ['Hi', 'There']
c) ['hi', 'there']
d) HITHERE
Correct Answer: a) ['HI', 'THERE']
Explanation:
The upper() method converts string characters to uppercase.

25. What happens if you omit the expression before the 'for' keyword in a list comprehension?

a) A SyntaxError is raised
b) It returns None values
c) It defaults to the loop variable
d) It returns an empty list
Correct Answer: a) A SyntaxError is raised
Explanation:
Python syntax mandates that an expression must precede the 'for' keyword.

26. What is the output of [x for x in range(3) if not x]?

a) [0]
b) [1, 2]
c) [0, 1, 2]
d) []
Correct Answer: a) [0]
Explanation:
not x is True only when x is 0 (falsy value).

27. Which built-in function provides both index and value when used with a list comprehension?

a) enumerate()
b) index()
c) range()
d) zip()
Correct Answer: a) enumerate()
Explanation:
enumerate() yields (index, element) tuples which can be unpacked in the loop header.

28. What does [round(x, 2) for x in [3.14159, 2.71828]] output?

a) [3.14, 2.72]
b) [3.1, 2.7]
c) [3.141, 2.718]
d) [3, 2]
Correct Answer: a) [3.14, 2.72]
Explanation:
The round() function rounds floats to the specified number of decimal places.

29. What is the output of [x for x in range(4) if x % 2 == 0]?

a) [0, 2]
b) [1, 3]
c) [0, 1, 2, 3]
d) [2, 4]
Correct Answer: a) [0, 2]
Explanation:
Filters even numbers from range(4), resulting in 0 and 2.

30. Can side effects (like modifying external state) occur inside list comprehensions?

a) Yes, because expressions and function calls can execute side effects
b) No, side effects are strictly prohibited by the compiler
c) Only in multi-threaded code
d) Only when using global variables
Correct Answer: a) Yes, because expressions and function calls can execute side effects
Explanation:
While syntactically possible, relying on side effects in comprehensions is considered non-idiomatic.

31. What does [x * 3 for x in 'ab'] return?

a) ['aaa', 'bbb']
b) ['ababab']
c) ['a', 'b', '3']
d) Error
Correct Answer: a) ['aaa', 'bbb']
Explanation:
Multiplying a string by 3 repeats its characters 3 times.

32. What is the output of [type(x) for x in [1, 'a', 3.5]]?

a) [, , ]
b) ['int', 'str', 'float']
c) [int, str, float]
d) Error
Correct Answer: a) [, , ]
Explanation:
The type() function returns the class type object for each element.

33. What does [x for x in range(5) if x > 1 and x < 4] evaluate to?

a) [2, 3]
b) [1, 2, 3, 4]
c) [2, 3, 4]
d) [1, 2, 3]
Correct Answer: a) [2, 3]
Explanation:
Numbers strictly between 1 and 4 in range(5) are 2 and 3.

34. How do you flatten a 2D matrix into a 1D list using a list comprehension?

a) [item for row in matrix for item in row]
b) [item for item in row for row in matrix]
c) [row for item in matrix for row in item]
d) [matrix[i][j] for i, j in matrix]
Correct Answer: a) [item for row in matrix for item in row]
Explanation:
The nested loop order matches standard nested for loops: outer loop first, inner loop second.

35. What is the output of [ord(c) - 64 for c in 'AB']?

a) [1, 2]
b) [65, 66]
c) [0, 1]
d) ['A', 'B']
Correct Answer: a) [1, 2]
Explanation:
ord('A') is 65 (65-64=1) and ord('B') is 66 (66-64=2).

36. What does [x for x in 'a-b' if x != '-'] return?

a) ['a', 'b']
b) ['a-', 'b']
c) ['a-b']
d) Error
Correct Answer: a) ['a', 'b']
Explanation:
Filters out the hyphen character '-'.

37. What is the output of [float(x) for x in [1, 2]]?

a) [1.0, 2.0]
b) [1, 2]
c) ['1.0', '2.0']
d) Error
Correct Answer: a) [1.0, 2.0]
Explanation:
The float() function converts integers to floating-point numbers.

38. What does [x for x in range(3) if x != 1] output?

a) [0, 2]
b) [0, 1, 2]
c) [1]
d) [0]
Correct Answer: a) [0, 2]
Explanation:
Excludes 1 from range(3).

39. Can list comprehensions iterate over file objects line by line?

a) Yes, because file objects are iterables yielding lines sequentially
b) No, files must be converted to lists first
c) Only in binary mode
d) Only with external modules
Correct Answer: a) Yes, because file objects are iterables yielding lines sequentially
Explanation:
Iterating over an open file object yields each line, making it compatible with list comprehensions.

40. What is the output of [abs(x) for x in [-3, -1, 2]]?

a) [3, 1, 2]
b) [-3, -1, 2]
c) [3, -1, 2]
d) [2, 1, 3]
Correct Answer: a) [3, 1, 2]
Explanation:
The abs() function converts negative numbers to positive.

41. What does [x for x in range(3)] + [3, 4] evaluate to?

a) [0, 1, 2, 3, 4]
b) [[0, 1, 2], [3, 4]]
c) [0, 1, 2]
d) Error
Correct Answer: a) [0, 1, 2, 3, 4]
Explanation:
The '+' operator concatenates the two lists.

42. What is the output of [x * y for x in range(2) for y in range(2)]?

a) [0, 0, 0, 1]
b) [0, 1, 2, 3]
c) [0, 1, 1, 1]
d) [1, 1, 1, 1]
Correct Answer: a) [0, 0, 0, 1]
Explanation:
Cartesian products: 0*0=0, 0*1=0, 1*0=0, 1*1=1.

43. What does [str(x) for x in range(3)] return?

a) ['0', '1', '2']
b) [0, 1, 2]
c) ('0', '1', '2')
d) '012'
Correct Answer: a) ['0', '1', '2']
Explanation:
Converts integers to string representations.

44. What is the output of [x for x in 'hello' if x in 'aeiou']?

a) ['e', 'o']
b) ['h', 'l', 'l']
c) ['a', 'e', 'i', 'o', 'u']
d) ['o', 'e']
Correct Answer: a) ['e', 'o']
Explanation:
Filters characters in 'hello' that are present in the vowel string 'aeiou'.

45. Can list comprehensions unpack tuples directly in their loop headers?

a) Yes, e.g., [a + b for a, b in [(1, 2), (3, 4)]]
b) No, unpacking is not supported
c) Only with dictionaries
d) Only in Python 2
Correct Answer: a) Yes, e.g., [a + b for a, b in [(1, 2), (3, 4)]]
Explanation:
Loop headers fully support sequence and tuple unpacking.

46. What does [x[::-1] for x in ['abc', 'def']] output?

a) ['cba', 'fed']
b) ['abc', 'def']
c) ['fed', 'cba']
d) Error
Correct Answer: a) ['cba', 'fed']
Explanation:
Slice [::-1] reverses each string.

47. What is the output of [x for x in range(3) if x]?

a) [1, 2]
b) [0, 1, 2]
c) [0, 1]
d) [2]
Correct Answer: a) [1, 2]
Explanation:
0 evaluates to False and is filtered out, leaving [1, 2].

48. What does [sum(sub) for sub in [[1, 2], [3, 4]]] return?

a) [3, 7]
b) [10]
c) [3, 4]
d) [1, 2, 3, 4]
Correct Answer: a) [3, 7]
Explanation:
Applies sum() to each sublist: sum([1,2])=3 and sum([3,4])=7.

49. What is the output of [x + 5 for x in range(3)]?

a) [5, 6, 7]
b) [0, 1, 2]
c) [6, 7, 8]
d) [5, 6]
Correct Answer: a) [5, 6, 7]
Explanation:
Adds 5 to each element in range(3) (0, 1, 2).

50. What does [x for x in range(10) if x % 3 == 0] evaluate to?

a) [0, 3, 6, 9]
b) [3, 6, 9]
c) [0, 3, 6]
d) [1, 3, 6, 9]
Correct Answer: a) [0, 3, 6, 9]
Explanation:
Numbers from 0 to 9 divisible by 3 are 0, 3, 6, 9.

51. What is the output of [x.capitalize() for x in ['python', 'java']]?

a) ['Python', 'Java']
b) ['PYTHON', 'JAVA']
c) ['python', 'java']
d) Error
Correct Answer: a) ['Python', 'Java']
Explanation:
Capitalizes the first letter of each string.

52. What does [bool(x) for x in [True, False]] return?

a) [True, False]
b) [False, True]
c) [True, True]
d) [False, False]
Correct Answer: a) [True, False]
Explanation:
Preserves the boolean values.

53. What is the output of [x for x in range(3) if x > 5]?

a) []
b) [0, 1, 2]
c) [5]
d) Error
Correct Answer: a) []
Explanation:
No numbers in range(3) satisfy x > 5.

54. What does [x.lower() for x in ['A', 'B']] output?

a) ['a', 'b']
b) ['A', 'B']
c) ['ab']
d) Error
Correct Answer: a) ['a', 'b']
Explanation:
Converts string characters to lowercase.

55. What is the output of [x for x in range(4) if x % 2 != 0]?

a) [1, 3]
b) [0, 2]
c) [1, 2, 3]
d) [0, 1, 2, 3]
Correct Answer: a) [1, 3]
Explanation:
Selects odd numbers from range(4).

56. What does [int(not x) for x in [True, False]] return?

a) [0, 1]
b) [1, 0]
c) [True, False]
d) [0, 0]
Correct Answer: a) [0, 1]
Explanation:
not True is False (0); not False is True (1).

57. What is the output of [len(x) for x in [[1], [2, 3], [4, 5, 6]]]?

a) [1, 2, 3]
b) [3, 2, 1]
c) [1, 1, 1]
d) [6]
Correct Answer: a) [1, 2, 3]
Explanation:
Computes the length of each sublist.

58. What does [x**3 for x in range(3)] evaluate to?

a) [0, 1, 8]
b) [0, 1, 27]
c) [1, 8, 27]
d) [0, 3, 9]
Correct Answer: a) [0, 1, 8]
Explanation:
Cubes of 0, 1, 2 are 0, 1, 8.

59. What is the output of [x for x in 'a1b2' if x.isdigit()]?

a) ['1', '2']
b) ['a', 'b']
c) ['1', '2', 'a', 'b']
d) [1, 2]
Correct Answer: a) ['1', '2']
Explanation:
The isdigit() method filters out non-digit characters.

60. What does [x * 2 for x in range(3) if x > 0] return?

a) [2, 4]
b) [0, 2, 4]
c) [2, 4, 6]
d) [4, 6]
Correct Answer: a) [2, 4]
Explanation:
Filtered elements > 0 are 1 and 2. Multiplying by 2 gives 2 and 4.

61. What is the output of [id(x) == id(x) for x in range(2)]?

a) [True, True]
b) [False, False]
c) [True, False]
d) Error
Correct Answer: a) [True, True]
Explanation:
An object's identity compared to itself is always True.

62. What does [x for x in range(3) for _ in range(2)] evaluate to?

a) [0, 0, 1, 1, 2, 2]
b) [0, 1, 2, 0, 1, 2]
c) [0, 1, 2]
d) [[0, 0], [1, 1], [2, 2]]
Correct Answer: a) [0, 0, 1, 1, 2, 2]
Explanation:
Each element of range(3) is repeated twice due to the inner loop over range(2).

63. What is the output of [x % 2 for x in range(4)]?

a) [0, 1, 0, 1]
b) [1, 0, 1, 0]
c) [0, 0, 1, 1]
d) [2, 2, 2, 2]
Correct Answer: a) [0, 1, 0, 1]
Explanation:
Modulo 2 results for 0, 1, 2, 3 are 0, 1, 0, 1.

64. What does [x for x in 'python' if x not in 'aeiou'] output?

a) ['p', 'y', 't', 'h', 'n']
b) ['o']
c) ['p', 't', 'h', 'n']
d) ['y', 'o']
Correct Answer: a) ['p', 'y', 't', 'h', 'n']
Explanation:
Wait, 'python' vowels are 'o'. Wait! In 'python', vowels are 'o'. So 'not in aeoiu' excludes 'o', leaving 'p', 'y', 't', 'h', 'n'. Wait, let's check: 'p', 'y', 't', 'h', 'n'. Yes.

65. What is the output of [x + 1 for x in [1, 2, 3] if x > 1]?

a) [3, 4]
b) [2, 3, 4]
c) [2, 3]
d) [1, 3, 4]
Correct Answer: a) [3, 4]
Explanation:
Filters elements > 1 (2 and 3), then adds 1 to each, yielding 3 and 4.

66. What does [x**0.5 for x in [4, 9, 16]] return?

a) [2.0, 3.0, 4.0]
b) [2, 3, 4]
c) [16.0, 81.0, 256.0]
d) [4.0, 9.0, 16.0]
Correct Answer: a) [2.0, 3.0, 4.0]
Explanation:
Square roots of 4, 9, 16 as floats.

67. What is the output of [x for x in range(3) if None]?

a) []
b) [0, 1, 2]
c) [None, None, None]
d) Error
Correct Answer: a) []
Explanation:
None is falsy, so the filter condition is never met.

68. What does [x for x in range(5) if x % 2 == 0] produce?

a) [0, 2, 4]
b) [1, 3]
c) [0, 1, 2, 3, 4]
d) [2, 4]
Correct Answer: a) [0, 2, 4]
Explanation:
Even numbers in range(5) are 0, 2, 4.

69. What is the output of [x.split() for x in ['hello world', 'python code']]?

a) [['hello', 'world'], ['python', 'code']]
b) ['hello', 'world', 'python', 'code']
c) ['hello world', 'python code']
d) [('hello', 'world'), ('python', 'code')]
Correct Answer: a) [['hello', 'world'], ['python', 'code']]
Explanation:
Splits each string into a list of words, creating a nested list structure.

70. What does [not x for x in [True, False, True]] return?

a) [False, True, False]
b) [True, False, True]
c) [False, False, False]
d) [True, True, True]
Correct Answer: a) [False, True, False]
Explanation:
Inverts each boolean value using the 'not' operator.

71. What is the output of [list(range(x)) for x in range(3)]?

a) [[], [0], [0, 1]]
b) [[0], [0, 1], [0, 1, 2]]
c) [[1], [1, 2]]
d) [0, 1, 2]
Correct Answer: a) [[], [0], [0, 1]]
Explanation:
For x=0: []; x=1: [0]; x=2: [0, 1].

72. What does [x for x in range(3)] * 0 evaluate to?

a) []
b) [0, 1, 2]
c) None
d) Error
Correct Answer: a) []
Explanation:
Multiplying a list by 0 yields an empty list.

73. What is the output of [len(str(x)) for x in [7, 77, 777]]?

a) [1, 2, 3]
b) [3, 2, 1]
c) [7, 77, 777]
d) [1, 1, 1]
Correct Answer: a) [1, 2, 3]
Explanation:
String lengths of integers converted to strings.

74. What does [x for x in 'ab' for y in '1'] output?

a) ['a1', 'b1']
b) ['ab1']
c) ['a', 'b', '1']
d) [('a', '1'), ('b', '1')]
Correct Answer: a) ['a1', 'b1']
Explanation:
Combines characters from 'ab' with '1'.

75. What is the output of [bool(x) for x in [1, 0, -1]]?

a) [True, False, True]
b) [True, True, True]
c) [False, True, False]
d) [False, False, False]
Correct Answer: a) [True, False, True]
Explanation:
1 and -1 are truthy, 0 is falsy.

76. What does [x for x in range(5) if x % 3 == 0] return?

a) [0, 3]
b) [3]
c) [0, 1, 3]
d) [0, 2, 4]
Correct Answer: a) [0, 3]
Explanation:
Numbers in range(5) divisible by 3 are 0 and 3.

77. What is the output of [x for x in range(3) if x != 0]?

a) [1, 2]
b) [0, 1, 2]
c) [0]
d) [2]
Correct Answer: a) [1, 2]
Explanation:
Excludes 0 from range(3).

78. What does [x + 2 for x in range(3)] output?

a) [2, 3, 4]
b) [1, 2, 3]
c) [0, 1, 2]
d) [3, 4, 5]
Correct Answer: a) [2, 3, 4]
Explanation:
Adds 2 to each element in range(3).

79. What is the output of [x for x in range(3) if x > 1]?

a) [2]
b) [1, 2]
c) [3]
d) []
Correct Answer: a) [2]
Explanation:
Only 2 in range(3) is strictly greater than 1.

80. What does [x * 2 for x in (1, 2, 3)] return?

a) [2, 4, 6]
b) (2, 4, 6)
c) [1, 2, 3, 1, 2, 3]
d) Error
Correct Answer: a) [2, 4, 6]
Explanation:
List comprehensions always return a list object, multiplying each tuple element by 2.

81. What is the output of [i for i in 'xyz']?

a) ['x', 'y', 'z']
b) ['xyz']
c) ('x', 'y', 'z')
d) xyz
Correct Answer: a) ['x', 'y', 'z']
Explanation:
Iterates over string characters into a list.

82. What does [x for x in range(4) if x % 2 == 1] output?

a) [1, 3]
b) [0, 2]
c) [1, 2, 3]
d) [0, 1, 2, 3]
Correct Answer: a) [1, 3]
Explanation:
Selects odd numbers from range(4).

83. What is the output of [x for x in range(3) for y in range(2)]?

a) [0, 0, 1, 1, 2, 2]
b) [0, 1, 2]
c) [0, 1, 0, 1, 0, 1]
d) [[0, 1], [0, 1], [0, 1]]
Correct Answer: a) [0, 0, 1, 1, 2, 2]
Explanation:
For each outer element (0, 1, 2), the inner loop runs twice, yielding duplicates of each outer element.

84. What does [x**2 for x in range(3)] evaluate to?

a) [0, 1, 4]
b) [1, 4, 9]
c) [0, 1, 9]
d) [2, 4, 6]
Correct Answer: a) [0, 1, 4]
Explanation:
Squares of 0, 1, 2 are 0, 1, 4.

85. What is the output of [ord(c) for c in 'AB']?

a) [65, 66]
b) [97, 98]
c) [1, 2]
d) ['A', 'B']
Correct Answer: a) [65, 66]
Explanation:
Unicode integer values for 'A' and 'B'.

86. What does [x for x in range(3) if x == 1] return?

a) [1]
b) [0, 1, 2]
c) [0, 2]
d) []
Correct Answer: a) [1]
Explanation:
Filters for elements equal to 1.

87. What is the output of [x + 10 for x in range(2)]?

a) [10, 11]
b) [10, 12]
c) [11, 12]
d) [0, 1, 10]
Correct Answer: a) [10, 11]
Explanation:
Adds 10 to 0 and 1.

88. What does [len(x) for x in ['', 'a', 'ab']] output?

a) [0, 1, 2]
b) [1, 2, 3]
c) [0, 0, 0]
d) [2, 1, 0]
Correct Answer: a) [0, 1, 2]
Explanation:
Lengths of empty string, 'a', and 'ab'.

89. What is the output of [x for x in range(3) if x < 2]?

a) [0, 1]
b) [0, 1, 2]
c) [2]
d) []
Correct Answer: a) [0, 1]
Explanation:
Numbers in range(3) strictly less than 2 are 0 and 1.

90. What does [x * 10 for x in range(3)] return?

a) [0, 10, 20]
b) [10, 20, 30]
c) [0, 1, 20]
d) [10, 20]
Correct Answer: a) [0, 10, 20]
Explanation:
Multiplies 0, 1, 2 by 10.

91. What is the output of [type(x).**name** for x in [1, 'a', 2.0]]?

a) ['int', 'str', 'float']
b) [, , ]
c) [int, str, float]
d) Error
Correct Answer: a) ['int', 'str', 'float']
Explanation:
Retrieves the string name of the class type for each object.

92. What does [x for x in range(4) if x > 2] evaluate to?

a) [3]
b) [2, 3]
c) [4]
d) []
Correct Answer: a) [3]
Explanation:
Only 3 in range(4) is strictly greater than 2.

93. What is the output of [x for x in range(2) for y in range(2) if x == y]?

a) [0, 1]
b) [0, 0]
c) [1, 1]
d) [0, 1, 2]
Correct Answer: a) [0, 1]
Explanation:
Pairs where x equals y: (0,0) yielding 0, and (1,1) yielding 1.
← Previous: Python Functions MCQs
Next →: Python OOP MCQs
NewTop Python Fundamentals MCQs & Answers for Beginners

Top Python Fundamentals MCQs & Answers for Beginners

Python is a dynamically typed, high-level programming language created by Guido van Rossum in 1991. Renowned for its clear syntax…

By MCQs Generator
NewPython OOP MCQs

Python OOP MCQs

Object-Oriented Programming (OOP) in Python is a programming paradigm that uses classes and objects to model real world entities, promoting…

By MCQs Generator
NewLatest Python Loops MCQs

Latest Python Loops MCQs

Loops in Python provide the fundamental mechanism for executing a block of code repeatedly until a specified condition is met…

By MCQs Generator