ProgrammingModerateCodeforces-style · placement coding round

What is printed?

What is printed?
from functools import reduce
import operator
a = [4, 1, 2, 1, 2]
print(reduce(operator.xor, a), reduce(operator.xor, a[1:], 0), reduce(lambda x, y: x ^ y, [], 7))
  1. A. 4 0 7
  2. B. 4 1 7
  3. C. 0 0 7
  4. D. 4 0 0

Correct answer: A. 4 0 7

XOR cancels equal pairs, so 1, 2, 1, 2 cancel to 0 and the single 4 remains. Folding an empty list with initial value 7 returns 7.

More Python Programming questions

Exams that ask this topic