ProgrammingHardGATE CS & IT · Infosys

What is printed?

What is printed?
from itertools import combinations
a = [1, 2, 3, 4]
cnt = 0
for r in range(1, len(a) + 1):
    for c in combinations(a, r):
        x = 0
        for v in c:
            x ^= v
        if x == 0:
            cnt += 1
print(cnt)
  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

Only {1, 2, 3} has XOR zero, because 1 ^ 2 ^ 3 = 0. Every other non-empty subset of {1, 2, 3, 4} has a non-zero XOR.

More Python Programming questions

Exams that ask this topic