Questions: List Comprehensions

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

What does `[x * 2 for x in range(5) if x % 2 != 0]` evaluate to?

A[0, 2, 4, 6, 8]
B[2, 6]
C[1, 3]
D[2, 4, 6, 8, 10]
Question 2 Multiple Choice

A developer uses a list comprehension `[send_email(user) for user in mailing_list]` to send emails. What is wrong with this usage?

AList comprehensions cannot call functions — only expressions are allowed
BList comprehensions should express data transformation without side effects; using one to drive API calls obscures intent and produces an unwanted list of return values
CThe comprehension is missing a filter condition, so it will fail on empty lists
DThis will execute correctly but is slower than a for loop for external calls
Question 3 True / False

`[x ** 2 for x in numbers]` and `(x ** 2 for x in numbers)` are interchangeable — both yield the same values and behave identically.

TTrue
FFalse
Question 4 True / False

A list comprehension `[f(x) for x in seq if pred(x)]` is semantically equivalent to a for loop that appends to a result list only when the condition is true.

TTrue
FFalse
Question 5 Short Answer

What does it mean for code to be 'declarative' rather than 'imperative,' and why is this considered an advantage of list comprehensions over equivalent for loops?

Think about your answer, then reveal below.