ProgrammingVery hardJEE Advanced · CAT placement

What is printed?

What is printed?
try:
    print((300).to_bytes(1, 'big'))
except OverflowError:
    print('overflow')
print((255).to_bytes(1, 'big'), int.from_bytes(b'', 'big'))
  1. A. overflow b'\x2c' 0
  2. B. overflow b'\xff' 0
  3. C. overflow b'\xff' 1
  4. D. b'\xff' 0 overflow

Correct answer: B. overflow b'\xff' 0

300 needs two bytes, so to_bytes with length 1 raises OverflowError. 255 fits in one byte as 0xff, and an empty byte string converts to 0.

More Python Programming questions

Exams that ask this topic