JavaScript Spread & Rest Operator Deep Usage Quiz

JavaScript
0 Passed
0% acceptance

Answer 35 questions exploring spread on arrays and objects, cloning behavior, combining collections, rest parameters, destructuring techniques, pitfalls, performance trade-offs, and modern practical patterns.

35 Questions
~70 minutes
1

Question 1

What does the spread operator do when used inside an array literal?

A
Expands the elements of the iterable into the surrounding array
B
Converts numbers into strings automatically
C
Sorts the array immediately
D
Mutates the original iterable
2

Question 2

Which scenario benefits from spreading an array into a new one?

A
Creating a shallow copy before pushing new items
B
Reversing the array in place
C
Running a for-loop faster
D
Replacing all elements with zeros
3

Question 3

What prints?

javascript
const base = [1, 2]
const extended = [...base, 3]
console.log(base)
console.log(extended)
A
[1, 2] and [1, 2, 3]
B
[1, 2, 3] and [1, 2, 3]
C
[1, 2, 3] and [1, 2]
D
Throws TypeError
4

Question 4

Why might spreading a NodeList into an array be useful?

A
It allows you to use array methods like map or filter on the DOM collection
B
It automatically converts nodes into JSON
C
It mutates the DOM tree
D
It improves CSS specificity
5

Question 5

What prints?

javascript
const user = { name: 'Ava', age: 29 }
const copy = { ...user, role: 'admin' }
console.log(copy.role)
console.log(user.role)
A
'admin' and undefined
B
'admin' and 'admin'
C
undefined and undefined
D
Throws TypeError
6

Question 6

Which property wins when spreading multiple objects with the same key?

A
Properties from the rightmost object override earlier ones
B
The leftmost property always wins
C
Keys merge into arrays
D
JavaScript throws an error
7

Question 7

Why is spreading objects preferred over Object.assign for shallow copies?

A
It is more concise and avoids mutating the target object
B
It performs deep cloning automatically
C
It preserves symbol properties by default
D
It freezes the object
8

Question 8

What prints?

javascript
const base = { a: 1, b: 2 }
const extra = { b: 5, c: 6 }
const result = { ...base, ...extra }
console.log(result)
A
{ a: 1, b: 5, c: 6 }
B
{ a: 1, b: 2, c: 6 }
C
{ a: 1, b: 2 }
D
{ b: 5, c: 6 }
9

Question 9

Why is spread considered a shallow copy?

A
Nested objects and arrays still reference the same underlying data
B
It copies every nested level recursively
C
It serializes to JSON internally
D
It blocks prototypes from copying
10

Question 10

Which approach creates an independent nested object clone?

A
Manually copy nested objects or use structuredClone
B
Spread by itself
C
Object.freeze before spreading
D
Using delete on the original object
11

Question 11

What prints?

javascript
const original = { stats: { score: 90 } }
const clone = { ...original }
clone.stats.score = 50
console.log(original.stats.score)
A
50
B
90
C
undefined
D
Throws TypeError
12

Question 12

Why can spreading arrays help avoid mutation bugs?

A
By cloning before mutating, the original array remains unchanged
B
Because spread locks the array
C
Because spread enforces pure functions automatically
D
Because spread prevents new properties from being added
13

Question 13

What advantage does spreading multiple arrays into one provide?

A
Concise syntax for concatenation without calling concat
B
Automatic deduplication
C
Sorting by default
D
Deep cloning nested arrays
14

Question 14

What prints?

javascript
const left = ['a', 'b']
const right = ['c']
const combined = [...left, 'mid', ...right]
console.log(combined)
A
['a', 'b', 'mid', 'c']
B
['a', 'b']
C
['mid']
D
['c', 'mid', 'a', 'b']
15

Question 15

How can you combine arrays while adding unique IDs?

A
Spread the arrays and map the result to append IDs
B
Spread automatically adds IDs
C
Use spread followed by delete
D
Spread into an object literal
16

Question 16

Why is spread helpful when merging configuration objects?

A
It lets you copy defaults and override selected keys in one readable statement
B
It merges nested deep structures by default
C
It prevents invalid keys entirely
D
It ensures type safety at compile time
17

Question 17

What does the rest parameter syntax do in a function signature?

A
Collects remaining arguments into an array
B
Spreads arguments into separate parameters
C
Converts the function into an async function
D
Disables the arguments object
18

Question 18

What prints?

javascript
function sum(label, ...values) {
  return `${label}:${values.reduce((a, b) => a + b, 0)}`
}
console.log(sum('total', 2, 3, 4))
A
'total:9'
B
'total:0'
C
'2:3'
D
Throws TypeError
19

Question 19

Why is rest preferred over the arguments object?

A
Rest produces a real array and works seamlessly with arrow functions
B
Rest clones all objects deeply
C
Rest enforces strict mode automatically
D
Rest hides extra parameters
20

Question 20

How many rest parameters may a function have?

A
Only one, and it must be the final parameter
B
Multiple rest parameters are allowed anywhere
C
One per parameter group
D
None in strict mode
21

Question 21

What prints?

javascript
const collect = (...args) => args.join(',')
console.log(collect('a', 'b', 'c'))
A
'a,b,c'
B
'abc'
C
[]
D
Throws TypeError
22

Question 22

Why is rest useful in wrapper functions?

A
It lets you forward arbitrary arguments to another function with spread
B
It automatically logs arguments
C
It enforces type checking
D
It prevents errors at runtime
23

Question 23

How can rest help with default argument patterns?

A
Capture extra configuration values while keeping named parameters explicit
B
Automatically populate missing parameters
C
Change the function arity to zero
D
Force synchronous execution
24

Question 24

What prints?

javascript
const [first, ...others] = [10, 20, 30]
console.log(first)
console.log(others)
A
10 and [20, 30]
B
10 and 20
C
[10,20,30] and []
D
Throws TypeError
25

Question 25

Why can object rest in destructuring be handy?

A
It extracts specific properties while collecting the rest into a new object
B
It deep clones nested objects
C
It enforces optional chaining
D
It sorts object keys
26

Question 26

What restriction applies to rest in array destructuring?

A
The rest element must appear in the last position
B
Rest can appear multiple times
C
Rest cannot be used with arrays
D
Rest must be renamed
27

Question 27

What prints?

javascript
const user = { id: 1, name: 'Lee', role: 'dev' }
const { role, ...identity } = user
console.log(role)
console.log(identity)
A
'dev' and { id: 1, name: 'Lee' }
B
'dev' and { role: 'dev' }
C
undefined and {}
D
Throws TypeError
28

Question 28

Why might spreading undefined in an array literal throw an error?

javascript
const arr = [...undefined]
A
undefined is not iterable, so spreading it causes a TypeError
B
Spread automatically converts undefined to []
C
It creates a sparse array
D
It silently ignores undefined
29

Question 29

What is a common mistake when spreading objects with methods?

A
Expecting prototype methods to copy over, even though spread only copies own enumerable properties
B
Believing spread deletes methods
C
Assuming spread changes the this binding
D
Thinking spread sorts method order
30

Question 30

What prints?

javascript
const config = { get value() { console.log('computed'); return 5 } }
const copy = { ...config }
console.log(copy.value)
A
Logs "computed" once, then outputs 5
B
Never logs "computed"
C
Logs "computed" twice
D
Throws TypeError
31

Question 31

Why can spreading large arrays hurt performance?

A
All elements must be iterated and copied, which may allocate large contiguous memory
B
Spread forces synchronous Ajax calls
C
Spread disables garbage collection
D
Spread runs in constant time
32

Question 32

When merging arrays frequently, what alternative might be faster?

A
Using push with spread (target.push(...source)) to avoid creating intermediate arrays
B
Using JSON.stringify
C
Using eval
D
Using for...in loops on arrays
33

Question 33

What prints?

javascript
function combine(...arrays) {
  return [].concat(...arrays)
}
console.log(combine([1], [2, 3]))
A
[1, 2, 3]
B
[[1], [2,3]]
C
[1]
D
[2, 3]
34

Question 34

What performance cost arises when spreading objects with many properties?

A
Enumerating every key/value and allocating a new object for each spread
B
Disabling the garbage collector
C
Blocking the event loop indefinitely
D
Triggering HTTP requests
35

Question 35

Why are spread and rest central to modern JS patterns?

A
They provide flexible data composition, allow ergonomic function signatures, and improve readability when handling variable inputs
B
They replace the need for promises
C
They eliminate async bugs
D
They enforce immutability automatically

QUIZZES IN JavaScript