Certified Associate in Python Programming (PCAP-31-03) — Question 62
What is the expected output of the following code?
mytu = ('a', 'b', 'c')
m = tuple(map(lambda x: chr(ord(x) + 1), mytu))
print(m[-2])
Answer options
- A. an exception is raised
- B. a
- C. b
- D. c
Correct answer: D
Explanation
The code takes a tuple of characters, increments their ASCII values by 1, and creates a new tuple. When accessing the second to last element with m[-2], it retrieves 'c', which has been incremented to 'd', making 'd' the last element. The correct answer is D, while A is incorrect because no exception occurs, and B and C are also incorrect as they do not represent the output.