ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def f(a, b):
    return a // b if b else None
vals = [(7, 2), (-7, 2), (7, 0), (7.0, -2)]
print([f(*p) for p in vals], [divmod(a, b)[1] for a, b in vals if b])
  1. A. [3, -3, None, -3.0] [1, -1, 1.0]
  2. B. [3, -4, None, -4.0] [1, 1, -1.0]
  3. C. [3, -4, None, -3.5] [1, 1, -1.0]
  4. D. [3, -4, None, -4] [1, 1, -1]

Correct answer: B. [3, -4, None, -4.0] [1, 1, -1.0]

// floors toward minus infinity, also for floats (7.0 // -2 = -4.0). The remainder takes the sign of the divisor: -7 % 2 = 1 and 7.0 % -2 = -1.0.

More Python Programming questions

Exams that ask this topic