ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = 0
for i in range(1, 9):
    x ^= i
print(x, 5 ^ 9 ^ 5, (1 << 3) - 1 & 6)
  1. A. 8 5 6
  2. B. 8 9 6
  3. C. 8 9 0
  4. D. 0 9 6

Correct answer: B. 8 9 6

The XOR of 1 through 8 is 8, and in 5 ^ 9 ^ 5 the two 5s cancel to leave 9. In the last term - binds tighter than &, so it is (8 - 1) & 6, which is 7 & 6 = 6.

More Python Programming questions

Exams that ask this topic