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?
- A. [1, 4, 9, 16, 25]
- B. [1, 9, 25]
- C. [0, 4, 16]
- 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
- Problem: What does the following print? python print(bool([]), bool([0]), bool(0.0), bool('False')) What is printed?
- Problem: What does the following print? python a = (1,) print(a 3, len(a 0)) What is printed?
- Problem: What does the following print? python print(list('abc')[::-1] == ['c', 'b', 'a'], 'abc'.index('c')) What is printed?
- Problem: What does the following print? python x = 5 x <<= 2 x |= 1 print(x) What is printed?
- Problem: What does the following print? python print(any([]), all([])) What is printed?
- Problem: What does the following print? python print(10 20 // 10 18) What is printed?