ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(-7.5 // 2, -7.5 % 2, 7 // -2.0, 7 % -2.0)
- A. -3.0 -1.5 -3.0 1.0
- B. -4 0.5 -4 -1
- C. -4.0 0.5 -3.0 1.0
- D. -4.0 0.5 -4.0 -1.0
Correct answer: D. -4.0 0.5 -4.0 -1.0
// floors toward minus infinity even for floats, and the result stays a float: -3.75 floors to -4.0, and 7 / -2.0 = -3.5 floors to -4.0. % is chosen so that a == (a // b) * b + a % b, which gives it the sign of the divisor: -7.5 - (-8.0) = 0.5 and 7 - 8.0 = -1.0. The -3.0 answers are C-style truncation.
More Python Programming questions
- What is printed? python n = [1] print(1 in n == True, (1 in n) == True, 0 < 1 in n)
- What is printed? python class P: slots = ("x",) def init(self): self.x = 1 p = P() try: p.y = 2 except AttributeError: print("AE", end="…
- What is printed? python class T: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): self.c = max(0…
- What is printed? python def acc(): total = 0 while True: try: x = yield total except ValueError: total = 0 continue if x is None: break…
- What is printed? python class Base: def init(self): self.log = ["Base"] class L(Base): def init(self): super().init() self.log.append("L")…
- What is printed? python class D: def get(self, obj, typ): return "desc" class DD(D): def set(self, obj, v): pass class C: a = D() b = DD()…