ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def f(xs):
    xs.sort()
    return xs
data = [3, 1, 2]
res = f(data)
print(data is res, data)
  1. A. True [3, 1, 2]
  2. B. False [3, 1, 2]
  3. C. True [1, 2, 3]
  4. D. False [1, 2, 3]

Correct answer: C. True [1, 2, 3]

sort() works in place and f returns the same list object. The caller's list is changed, so data is res holds and data shows the sorted order.

More Python Programming questions

Exams that ask this topic