ProgrammingModerateTCS NQT · Accenture
What is printed?
What is printed?
print([y for x in [[1, 2], [3], []] for y in x if y % 2])
- A. [1, 2, 3]
- B. [1, 3]
- C. [2]
- D. [1, 3, 0]
Correct answer: B. [1, 3]
The first for clause walks the sublists, the second walks their items, and the filter keeps odd values. The empty sublist contributes nothing.
More Python Programming questions
- What is printed? python out = [] for i in range(3): try: if i == 1: continue out.append(i) finally: out.append('f') print(out)
- What is printed? python print(int('0x1f', 16), int('1f', 16), int('0o17', 8))
- What is printed? python print(divmod(-9, 4), -9 % 4, round(-9 / 4))
- What is printed? python print(len(str(float(1016))), str(1016 1.0), len(str(1016)))
- What is printed? python print(0.1 + 0.7 == 0.8, 0.8 - 0.7, 0.3 / 0.1, int(0.3 / 0.1))
- What is printed? python print(1.0 == 1, 1.5 // 0.5, 1.5 % 0.4, 2.0 -1074 > 0, 2.0 -1075 == 0)