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')))- A. b'?' b'' hello 2
- B. b'?' b'' hllo 2
- C. b'é' b'' hllo 2
- 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
- What is printed? python print(10 50 // 3 20 % 100, len(bin(10 50)) - 2, (10 50).bitlength())
- What is printed? python print(1 < 2 < 3 == 3.0 != 4, 1 < 2 > 3, (1 < 2) > 3, 'a' < 'b' < 'a')
- What is printed? python x = 0b1011 print(x & ~(1 << 1), x ^ (1 << 2), x | (1 << 4), (x >> 1) & 1)
- What is printed? python words = ['bb', 'a', 'ccc', 'aa'] print(sorted(words, key=lambda w: (-len(w), w)))
- What is printed? python pairs = [(1, 'b'), (1, 'a'), (0, 'c')] print(sorted(pairs, key=lambda p: p[0]), sorted(pairs))
- What is printed? python print(int('0b11', 0) + int('0o11', 0) + int('0x11', 0), int('11', 16), int('-0b11', 0))