If PL is a Python list as follows: PL = ['Basic', 'C', 'C++'] Then what will…
2023
If PL is a Python list as follows: PL = ['Basic', 'C', 'C++'] Then what will be the status of the list PL after PL.sort(reverse = True) ?
- A.
['Basic', 'C', 'C++']
- B.
['C++', 'C', 'Basic']
- C.
['C', 'C++', 'Basic']
- D.
['Basic', 'C++', 'C']
Attempted by 1898 students.
Show answer & explanation
Correct answer: B
Answer: ['C++', 'C', 'Basic']
Key idea: PL.sort(reverse=True) sorts the list in descending lexicographic order (highest to lowest) and modifies the list in place.
Compare 'C++' vs 'C': 'C' is a prefix of 'C++'. In lexicographic comparison the longer string is considered greater when one is a prefix of the other, so 'C++' > 'C'. Therefore 'C++' comes before 'C' in descending order.
Compare 'C' vs 'Basic': 'C' (starts with 'C') is greater than 'Basic' (starts with 'B'), so 'C' comes before 'Basic' in descending order.
Putting these comparisons together gives the final list: ['C++', 'C', 'Basic'].