JavaScript Shallow vs Deep Copying Objects Quiz
Answer 30 questions comparing shallow copy techniques, deep copy strategies, spread/Object.assign behavior, JSON and structuredClone approaches, reference vs value semantics, and common pitfalls like cyclic structures.
Question 1
What defines a shallow copy of an object?
Question 2
Why are shallow copies often sufficient for configuration objects?
Question 3
What prints?
const original = { stats: { score: 10 } }
const clone = { ...original }
clone.stats.score = 99
console.log(original.stats.score)Question 4
Which shallow copy technique also copies property descriptors like getters/setters?
Question 5
What is the goal of a deep copy?
Question 6
Why are recursive algorithms common in deep cloning utilities?
Question 7
What prints?
import structuredClone from '@ungap/structured-clone'
const original = { nested: { value: 1 } }
const clone = structuredClone(original)
clone.nested.value = 2
console.log(original.nested.value)Question 8
Why can deep copies be expensive?
Question 9
Which data types are copied by value when using spread on an object?
Question 10
What prints?
const source = { list: [1, 2] }
const copy = { ...source, list: [...source.list] }
copy.list.push(3)
console.log(source.list)Question 11
Why might spreading arrays of objects still lead to shared references?
Question 12
How does Object.assign handle getters on the source object?
Question 13
What prints?
const target = {}
const source = { get value() { return Math.random() } }
Object.assign(target, source)
console.log(typeof Object.getOwnPropertyDescriptor(target, 'value').get)Question 14
Why is Object.assign useful for merging default options?
Question 15
Why does JSON.parse(JSON.stringify(obj)) drop functions and undefined values?
Question 16
What prints?
const obj = { date: new Date('2024-01-01') }
const cloned = JSON.parse(JSON.stringify(obj))
console.log(typeof cloned.date)
console.log(cloned.date instanceof Date)Question 17
Why is JSON cloning unsuitable for cyclic references?
Question 18
Which built-in browser/Node API deep clones structured data including Maps and Sets?
Question 19
What prints?
const map = new Map([['a', { count: 1 }]])
const cloned = structuredClone(map)
cloned.get('a').count = 5
console.log(map.get('a').count)Question 20
Why might structuredClone fail for objects containing functions?
Question 21
Why do primitive assignments not require explicit copying?
Question 22
What prints?
const original = { flag: true }
const alias = original
alias.flag = false
console.log(original.flag)
console.log(original === alias)Question 23
Why should large shared objects be treated immutably even after shallow copying?
Question 24
What is a common pitfall when copying DOM nodes via shallow clone?
Question 25
What prints?
const obj = { }
obj.self = obj
console.log(typeof obj.self.self === 'object')Question 26
Why must deep copy algorithms track visited objects?
Question 27
What prints?
function deepClone(value, seen = new WeakMap()) {
if (typeof value !== 'object' || value === null) return value
if (seen.has(value)) return seen.get(value)
const result = Array.isArray(value) ? [] : {}
seen.set(value, result)
for (const key in value) {
result[key] = deepClone(value[key], seen)
}
return result
}
const obj = { name: 'A' }
obj.self = obj
const clone = deepClone(obj)
console.log(clone.self === clone)
console.log(clone === obj)Question 28
Why does copying large graphs with JSON pose performance issues beyond type loss?
Question 29
What is a safer alternative to JSON cloning when Dates, Maps, or Sets must be preserved?
Question 30
Why should teams document their copying strategy?
