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)- A. ['call', 1, 2, 3, 4, 5] (1, 2, (3,), 5, ['z'])
- B. ['d1', 'd2', 'call', 1, 2, 3, 4, 5] (1, 2, (3,), 5, ['z'])
- C. ['d1', 'd2', 'call', 1, 2, 3, 4, 5] (1, 'd1', (2, 3), 5, ['z'])
- 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
- What is printed? python d = dict.fromkeys("ab", []) d["a"].append(1) d["b"] = d["b"] + [2] d["a"] += [3] g = [[]] 2 + [[] for in range(2)]…
- A log pipeline must remove duplicate event ids from a list xs of n = 2·10⁵ integers while keeping the order of first occurrence. Four…
- What is printed? python class Base: registry = [] def initsubclass(cls, kw): cls.registry.append(cls.name) cls.tag = kw.get("tag"…
- What is printed? python class CM: def init(self, name, swallow): self.name, self.swallow = name, swallow def enter(self): print("in" +…
- What is printed? python import bisect a = [1, 3, 3, 3, 7] i, j = bisect.bisectleft(a, 3), bisect.bisectright(a, 3) bisect.insortleft(a…
- What is printed? python from itertools import groupby s = "aabbbaac" g1 = [(k, len(list(v))) for k, v in groupby(s)] g2 = {k: len(list(v))…