ProgrammingModerateGATE CS · Product-company placement

What is printed?

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

Correct answer: B. [2, 4, 7] [7, 4, 2] True False

b is the same list as a, so sorting through b sorts a in place. sorted() always returns a new list.

More Python Programming questions

Exams that ask this topic