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])- A. [3, -3, None, -3.0] [1, -1, 1.0]
- B. [3, -4, None, -4.0] [1, 1, -1.0]
- C. [3, -4, None, -3.5] [1, 1, -1.0]
- 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
- What is printed? python print(int("12", 8), oct(64), hex(-255), int("z", 36), format(10, "b").zfill(6))
- What is printed? python x = 2 63 print(x.bitlength(), (x - 1).bitlength(), x >> 62, (-1) >> 100, 1 << 0, (5).tobytes(2, "big"))
- What is printed? python import math print(math.floor(-2.5), math.ceil(-2.5), math.trunc(-2.5), int(-2.5), round(-2.5), -2.5 // 1)
- What is printed? python print(3 "ab" == "ab" 3, [1] 3 is [1] 3, "%" in "100%", "100%%" % (), f"{{x}}")
- What is printed? python x = 10 s = f"{x=}, {x2=}, {x:04}, {'a' if x > 5 else 'b'}" print(s)
- What is printed? python def f(): yield 1 g = f() print(type(f).name, type(g).name, hasattr(g, "next"), iter(g) is g, callable(g))