ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print('é'.encode('ascii', 'replace'), 'é'.encode('ascii', 'ignore'), b'h\xc3\xa9llo'.decode('ascii', 'ignore'), len(b'\xff\xfe'.decode('utf-8', 'replace')))
  1. A. b'?' b'' hello 2
  2. B. b'?' b'' hllo 2
  3. C. b'é' b'' hllo 2
  4. D. b'?' b'' hllo 1

Correct answer: B. b'?' b'' hllo 2

replace turns an unencodable character into '?', and ignore drops it. Decoding the two bytes c3 a9 with the ascii codec and ignore drops them too, leaving hllo, and each invalid byte in the utf-8 input becomes one U+FFFD.

More Python Programming questions

Exams that ask this topic