ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def add(x, memo={}):
memo[x] = len(memo)
return memo
print(add(5), add(7))- A. {5: 0} {7: 0}
- B. {5: 0} {5: 0, 7: 1}
- C. {5: 0, 7: 1} {7: 1}
- D. {5: 0, 7: 1} {5: 0, 7: 1}
Correct answer: D. {5: 0, 7: 1} {5: 0, 7: 1}
The default dict is created once and shared by all calls. Both calls return that same object, and print runs after both calls, so both values show the final contents.
More Python Programming questions
- What is printed? python print(min('10', '9'), min(10, 9), max('10', '9', key=int))
- What is printed? python i = 'x' = [i for i in range(3)] print(i)
- What is printed? python print([y for x in range(5) if (y := x x) > 5])
- What is printed? python print(bin(10).count('1'), (10).bitcount(), (-10).bitcount())
- What is printed? python from collections import Counter print(Counter('abracadabra').mostcommon(3))
- What is printed? python print(float(10 16 + 1) == 10 16, 10 16 + 1 == float(10 16 + 1))