ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
x = 5 print(x.__add__(3), (5).__mod__(-3), (-5).__mod__(3), int.__floordiv__(-5, 3))
- A. 8 1 1 3
- B. 8 1 1 1
- C. 8 -1 1 -2
- D. 9 1 1 3
Correct answer: C. 8 -1 1 -2
Dunder methods behave like the operators they implement. 5 % -3 is -1 since the sign follows the divisor, while -5 % 3 is 1, and -5 // 3 floors to -2.
More Python Programming questions
- What is printed? python print('ab'.rjust(5, ''), 'ab'.ljust(5, '-'), 'ab'.center(5, '.'), 'ab'.center(6, '.'))
- What is printed? python print(f'{"x"!r}', '%s and %r' % ('x', 'x'), '{!s} {!r}'.format('x', 'x'))
- What is printed? python s = 'abc' print(s 2 == 'abcabc', s[1:] + s[:1], s[-1:] + s[:-1], len('ab' 0))
- What is printed? python print(int('007'), int('-07'), int('01'), int('00'))
- What is printed? python print(float('1e3') == 1000, float('1E-2') 100, float(' .5 '), float('5.'))
- What is printed? python print('{0:.2%}'.format(0.12345), '{:,}'.format(1234), '{:e}'.format(0), '{:.0f}'.format(0.5))