ProgrammingEasyTCS NQT · Wipro pattern

What is printed?

What is printed?
print([x * x for x in range(5) if x % 2])
  1. A. [0, 4, 16]
  2. B. [1, 9]
  3. C. [0, 1, 9]
  4. D. [1, 4, 9, 16]

Correct answer: B. [1, 9]

x % 2 is truthy only for odd x, so the filter keeps 1 and 3 from range(5). Their squares are 1 and 9.

More Python Programming questions

Exams that ask this topic