ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
x = [3, 1, 2]
y = sorted(x)
x.sort(reverse=True)
print(x, y, x is y)
  1. A. [3, 2, 1] [1, 2, 3] True
  2. B. [3, 2, 1] [1, 2, 3] False
  3. C. [1, 2, 3] [1, 2, 3] False
  4. D. [3, 2, 1] [3, 2, 1] True

Correct answer: B. [3, 2, 1] [1, 2, 3] False

sort(reverse=True) reorders x in place. sorted made y a separate list before that, so it keeps the ascending order.

More Python Programming questions

Exams that ask this topic