Data Structures & AlgorithmsHardTCS NQT Digital · Python semantics

Problem: What does the following print?

Problem: What does the following print?
print(list(map(lambda x: x ** 2, filter(lambda x: x % 2, range(6)))))
What is printed?
  1. A. [1, 4, 9, 16, 25]
  2. B. [1, 9, 25]
  3. C. [0, 4, 16]
  4. D. [1, 9, 25, 0]

Correct answer: B. [1, 9, 25]

filter keeps the odd values 1, 3 and 5 from range(6), and squaring gives 1, 9 and 25. Testing for even values instead gives the trap [0, 4, 16].

More Coding Interview Problems questions

Exams that ask this topic