ProgrammingModerateCodeforces-style · placement coding round

What is printed?

What is printed?
def ceil_div(a, b):
    return -(-a // b)
print(ceil_div(7, 2), ceil_div(-7, 2), ceil_div(6, 3), ceil_div(1, 5))
  1. A. 4 -4 2 1
  2. B. 4 -3 2 1
  3. C. 3 -3 2 1
  4. D. 4 -3 2 0

Correct answer: B. 4 -3 2 1

For positive b, -(-a // b) is the ceiling of a / b, because floor division of -a rounds toward minus infinity. ceil(7/2) is 4, ceil(-7/2) is -3 and ceil(1/5) is 1.

More Python Programming questions

Exams that ask this topic