ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print((-7) % 3, 7 % -3, (-7) % -3, -7 % 3 * 2)
  1. A. 2 -2 -1 4
  2. B. -1 2 -2 -4
  3. C. 2 -2 -1 3
  4. D. 1 2 1 4

Correct answer: A. 2 -2 -1 4

Python's % takes the sign of the divisor, so -7 % 3 is 2 and 7 % -3 is -2. Then -7 % 3 * 2 is 2 * 2 = 4, because % and * share precedence and run left to right.

More Python Programming questions

Exams that ask this topic