ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
print(len([x for x in range(3) for y in range(x)]), [x * y for x in range(3) for y in (1, 2) if x], sum(1 for i in range(1, 4) if i % 2))
  1. A. 3 [1, 2, 4] 2
  2. B. 2 [1, 2, 2, 4] 2
  3. C. 3 [1, 2, 2, 4] 2
  4. D. 3 [1, 2, 2, 4] 3

Correct answer: C. 3 [1, 2, 2, 4] 2

The first comprehension has one pair for x = 1 and two for x = 2, giving three. With the filter x must be nonzero, giving products 1, 2, 2 and 4. Odd numbers among 1 to 3 are 1 and 3.

More Python Programming questions

Exams that ask this topic