ProgrammingHardCognizant GenC · Python
What is printed?
What is printed?
import copy
a = {'k': [1, 2]}
b = a.copy()
c = copy.deepcopy(a)
a['k'].append(3)
a['j'] = 0
print(b, c)- A. {'k': [1, 2]} {'k': [1, 2]}
- B. {'k': [1, 2, 3], 'j': 0} {'k': [1, 2]}
- C. {'k': [1, 2, 3]} {'k': [1, 2, 3]}
- D. {'k': [1, 2, 3]} {'k': [1, 2]}
Correct answer: D. {'k': [1, 2, 3]} {'k': [1, 2]}
A shallow copy shares the inner list (it sees 3) but not later key additions; the deep copy is fully independent.
More Python Programming questions
- What is printed? python m = map(lambda x: x 2, [1, 2, 3]) print(list(m), list(m), sum(map(int, '123')))
- What is printed? python class P: def init(self, v): self.v = v def eq(self, o): return self.v == o.v try: s = {P(1), P(1)} print(len(s))…
- What is printed? python fs = [lambda x, i=i: x i for i in range(3)] gs = [lambda x: x i for i in range(3)] print([f(2) for f in fs], [g(2)…
- What is printed? python n = 1 xs = [1, 2, 3] g = (x for x in xs if x > n) xs = [10, 20] n = 2 print(list(g))
- What is printed? python print(f"{255:#x} {255:08b} {3.14159:.2f} {'ab':>4}|{12345.678:,.1f}")
- What is printed? python def fib(n, memo={}): if n < 2: return n if n not in memo: memo[n] = fib(n - 1) + fib(n - 2) return memo[n]…