If PL is a Python list as follow: PL=['Basic', 'C', 'C++'] Then what will be…

2023

If PL is a Python list as follow: PL=['Basic', 'C', 'C++'] Then what will be the status of the list PL after PL.sort (reverse=True)?

  1. A.

    ['Basic', 'C', 'C++']

  2. B.

    ['C++', 'C', 'Basic']

  3. C.

    ['C', 'C++', 'Basic']

  4. D.

    ['Basic', 'C++', 'C']

Attempted by 1685 students.

Show answer & explanation

Correct answer: B

Key points: PL.sort(reverse=True) sorts the list in place in descending lexicographic order and the sort() method returns None.

Resulting list:

After calling PL.sort(reverse=True), the list PL becomes ['C++', 'C', 'Basic'].

  • In-place behavior: sort() modifies the original list and returns None, so do not write PL = PL.sort(...).

  • Why this order: Strings are compared lexicographically; ascending order is ['Basic', 'C', 'C++'] because 'B' < 'C' and 'C' < 'C++' (a shorter prefix sorts before a longer equal-prefixed string). Reversing that order gives ['C++', 'C', 'Basic'].

  • Practical note: If you need a new list instead of modifying the original, use sorted(PL, reverse=True) which returns the sorted list.

Explore the full course: Rssb Senior Computer Instructor