Question

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

Option 3 : [1] [1, 2]

Advertisement

Detailed Solution

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

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

Question:

Consider the Python fragment: def f(a, b=[]): b.append(a); return b; print(f(1)); print(f(2)); What is printed? / Python कोड खंड पर विचार करें: def f(a, b=[]): b.append(a); return b; print(f(1)); print(f(2)); क्या मुद्रित होगा?

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

Correct Answer: [1] [1, 2]

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

Downloaded from PrepSnap (prepsnap.in)