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))
- A. 3 [1, 2, 4] 2
- B. 2 [1, 2, 2, 4] 2
- C. 3 [1, 2, 2, 4] 2
- 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
- What is printed? python data = {'a': 1} print(data.get('b', 0) + 1, data.get('a') + 1, 'a' in data, 1 in data.values())
- What is printed? python print(3 in range(5), 5 in range(5), range(0, 6, 2) == range(0, 7, 2), list(range(3, 0, -1)))
- What is printed? python print(10 % 4 == 2, -10 % 4 == 2, 10 // -4, 10 / -4)
- What is printed? python s = 'abcdef' print(s[-2:], s[:-2], s[-10:], s[::-2][0])
- What is printed? python print('abc' < 'abd' and 'x' or 'y', [] or 'empty', 0 or 0.0 or 'z')
- What is printed? python print(len({'a': 1, 'a': 2}), {'a': 1, 'a': 2}, list({1, 2, 1}), sorted({3, 1, 2} & {2, 3, 4}))