ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sorted('bca'), ''.join(sorted('bca', reverse=True)), sorted([3, 1, 2], reverse=True)[0], list(reversed('ab')))
  1. A. ['a', 'b', 'c'] abc 3 ['a', 'b']
  2. B. ['a', 'b', 'c'] cba 3 ['b', 'a']
  3. C. ['c', 'b', 'a'] cba 1 ['b', 'a']
  4. D. ['a', 'b', 'c'] cba 2 ['a', 'b']

Correct answer: B. ['a', 'b', 'c'] cba 3 ['b', 'a']

sorted on a string gives a list of single characters in order. Joining the reverse-sorted characters gives cba, and the largest of 3, 1, 2 is 3. reversed returns an iterator, and list() shows its elements in reverse order.

More Python Programming questions

Exams that ask this topic