ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print(7 // -2, -7 // 2, 7 % -2, divmod(-7, -2))
- A. -3 -4 -1 (3, -1)
- B. -4 -4 -1 (3, -1)
- C. -4 -3 1 (3, -1)
- D. -4 -4 -2 (3, -1)
Correct answer: B. -4 -4 -1 (3, -1)
Floor division gives 7 // -2 = -4 because the quotient -3.5 is rounded down. The remainder 7 % -2 must have the divisor's sign, so it is -1, and divmod(-7, -2) is (3, -1) since 3 * -2 + -1 = -7.
More Python Programming questions
- 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')
- What is printed? python import math print(f'{-0.0}', -0.0 == 0.0, math.copysign(1, -0.0))
- What is printed? python print(1 == 1.0, hash(1) == hash(1.0), {1: 'a'}[1.0], True + 1.0)
- What is printed? python print(sorted([3, -3, 2, -2, 1], key=abs))
- What is printed? python print(f'{-1.5:+.1f}', f'{0.5:.0f}', f'{1.5:.0f}', f'{-0.04:.1f}')