ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE

What does the following Python 3 code print? def f(x, acc=[]): acc.append(x) return acc f(8) f(4) print(f(8))

  1. A. [8, 8]
  2. B. [8]
  3. C. [8, 8, 8, 8]
  4. D. [8, 4, 8]

Correct answer: D. [8, 4, 8]

The default list is created once when the function is defined, so appends accumulate across calls. Output: [8, 4, 8].

More Python Programming questions

Exams that ask this topic