Single Choice Moderate

QWhat will be the output after the following statements?

x = [5, 4, 3, 2, 1]
y = x.copy()
x[0] = 6
print(y)

ID: #3941 Python Built-in Functions MCQ 214 views
Question Info
#3941Q ID
ModerateDifficulty
Python Built-in Functions MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A [6, 4, 3, 2, 1]
  • B 6
  • C [5, 4, 3, 2, 1]
  • D 5
Correct Answer

Explanation

The copy() method creates a shallow copy of the original list. This means y becomes a new list with the same elements as x, but it is stored in a different memory location.

Initially:

x = [5, 4, 3, 2, 1]
y = x.copy()

Both lists contain the same values:

x = [5, 4, 3, 2, 1]
y = [5, 4, 3, 2, 1]

The next statement changes only the first element of x:

x[0] = 6

After this modification:

x = [6, 4, 3, 2, 1]
y = [5, 4, 3, 2, 1]

Since y is an independent copy, it remains unchanged. Therefore, print(y) displays:

[5, 4, 3, 2, 1]

Hence, the correct answer is Option C) [5, 4, 3, 2, 1].

Share This Question

Challenge a friend or share with your study group.