Loading

Quipoin Menu

Learn • Practice • Grow

python / List Comprehension
mcq
Direction: Choose the correct option

Q1.

What is list comprehension in Python?
A. A way to read lists from files
B. A type of loop
C. A concise way to create lists
D. A function to sort lists
Direction: Choose the correct option

Q2.

What is the output of `[x for x in range(5)]`?
A. (0,1,2,3,4)
B. [1,2,3,4,5]
C. [0,1,2,3,4,5]
D. [0,1,2,3,4]
Direction: Choose the correct option

Q3.

What does `[x**2 for x in range(3)]` produce?
A. [1,4,9]
B. [0,1,2]
C. [0,2,4]
D. [0,1,4]
Direction: Choose the correct option

Q4.

How do you include a condition in list comprehension?
A. Using `filter`
B. Using `if` inside the loop
C. Using `else`
D. Using `if` at the end
Direction: Choose the correct option

Q5.

What is the result of `[i for i in range(3) if i>1]`?
A. [1,2]
B. [2]
C. []
D. [0,1]
Direction: Choose the correct option

Q6.

Can list comprehension be used with multiple for loops?
A. No, only one loop
B. Only with `zip`
C. Only with `enumerate`
D. Yes, e.g., `[(x,y) for x in [1,2] for y in [3,4]]`
Direction: Choose the correct option

Q7.

What is the output of `[x for x in 'abc']`?
A. ['a','b','c']
B. ['a','b','c']
C. ['a','b','c']
D. ['abc']
Direction: Choose the correct option

Q8.

Which of the following is equivalent to `list(map(lambda x: x*2, [1,2,3]))`?
A. [x*2 for x in (1,2,3)]
B. All of the above
C. [x*2 for x in [1,2,3]]
D. [x*2 for x in range(3)]
Direction: Choose the correct option

Q9.

What does `[x if x>0 else 0 for x in [-1,2,0]]` produce?
A. [-1,2,0]
B. [0,0,0]
C. [0,2]
D. [0,2,0]
Direction: Choose the correct option

Q10.

What is the output of `[x for x in range(3)] + [x for x in range(3,6)]`?
A. [0,1,2,3,4,5]
B. [0,1,2,3,4,5]
C. [0,1,2,3,4,5]
D. [0,1,2,3,4,5]