ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print([y for x in [1, 2, 3] if (y := x + 1) % 2 == 0])
- A. [4]
- B. [2, 4]
- C. [2, 3, 4]
- D. [3]
Correct answer: B. [2, 4]
y is x + 1, so the values are 2, 3, 4. Only the even ones, 2 and 4, pass the filter, and the condition is evaluated before y is used.
More Python Programming questions
- What is printed? python print(all(i < j for i, j in zip([1, 2, 3], [2, 3, 3])))
- What is printed? python print(b'abc'[0], b'abc'[0:1], len('é'.encode()))
- What is printed? python def ack(m, n): if m == 0: return n + 1 if n == 0: return ack(m - 1, 1) return ack(m - 1, ack(m, n - 1))…
- What is printed? python print(list(range(-3, 3)), list(zip(range(3), 'ab')))
- Why is del a[0] on a Python list O(n), while a.pop() is O(1)?
- For a sliding-window maximum over an array of n = 2 10^5 values, which Python structure gives an O(n) total algorithm?