CS FundamentalsVery hardProduct-company placement · Python interview

What does this Prototype-style code print?

What does this Prototype-style code print?
import copy
class Doc:
    def __init__(self, title, tags):
        self.title = title
        self.tags = tags
a = Doc("v1", ["draft"])
b = copy.copy(a)
c = copy.deepcopy(a)
b.title = "v2"
b.tags.append("review")
c.tags.append("final")
print(a.title, a.tags, len(c.tags))
  1. A. v1 ['draft'] 2
  2. B. v2 ['draft', 'review'] 2
  3. C. v1 ['draft', 'review'] 2
  4. D. v1 ['draft', 'review', 'final'] 3

Correct answer: C. v1 ['draft', 'review'] 2

A shallow copy shares the same list, so b.tags.append changes a.tags; rebinding b.title does not touch a. The deep copy has its own list ['draft', 'final'], so its length is 2 and a is unaffected by it.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic