Question
1.
2.
3.
4.
Answer (Detailed Solution Below)
Option 3 : [1] and [1, 2]
Advertisement
Detailed Solution
सही उत्तर [1] and [1, 2] है।
Key Points
Helpful?
Question:
What are the two lines printed by the following Python code? def f(a, b=[]): b.append(a); return b; print(f(1)); print(f(2)) / निम्नलिखित Python कोड द्वारा कौन-सी दो पंक्तियाँ प्रिंट होंगी?
1.
[1] and [2]
2.
[1] and [1]
3.
[1] and [1, 2]
4.
Error on the second call
Correct Answer: [1] and [1, 2]
The default list is created once and shared across calls. The first call appends 1, printing [1]; the second call appends 2 to the same list, printing [1, 2]. / डिफ़ॉल्ट लिस्ट एक बार बनती है और सभी कॉलों में साझा होती है। पहली कॉल में 1 जुड़ता है, [1] प्रिंट होता है; दूसरी कॉल में उसी लिस्ट में 2 जुड़ता है, [1, 2] प्रिंट होता है।
Downloaded from PrepSnap (prepsnap.in)