PCEP-30-02 · Question #270
What is the expected output of the following code? ``python print(ord('c')?ord('a')) ``
The correct answer is B. 2. ord('c') returns 99 and ord('a') returns 97. The - operator subtracts them: 99 - 97 = 2, making B the correct answer. Why the distractors are wrong: A (1): Off-by-one error - this would be the result of ord('b') - ord('a'), since 'b' is one step from 'a'. C (3): Confuses the…
Question
print(ord('c')?ord('a'))
Options
- A1
- B2
- C3
- D0
How the community answered
(25 responses)- A4% (1)
- B84% (21)
- C4% (1)
- D8% (2)
Explanation
ord('c') returns 99 and ord('a') returns 97. The - operator subtracts them: 99 - 97 = 2, making B the correct answer.
Why the distractors are wrong:
- A (1): Off-by-one error - this would be the result of
ord('b') - ord('a'), since 'b' is one step from 'a'. - C (3): Confuses the alphabetical position of 'c' (the 3rd letter) with the difference in ASCII values between 'c' and 'a'.
- D (0): Would only result if both arguments to
ord()were the same character, yielding a difference of zero.
Memory tip: Lowercase ASCII values start at 97 for 'a' and increase by 1 per letter ('b'=98, 'c'=99, etc.). So ord(x) - ord('a') always gives you the 0-based alphabetical index of x - a handy pattern for letter-difference problems on exams.
Community Discussion
No community discussion yet for this question.