ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
def f(x, acc={}):
    acc[x] = len(acc)
    return acc
a = f("p")
b = f("q", {})
c = f("r")
print(a is c, a, b)
  1. A. True {'p': 0, 'r': 1} {'q': 0}
  2. B. False {'p': 0} {'q': 0}
  3. C. True {'p': 0, 'q': 1, 'r': 2} {'q': 1}
  4. D. True {'p': 0, 'r': 2} {'q': 0}

Correct answer: A. True {'p': 0, 'r': 1} {'q': 0}

The default dict is created once when def runs. Calls 1 and 3 share it (so a is c, holding p:0 then r:1), while call 2 used its own fresh dict.

More Python Programming questions

Exams that ask this topic