ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print([i ^ (i >> 1) for i in range(4)])
  1. A. [0, 1, 3, 2]
  2. B. [0, 1, 2, 3]
  3. C. [0, 2, 3, 1]
  4. D. [1, 0, 3, 2]

Correct answer: A. [0, 1, 3, 2]

The formula i XOR (i >> 1) is the binary reflected Gray code. Consecutive values differ in exactly one bit, here 00, 01, 11, 10.

More Python Programming questions

Exams that ask this topic