ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
print([format(i ^ (i >> 1), '02b') for i in range(4)])
  1. A. ['00', '01', '10', '11']
  2. B. ['00', '01', '11', '10']
  3. C. ['00', '10', '11', '01']
  4. D. ['11', '10', '00', '01']

Correct answer: B. ['00', '01', '11', '10']

i XOR (i >> 1) gives the reflected Gray code, in which adjacent values differ by one bit: 00, 01, 11, 10.

More Python Programming questions

Exams that ask this topic