ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
b = "Hé".encode("utf-8")
print(len("Hé"), len(b), b[1], b.decode("latin-1"))
  1. A. 2 2 233 Hé
  2. B. 2 3 195 Hé
  3. C. 2 3 233 Hé
  4. D. 3 3 195 Hé

Correct answer: B. 2 3 195 Hé

'é' takes two bytes in UTF-8 (0xC3 0xA9), so the bytes object has length 3 and b[1] is the int 195. Decoding those bytes as Latin-1 maps each byte to one character, producing the mojibake 'Hé'.

More Python Programming questions

Exams that ask this topic