JavaScript Advanced Array Methods (map, filter, reduce) Quiz

JavaScript
0 Passed
0% acceptance

Answer 35 questions that dive into map transformations, filter logic, reduce accumulations, callback signatures, immutability, chaining, patterns, performance trade-offs, and behavior comparisons.

35 Questions
~70 minutes
1

Question 1

What does Array.prototype.map return?

A
A new array with each element transformed by the callback
B
The original array mutated in place
C
A single cumulative value
D
A boolean indicating success
2

Question 2

Which use case best fits map?

A
Transforming each item into a different shape
B
Removing elements based on a condition
C
Summing all numbers
D
Checking if any element matches
3

Question 3

What prints?

javascript
const nums = [1, 2, 3]
const doubled = nums.map(n => n * 2)
console.log(nums)
console.log(doubled)
A
[1, 2, 3] and [2, 4, 6]
B
[2, 4, 6] and [2, 4, 6]
C
[1, 2, 3] and [1, 2, 3]
D
Throws TypeError
4

Question 4

Why is map often preferred over forEach plus push?

A
It expresses intent declaratively and avoids manually managing destination arrays
B
It runs in O(1) time regardless of input size
C
It automatically deduplicates entries
D
It mutates the original array for better performance
5

Question 5

What prints?

javascript
const arr = [1, , 3]
const result = arr.map(x => x ?? 0)
console.log(result.length)
A
3
B
2
C
1
D
Throws ReferenceError
6

Question 6

What does Array.prototype.filter expect from its callback?

A
A truthy/falsey value to determine element inclusion
B
A numeric accumulator
C
An object to merge
D
A string label
7

Question 7

Why is filter considered non-destructive?

A
It returns a new array and does not alter the source array
B
It deletes matching items
C
It freezes the array
D
It clones nested objects by default
8

Question 8

What prints?

javascript
const words = ['apple', 'banana', 'pear']
const short = words.filter(w => w.length <= 5)
console.log(short)
A
['apple', 'pear']
B
['banana']
C
[]
D
undefined
9

Question 9

Which scenario favors filter?

A
Selecting active users from a list
B
Converting numbers into strings
C
Summing inventory counts
D
Checking existence of a property
10

Question 10

What prints?

javascript
const data = [0, 1, 2, 3]
const truthy = data.filter(Boolean)
console.log(truthy)
A
[1, 2, 3]
B
[0]
C
[]
D
[0, 1, 2, 3]
11

Question 11

What does Array.prototype.reduce return?

A
A single accumulated value
B
Always an array
C
A boolean
D
A generator
12

Question 12

What prints?

javascript
const nums = [1, 2, 3]
const sum = nums.reduce((acc, n) => acc + n, 0)
console.log(sum)
A
6
B
0
C
[1,2,3]
D
undefined
13

Question 13

Why is the initial accumulator value important?

A
Without it, reduce uses the first array element, which may break when the array is empty or the accumulator type differs from elements
B
It enables map to run faster
C
It ensures filter works
D
It copies the array
14

Question 14

What prints?

javascript
const letters = ['a', 'b', 'c']
const joined = letters.reduce((acc, cur) => acc + cur)
console.log(joined)
A
abc
B
['a', 'b', 'c']
C
undefined
D
Throws TypeError
15

Question 15

When should reduce be avoided?

A
When dedicated methods (map, filter, some, find) express intent more clearly
B
When you need to sum numbers
C
When the array length is two
D
When using async callbacks
16

Question 16

What is the parameter order for map/filter/reduce callbacks?

A
(currentValue, index, array)
B
(array, index, currentValue)
C
(index, currentValue, array)
D
(currentValue, array, index)
17

Question 17

How many arguments can reduce callbacks receive?

A
Four: accumulator, currentValue, index, array
B
Two: value and index only
C
One: accumulator only
D
Three: accumulator, array, index
18

Question 18

What prints?

javascript
const nums = [10, 20]
nums.map((value, index, array) => {
  if (array === nums && index === 1) {
    console.log(value)
  }
})
A
20
B
10
C
undefined
D
Throws TypeError
19

Question 19

Why is using the index parameter helpful?

A
It enables position-aware transformations, such as alternating styles or referencing neighbors
B
It accelerates map by using typed arrays
C
It prevents GC pauses
D
It allows filter to mutate the array safely
20

Question 20

What prints?

javascript
const arr = ['x', 'y', 'z']
const labeled = arr.map((value, index) => `${index}:${value}`)
console.log(labeled)
A
['0:x', '1:y', '2:z']
B
['x', 'y', 'z']
C
['1:x', '2:y', '3:z']
D
Throws TypeError
21

Question 21

Why should map/filter callbacks stay pure?

A
Purity keeps transformations predictable and avoids hidden side effects
B
Purity shortens execution time to O(1)
C
Purity enables automatic memoization
D
Purity clones DOM nodes
22

Question 22

How can you ensure immutability when mapping objects?

A
Return new objects (e.g., using spread) instead of mutating existing references
B
Freeze the original array before mapping
C
Use var declarations
D
Call delete on properties
23

Question 23

What prints?

javascript
const items = [{ count: 1 }, { count: 2 }]
const doubled = items.map(item => ({ ...item, count: item.count * 2 }))
doubled[0].count = 99
console.log(items[0].count)
A
1
B
99
C
undefined
D
Throws TypeError
24

Question 24

Why is mutating the array inside a map callback discouraged?

A
It introduces surprising side effects and can break assumptions about data flow
B
It speeds up the callback
C
It forces map to return undefined
D
It reduces memory usage automatically
25

Question 25

How does reduce help maintain immutability?

A
By producing new accumulator values without changing source items
B
By freezing the array
C
By deleting array elements automatically
D
By forcing pass-by-value semantics
26

Question 26

What prints?

javascript
const result = [1, 2, 3, 4]
  .filter(n => n % 2 === 0)
  .map(n => n * 10)
console.log(result)
A
[20, 40]
B
[10, 30]
C
[1, 2, 3, 4]
D
[]
27

Question 27

Why is chaining map/filter/reduce powerful?

A
It builds declarative pipelines that read like data requirements
B
It turns synchronous code into async code
C
It mutates arrays faster
D
It forces tail-call optimization
28

Question 28

What prints?

javascript
const pipeline = [
  { price: 5, inStock: true },
  { price: 12, inStock: false },
  { price: 8, inStock: true }
]
const total = pipeline
  .filter(item => item.inStock)
  .map(item => item.price)
  .reduce((sum, price) => sum + price, 0)
console.log(total)
A
13
B
25
C
8
D
0
29

Question 29

When chaining multiple methods, what helps readability?

A
Break complex callbacks into named functions and format each method on its own line
B
Avoid indentation entirely
C
Use reduce for every step
D
Mix synchronous and asynchronous callbacks randomly
30

Question 30

What prints?

javascript
const names = ['Ada', 'Grace', 'Linus']
const sentence = names
  .map(name => name.toUpperCase())
  .join(', ')
console.log(sentence)
A
ADA, GRACE, LINUS
B
['ADA', 'GRACE', 'LINUS']
C
Ada, Grace, Linus
D
[]
31

Question 31

Which pattern suits reduce well?

A
Grouping items by category into an object
B
Finding the first match
C
Flattening nested promises automatically
D
Scheduling tasks asynchronously
32

Question 32

Why does map produce the same number of items as the original array?

A
map transforms elements but never removes or adds entries
B
map automatically filters out falsy values
C
map enforces strict equality checks
D
map deduplicates duplicates
33

Question 33

What prints?

javascript
const scores = [40, 80, 90, 55]
const passing = scores.filter(score => score >= 60).length
console.log(passing)
A
2
B
4
C
3
D
0
34

Question 34

Which statement contrasts map vs reduce?

A
map returns an array; reduce can return any type depending on accumulator
B
Both always return arrays
C
reduce returns arrays while map returns scalars
D
Only reduce accepts callbacks
35

Question 35

How does filter differ from map regarding side effects?

A
filter expects a boolean condition and avoids transforming values, while map expects a transformation result
B
filter mutates the source by default
C
map automatically clones objects deeply
D
filter runs in O(1) time

QUIZZES IN JavaScript