ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print([x % 3 for x in [-7, -3, 0, 4]])
  1. A. [2, 0, 0, 1]
  2. B. [-1, 0, 0, 1]
  3. C. [1, 0, 2, 1]
  4. D. [2, 0, 0, -2]

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

Python's % takes the sign of the divisor, so every result lies in 0, 1, 2. -7 % 3 is 2, -3 % 3 is 0, 0 % 3 is 0 and 4 % 3 is 1.

More Python Programming questions

Exams that ask this topic