What does [x * x for x in range(4)] produce?
-
A
[0, 1, 4, 9]
Correct answer
-
B
[0, 2, 4, 6]
-
C
[0, 1, 2, 3]
-
D
[1, 4, 9, 16]
Correct answer
A. [0, 1, 4, 9]
16 views
Explanation
range(4) yields 0, 1, 2 and 3, and the list comprehension squares each one. Forgetting that range starts at 0 gives the wrong answer [1, 4, 9, 16].
Reference
Asked in: NTS, PPSC, Python Developer Interviews
Doubts & discussion
Not sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ