ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print(list(map(round, [0.5, 1.5, 2.5, -0.5, -1.5])), list(map(int, [0.5, 1.5, 2.5, -0.5, -1.5])))
- A. [0, 2, 2, 0, -2] [0, 1, 2, 0, -1]
- B. [0, 2, 2, 0, -2] [0, 1, 3, 0, -1]
- C. [1, 2, 3, -1, -2] [0, 1, 2, 0, -1]
- D. [0, 2, 2, 0, -1] [0, 1, 2, 0, -1]
Correct answer: A. [0, 2, 2, 0, -2] [0, 1, 2, 0, -1]
round uses half-to-even, so 0.5 becomes 0, 1.5 becomes 2, 2.5 becomes 2, and -1.5 becomes -2. int truncates toward zero, so the same halves give 0, 1, 2, 0 and -1.
More Python Programming questions
- What is printed? python print(max(['10', '9']), max(['10', '9'], key=len), sorted(['10', '9', '100'], key=lambda s: (len(s), s)))
- What is printed? python print(int(True) - ~True, 4 >> True, True << 3, True + True True)
- What is printed? python print('5' 3 > '10' 2, 5 3 > 10 2, '55' < '9', 55 < 9)
- What is printed? python print(float('inf') > 21000, -float('inf') < -(21000), float('inf') == float('inf') + 1)
- What is printed? python print(repr(0.1), repr(1 / 3), repr(2 / 3), repr(1e-7), repr(100.0))
- What is printed? python print(0.1 3 != 0.3, abs(0.1 3 - 0.3) < 2-52, abs(0.1 3 - 0.3) < 2-54)