ProgrammingHardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
def mod(a, m):
r = a % m
return r + m if r < 0 else r
print(mod(-1, 5), mod(-5, 5), mod(-6, 5))
print(-1 % 5, (-1) % 5 == 4, -1 % -5)- A. 4 False -1
- B. 4 True -1
- C. 1 True -1
- D. 4 True 4
Correct answer: B. 4 True -1
Python's % already returns a result with the sign of the divisor, so the helper's correction never runs. -1 % 5 is 4 and -1 % -5 is -1, since the result takes the divisor's sign.
More Python Programming questions
- What is printed on the first line? python M = 109 + 7 print((-1) % M, (262) % M, pow(3, M - 2, M) 3 % M) print(((-5) % 3) ((-5) % 3), (-5…
- What is printed on the second line? python a, b = -13, 4 print(a // b, a % b, a // b b + a % b == a) print(int(a / b), int(-3.25), -3.25…
- What is printed on the first line? python n = -17 print(n >> 2, n // 4, n & 3, n % 4) print(n << 1, (n << 1) >> 1 == n, ~n)
- What is printed on the first line? python x = 1 for in range(70): x = 2 print(x, x.bitlength(), x == 270) print(x.bitlength() - 1, (x…
- What is printed on the first line? python import math print(math.factorial(20) == 2432902008176640000, len(str(math.factorial(25))))…
- What is printed on the first line? python n = 264 print(n, n.bitlength(), hex(n - 1), oct(8), bin(5)) print(int(hex(n)[2:], 16) == n, -263…