ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print((-7) % 3, 7 % -3, (-7) % -3, -7 % 3 * 2)
- A. 2 -2 -1 4
- B. -1 2 -2 -4
- C. 2 -2 -1 3
- 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
- What is printed? python print(-6 & 0xFF, -6 | 3, -6 ^ -3, ~-6 >> 1)
- What is printed? python print(len('ab' 3 0 + 'c'), ('%5.2f' % 3.14159).strip(), '%-5s|' % 'ab', '%05d' % -42)
- What is printed? python print(-7 // 2, -7 % 2, -7 // 2 2 + -7 % 2)
- What is printed? python print(7 // -2, -7 // 2, 7 % -2, divmod(-7, -2))
- What is printed? python print(round(2.5), round(3.5), round(-0.5), round(0.5 + 1e-16))
- What is printed? python print('ß'.upper(), 'Dž'.lower(), len('ß'.upper()), 'İ'.lower() == 'i')