Bird
Raised Fist0

Consider the following code snippet using deep copy on an object with an empty scores list. What will be the output after appending 5 to the copy's scores?

medium🧾 Code Trace Q4 of Q15
OOP & Design Patterns - Prototype Pattern - Deep Copy vs Shallow Copy
Consider the following code snippet using deep copy on an object with an empty scores list. What will be the output after appending 5 to the copy's scores?
AOriginal scores: [5] Copy scores: [5]
BOriginal scores: [] Copy scores: []
COriginal scores: [] Copy scores: [5]
DOriginal scores: [5] Copy scores: []
Step-by-Step Solution
Solution:
  1. Step 1: Trace deepcopy with empty list

    Deepcopy creates a new empty list for copy_obj.scores, separate from original.scores.
  2. Step 2: Append 5 to copy's scores

    Appending 5 affects only copy_obj.scores, so original.scores remains empty.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Deep copy isolates empty nested lists -> [OK]
Quick Trick: Empty nested lists are copied separately in deep copy [OK]
Common Mistakes:
MISTAKES
  • Assuming original list changes
  • Confusing shallow copy behavior
Trap Explanation:
PITFALL
  • Candidates expect original to change when copy is modified, missing deep copy isolation.
Interviewer Note:
CONTEXT
  • Tests edge case handling of deep copy with empty nested objects.
Master "Prototype Pattern - Deep Copy vs Shallow Copy" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes