ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
print([x for x in range(20) if x % 2 == 0 if x % 3 == 0])
  1. A. [0, 6, 12, 18]
  2. B. [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
  3. C. [0, 3, 6, 9, 12, 15, 18]
  4. D. [6, 12, 18]

Correct answer: A. [0, 6, 12, 18]

Two consecutive if-clauses in a comprehension act as an AND: both conditions must hold. Only multiples of 6 (divisible by both 2 and 3) survive: 0, 6, 12, 18.

More Python Programming questions

Exams that ask this topic