1. Home
  2. Data Science
  3. Python
  4. def add(item, box=[]): box.append(item); return box. What…

Added 2 Nov 2026

def add(item, box=[]): box.append(item); return box. What does calling add(1) and then add(2) return the second time?

  1. An error
  2. [1, 2] Correct answer
  3. [2]
  4. [1]

Correct answer

B. [1, 2]

16 views

MCQSEXAM

Explanation

Default arguments are evaluated once, when the function is defined, so the same list is reused across calls. The safe pattern is box=None, then create a new list inside the function.

Reference

Asked in: NTS, Python Developer Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs