ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
import math print(int(-0.5), int(-1.9), math.floor(-1.1), round(-1.5))
- A. 0 -1 -2 -2
- B. 0 -2 -2 -2
- C. -1 -1 -2 -2
- D. 0 -1 -1 -2
Correct answer: A. 0 -1 -2 -2
int() truncates toward zero, so -0.5 becomes 0 and -1.9 becomes -1. floor rounds toward negative infinity, giving -2, and round(-1.5) rounds to the even neighbour, -2.
More Python Programming questions
- What is printed? python print(7 & -8, 7 | -8, 7 ^ -8, ~7 & 0xFF)
- What is printed? python print(float(' 1000.5 '), float('infinity') > 1e308, float('-nan') != float('-nan'), int(' +0012 '))
- What is printed? python print(b'abc'[0], bytes([65, 66]), bytes.fromhex('41 42'), len(b'a' 2 + b'x00'))
- What is printed? python print(sorted(['b', 'A', 'c', 'B'], key=str.casefold, reverse=True))
- What is printed? python try: print(sorted([3, None, 1])) except TypeError: print('cannot compare')
- What is printed? python print('%(a)s-%(b)05.1f' % {'a': 'x', 'b': 3.14159})