JavaScript Shallow vs Deep Copying Objects Quiz

JavaScript
0 Passed
0% acceptance

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.

30 Questions
~60 minutes
1

Question 1

What defines a shallow copy of an object?

A
Top-level properties are copied while nested references still point to the same objects
B
All nested structures are fully duplicated recursively
C
Only primitive values are preserved
D
References are broken at every depth regardless of type
2

Question 2

Why are shallow copies often sufficient for configuration objects?

A
Many configs are flat or treat nested references immutably, so duplicating only top-level keys is enough
B
They automatically deep copy arrays
C
They freeze the object
D
They enforce strict typing
3

Question 3

What prints?

javascript
const original = { stats: { score: 10 } }
const clone = { ...original }
clone.stats.score = 99
console.log(original.stats.score)
A
99
B
10
C
undefined
D
Throws TypeError
4

Question 4

Which shallow copy technique also copies property descriptors like getters/setters?

A
Object.defineProperties with Object.getOwnPropertyDescriptors
B
Spread syntax
C
Object.assign
D
JSON.parse(JSON.stringify())
5

Question 5

What is the goal of a deep copy?

A
Create a new object tree where nested objects and arrays are duplicated rather than referenced
B
Freeze the original object
C
Convert all values to JSON strings
D
Strip functions and symbols
6

Question 6

Why are recursive algorithms common in deep cloning utilities?

A
Nested structures require visiting each node to clone references appropriately
B
Recursion automatically optimizes memory
C
Iterative approaches are impossible
D
Recursion guarantees faster execution
7

Question 7

What prints?

javascript
import structuredClone from '@ungap/structured-clone'
const original = { nested: { value: 1 } }
const clone = structuredClone(original)
clone.nested.value = 2
console.log(original.nested.value)
A
1
B
2
C
undefined
D
Throws TypeError
8

Question 8

Why can deep copies be expensive?

A
They must visit every nested node, increasing CPU time and allocations, especially for large graphs
B
They require network calls
C
They always block the event loop indefinitely
D
They convert objects to DOM nodes
9

Question 9

Which data types are copied by value when using spread on an object?

A
Primitives like numbers, booleans, strings, symbols, and bigints
B
Functions and DOM nodes
C
Maps and Sets automatically
D
Date objects with deep clone
10

Question 10

What prints?

javascript
const source = { list: [1, 2] }
const copy = { ...source, list: [...source.list] }
copy.list.push(3)
console.log(source.list)
A
[1, 2]
B
[1, 2, 3]
C
[]
D
Throws TypeError
11

Question 11

Why might spreading arrays of objects still lead to shared references?

A
Spread copies the array structure but not the objects inside it
B
Spread mutates the original array
C
Spread converts objects to strings
D
Spread requires deep copy flag
12

Question 12

How does Object.assign handle getters on the source object?

A
It invokes the getter and copies the returned value
B
It copies the getter definition
C
It throws a TypeError
D
It bypasses the property entirely
13

Question 13

What prints?

javascript
const target = {}
const source = { get value() { return Math.random() } }
Object.assign(target, source)
console.log(typeof Object.getOwnPropertyDescriptor(target, 'value').get)
A
undefined
B
function
C
number
D
boolean
14

Question 14

Why is Object.assign useful for merging default options?

A
It copies enumerable own properties from sources into target in order, letting later objects override earlier defaults
B
It deep clones arrays
C
It freezes the target automatically
D
It enforces type safety
15

Question 15

Why does JSON.parse(JSON.stringify(obj)) drop functions and undefined values?

A
JSON serialization ignores unsupported data types, so those properties are omitted
B
It converts functions to empty strings
C
It throws errors for any functions
D
It deep clones everything
16

Question 16

What prints?

javascript
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)
A
string and false
B
object and true
C
number and false
D
object and false
17

Question 17

Why is JSON cloning unsuitable for cyclic references?

A
JSON.stringify throws a TypeError when encountering circular structures
B
JSON automatically removes cycles without warning
C
Cycles cause values to be duplicated infinitely
D
Cycles produce null entries
18

Question 18

Which built-in browser/Node API deep clones structured data including Maps and Sets?

A
structuredClone
B
Object.freeze
C
JSON.stringify
D
Reflect.clone
19

Question 19

What prints?

javascript
const map = new Map([['a', { count: 1 }]])
const cloned = structuredClone(map)
cloned.get('a').count = 5
console.log(map.get('a').count)
A
1
B
5
C
undefined
D
Throws TypeError
20

Question 20

Why might structuredClone fail for objects containing functions?

A
Functions are not cloneable by structuredClone per spec, leading to DataCloneError
B
Functions are converted to strings
C
Functions are executed during clone
D
Functions force shallow copy
21

Question 21

Why do primitive assignments not require explicit copying?

A
Primitives are assigned by value, so each variable gets its own copy automatically
B
Primitives are stored on the DOM
C
Primitives are immutable objects
D
Primitives cannot be reassigned
22

Question 22

What prints?

javascript
const original = { flag: true }
const alias = original
alias.flag = false
console.log(original.flag)
console.log(original === alias)
A
false and true
B
true and false
C
false and false
D
true and true
23

Question 23

Why should large shared objects be treated immutably even after shallow copying?

A
Mutations could inadvertently impact other consumers referencing the same nested data
B
Immutability speeds up JSON
C
Shared objects throw errors when mutated
D
Garbage collection requires immutability
24

Question 24

What is a common pitfall when copying DOM nodes via shallow clone?

A
Event listeners bound to nested elements are not copied automatically
B
Cloning rewires CSS selectors
C
Shallow clones convert nodes to JSON
D
Clones lose attribute values by default
25

Question 25

What prints?

javascript
const obj = { }
obj.self = obj
console.log(typeof obj.self.self === 'object')
A
true
B
false
C
undefined
D
Throws TypeError
26

Question 26

Why must deep copy algorithms track visited objects?

A
To handle cyclic references without infinite recursion and to reuse clones of repeated references
B
To speed up Math.random
C
To delete original data
D
To convert objects to Maps
27

Question 27

What prints?

javascript
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)
A
true and false
B
false and true
C
true and true
D
false and false
28

Question 28

Why does copying large graphs with JSON pose performance issues beyond type loss?

A
Serialization allocates intermediate strings proportional to data size, increasing memory churn and CPU usage
B
JSON cloning triggers garbage collection off the main thread
C
JSON cloning automatically fetches network data
D
JSON cloning compiles regexes
29

Question 29

What is a safer alternative to JSON cloning when Dates, Maps, or Sets must be preserved?

A
structuredClone or libraries like lodash.cloneDeep that understand those types
B
Object.assign
C
Array.prototype.slice
D
Promise.resolve
30

Question 30

Why should teams document their copying strategy?

A
Clarity on shallow vs deep expectations prevents accidental shared-state bugs and ensures engineers know which utility to use
B
Documentation accelerates GC
C
Documentation forces deep copies always
D
Documentation removes the need for tests

QUIZZES IN JavaScript