ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
print([i for i in range(10) if i % 3 == 0][::-1], [i * i for i in range(5)][-2:], len([]) == 0)
  1. A. [0, 3, 6, 9] [9, 16] True
  2. B. [9, 6, 3, 0] [9, 16] True
  3. C. [9, 6, 3, 0] [4, 9] True
  4. D. [9, 6, 3, 0] [9, 16] False

Correct answer: B. [9, 6, 3, 0] [9, 16] True

The filter keeps 0, 3, 6 and 9, and reversing puts 9 first. The last two squares are 9 and 16, and an empty list has length zero.

More Python Programming questions

Exams that ask this topic