ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class Cart:
    def __init__(self, items=[]):
        self.items = items
a = Cart(); b = Cart(); c = Cart([])
a.items.append("pen")
c.items.append("ink")
print(b.items, len(Cart.__init__.__defaults__[0]))
  1. A. [] 0
  2. B. ['pen', 'ink'] 2
  3. C. [] 1
  4. D. ['pen'] 1

Correct answer: D. ['pen'] 1

a and b both store the single default list, so b.items shows 'pen' and the stored default now has length 1. c was given its own list, so 'ink' does not leak.

More Python Programming questions

Exams that ask this topic