Find the output of the following Python code: L1=[1,4,3,2] L1.sort() L2=L1…
2023
Find the output of the following Python code: L1=[1,4,3,2] L1.sort() L2=L1 print (L2)
- A.
[1, 4, 3, 2]
- B.
[1, 2, 3, 4]
- C.
1432
- D.
1234
Attempted by 1855 students.
Show answer & explanation
Correct answer: B
Key insight: assigning L2 = L1 makes both names refer to the same list object, and list.sort() sorts the list in place.
Create L1 = [1, 4, 3, 2].
Assign L2 = L1 so L2 refers to the same list object as L1.
Call L1.sort(), which sorts the list in place to [1, 2, 3, 4].
print(L2) prints the current contents of that same list: [1, 2, 3, 4].
Final output: [1, 2, 3, 4]