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)
  1. A. {'k': [1, 2]} {'k': [1, 2]}
  2. B. {'k': [1, 2, 3], 'j': 0} {'k': [1, 2]}
  3. C. {'k': [1, 2, 3]} {'k': [1, 2, 3]}
  4. 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

Exams that ask this topic