ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(-7 % 3, 7 % -3, -7 // 3, 7 // -3)
- A. 2 -2 -3 -3
- B. 2 -2 -2 -3
- C. 1 2 -3 -3
- D. 2 -2 -3 -2
Correct answer: A. 2 -2 -3 -3
Python's % takes the sign of the divisor and // floors toward minus infinity, so -7 % 3 = 2, 7 % -3 = -2 and -7 // 3 = -3. A C-style truncating division would print -2 for -7 // 3, which is the trap.
More Python Programming questions
- What is printed? python print(pow(-7, 3, 5), (-7) 2 % 5, -7 2 % 5)
- What is printed? python print(2100 % 7, pow(2, 100, 7), pow(2, 100, 1))
- What is printed? python x = 1020 print(x // 3, x / 3 == x // 3, x % 3)
- What is printed? python n = int('9' 30) print(len(str(n + 1)), (n + 1) - 1030)
- What is printed? python print(5 & 3, 5 | 3, 5 ^ 3, ~5, 5 << 2, -5 >> 1)
- What is printed? python print(-1 & 0xFF, -8 >> 1, -1 >> 100, -9 & 7)