PCAP-31-03 practice question 10 of 25

Which of the equations are True? (Select two answers)

Full set: 147 questions
Answer options
A: chr (ord (x)) = = x Correct answer
B: ord (ord (x)) = = x
C: chr (chr (x)) = = x
D: ord (chr (x)) = = x Correct answer
Correct Answer:
A. chr (ord (x)) = = x; D. ord (chr (x)) = = x
Explanation

For a one-character string x, ord(x) gives its Unicode code point (int); applying chr to that code returns the original character, so chr(ord(x)) == x is True. Conversely, if x is an int code point, chr(x) yields the character and ord on that character restores the original int, making ord(chr(x)) == x True.

Why Incorrect

B. ord()’s argument must be str of length 1; passing int from first ord raises TypeError.
C. chr() needs int; inner chr returns str, so outer chr raises TypeError.

References

1. Python 3.11 Library Reference, “Built-in Functions”, §ord, §chr,

https://docs.python.org/3/library/functions.html#ord

2. University of Michigan PY4E, “Unicode in Python”, Week 6 notes, explanatory example.

Scroll to Top