ProgrammingVery hardProduct company OA

What is printed?

What is printed?
log = []
def t(x): log.append(x); return x
def f(a, b=t("d1"), *args, c=t("d2"), **kw):
    return a, b, args, c, sorted(kw)
log.append("call")
r = f(t(1), *[t(2), t(3)], z=t(4), **{"c": t(5)})
print(log, r)
  1. A. ['call', 1, 2, 3, 4, 5] (1, 2, (3,), 5, ['z'])
  2. B. ['d1', 'd2', 'call', 1, 2, 3, 4, 5] (1, 2, (3,), 5, ['z'])
  3. C. ['d1', 'd2', 'call', 1, 2, 3, 4, 5] (1, 'd1', (2, 3), 5, ['z'])
  4. D. ['call', 1, 'd1', 2, 3, 'd2', 4, 5] (1, 2, (3,), 'd2', ['c', 'z'])

Correct answer: B. ['d1', 'd2', 'call', 1, 2, 3, 4, 5] (1, 2, (3,), 5, ['z'])

Default values are evaluated once, when the def statement executes, so 'd1' and 'd2' are logged before 'call' and never again. At the call, arguments are evaluated left to right (1, 2, 3, 4, 5). Positional values fill a and then b, so b = 2 overrides its default and only 3 overflows into args. c is keyword-only; it is supplied through the ** mapping, so c = 5 and only z lands in kw.

More Python Programming questions

Exams that ask this topic