ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
print(min([3, -4, 2], key=lambda v: v * v), max({"a": 3, "b": 1}), max("abc", "b", key=len))- A. -4 a abc
- B. 2 a abc
- C. 2 b abc
- D. 2 b b
Correct answer: C. 2 b abc
min with key compares squares 9, 16, 4 and returns the original element with the smallest key: 2. max over a dict iterates its keys, not values, so it compares 'a' and 'b' and returns 'b'. With key=len, 'abc' (length 3) beats 'b'.
More Python Programming questions
- What is printed? python class Box: def len(self): return 0 b = Box() print(bool(b), "yes" if b else "no", len([b, b]))
- What is printed? python a = [3, 1, 4, 1, 5] a.insert(-1, 9) print(a, a.index(1), a.pop(), a.count(1))
- What is printed? python def g(): print("start", end=" ") yield 1 print("mid", end=" ") yield 2 it = g() print("made", end=" ")…
- What is printed? python x = 1 def f(flag): if flag: x = 2 return x print(f(True), end=" ") try: print(f(False)) except UnboundLocalError…
- What is printed? python class A: pass class B(A): pass try: class C(A, B): pass print("ok", end=" ") except TypeError: print("TypeError"…
- What is printed? python class R: def iter(self): print("it", end=" ") return iter([1, 2, 3]) r = R() print(2 in r, 5 in r)