Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 2 : [1, 2, 3, 4] / [1, 2, 3, 4]

Advertisement

Detailed Solution

सही उत्तर [1, 2, 3, 4] / [1, 2, 3, 4] है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following Python code? `x = [1, 2, 3]` / `y = x` / `y += [4]` / `print(x)` / निम्नलिखित Python कोड का आउटपुट क्या होगा? `x = [1, 2, 3]` / `y = x` / `y += [4]` / `print(x)`

1.
[1, 2, 3] / [1, 2, 3]
2.
[1, 2, 3, 4] / [1, 2, 3, 4]
3.
[4, 1, 2, 3] / [4, 1, 2, 3]
4.
Error — cannot modify through alias / त्रुटि — alias से संशोधन नहीं हो सकता

Correct Answer: [1, 2, 3, 4] / [1, 2, 3, 4]

`y = x` makes both names refer to the same list object, and `+=` on a list mutates it in place, so `x` also shows `[1, 2, 3, 4]`. The trap is assuming `y += [4]` rebinds `y` to a new list. / `y = x` से दोनों नाम एक ही list ऑब्जेक्ट को संदर्भित करते हैं, और list पर `+=` उसे in-place बदलता है, अतः `x` भी `[1, 2, 3, 4]` दिखाता है। जाल यह मानना है कि `y += [4]` से `y` नई list से bind हो जाता है।

Downloaded from PrepSnap (prepsnap.in)