Question

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

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

Advertisement

Detailed Solution

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

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is the output of the following Python code involving a mutable default argument? / परिवर्तनीय डिफ़ॉल्ट तर्क वाले निम्नलिखित Python कोड का आउटपुट क्या होगा? def f(a, b=[]): b.append(a); return b print(f(1), f(2))

1.
[1] [2] / [1] [2]
2.
[1] [1, 2] / [1] [1, 2]
3.
[1, 2] [1, 2] / [1, 2] [1, 2]
4.
TypeError / TypeError

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

The default list is created once at function definition time and shared across calls, so the second call appends to the same list. / डिफ़ॉल्ट सूची फ़ंक्शन परिभाषा के समय एक बार बनती है और सभी कॉलों में साझा रहती है, इसलिए दूसरी कॉल उसी सूची में जोड़ती है।

Downloaded from PrepSnap (prepsnap.in)