Question

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

Option 3 : [1, 1]

Advertisement

Detailed Solution

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

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

What is printed by the second print statement in the following Python code? def f(x=[]): x.append(1); return x; print(f()); print(f()); / निम्नलिखित Python कोड में दूसरे print कथन द्वारा क्या प्रिंट होता है? def f(x=[]): x.append(1); return x; print(f()); print(f());

1.
[1]
2.
[]
3.
[1, 1]
4.
TypeError / टाइपत्रुटि

Correct Answer: [1, 1]

The default argument list is created once when the function is defined, not on each call, so both calls append to the same list object: the first print shows [1] and the second shows [1, 1]. / डिफ़ॉल्ट तर्क सूची फंक्शन परिभाषित होते समय एक बार बनती है, प्रत्येक कॉल पर नहीं, अतः दोनों कॉल एक ही सूची ऑब्जेक्ट में जोड़ते हैं: पहला print [1] और दूसरा [1, 1] दिखाता है।

Downloaded from PrepSnap (prepsnap.in)