JavaScript MCQs - Arrays, Objects and Higher-Order Functions (Set 5)

JavaScript MCQs - Arrays, Objects and Higher-Order Functions (Set 5) - CodingHumans

Array and object questions come up in almost every JavaScript interview, and most of them are tricky output questions, not definitions. This set covers map, filter, reduce, sort, flat and the newer array methods that do not change the original, plus object key order, shallow and deep copies, and Object.freeze. For every snippet, the explanation tells you exactly why it behaves the way it does.

How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.

JavaScript MCQ series: Set 1 | Set 2 | Set 3 | Set 4 | Set 5

Topics covered in this set

  • map, filter and reduce
  • Default sort behaviour and comparators
  • flat, includes and Array.from
  • Mutating vs non-mutating array methods
  • Object key ordering and property keys
  • Shallow copy, deep copy and Object.freeze

JavaScript MCQs with answers and explanations

Q1. What is the output?

console.log(['1', '2', '3'].map(parseInt));

a) [1, 2, 3]

b) [1, NaN, NaN]

c) [1, NaN, 3]

d) [NaN, NaN, NaN]

Q2. What is the output?

console.log([10, 1, 3, 25].sort());

a) [1, 3, 10, 25]

b) [25, 10, 3, 1]

c) [1, 10, 25, 3]

d) [1, 10, 3, 25]

Q3. What happens here?

const total = [].reduce((acc, n) => acc + n);

a) total is 0

b) total is undefined

c) total is NaN

d) A TypeError is thrown

Q4. What is the output?

const arr = [1, 2, 3];
arr.length = 0;
console.log(arr, arr[0]);

a) [] undefined

b) [1, 2, 3] 1

c) [empty x 3] undefined

d) TypeError

Q5. What does this return?

Array.from({ length: 3 }, (_, i) => i * 2);

a) [undefined, undefined, undefined]

b) [0, 2, 4]

c) [2, 4, 6]

d) [0, 1, 2]

Q6. What is the output?

console.log([1, [2, [3, [4]]]].flat());

a) [1, 2, 3, 4]

b) [1, 2, [3, 4]]

c) [1, 2, [3, [4]]]

d) [[1, 2], [3, [4]]]

Q7. What is the output in non-strict mode?

const o = Object.freeze({ a: 1, b: { c: 2 } });
o.a = 5;
o.b.c = 9;
console.log(o.a, o.b.c);

a) 5 9

b) 1 2

c) 5 2

d) 1 9

Q8. What is the output?

const o = { b: 1, 2: 'x', a: 2, 1: 'y' };
console.log(Object.keys(o));

a) ['b', '2', 'a', '1']

b) ['1', '2', 'b', 'a']

c) ['a', 'b', '1', '2']

d) ['b', 'a', '1', '2']

Q9. What is the output?

const a = { x: { y: 1 } };
const b = { ...a };
b.x.y = 2;
console.log(a.x.y);

a) 2

b) 1

c) undefined

d) TypeError

Q10. Which built-in function creates a deep copy of an object containing nested objects, Dates and Maps?

a) Object.assign()

b) The spread operator

c) structuredClone()

d) Array.prototype.slice()

Q11. What is the output?

const arr = [NaN];
console.log(arr.includes(NaN), arr.indexOf(NaN));

a) true 0

b) false -1

c) false 0

d) true -1

Q12. Which of these array methods does NOT change the original array?

a) sort()

b) reverse()

c) splice()

d) toSorted()

Q13. What is the output?

const r = [1, 2, 3, 4].filter(n => n % 2).map(n => n * n);
console.log(r);

a) [1, 9]

b) [4, 16]

c) [1, 4, 9, 16]

d) [2, 4]

Q14. What is the output?

const k1 = {};
const k2 = {};
const o = {};
o[k1] = 1;
o[k2] = 2;
console.log(o[k1]);

a) 1

b) undefined

c) 2

d) TypeError

Q15. What is the output?

console.log([].every(x => x > 0), [].some(x => x > 0));

a) false false

b) true true

c) false true

d) true false

Quick revision notes

  • map passes (value, index, array) to the callback. That is why map(parseInt) gives strange results.
  • Default sort compares strings. For numbers, always pass a comparator like (a, b) => a - b.
  • reduce on an empty array with no starting value throws a TypeError, so always pass one.
  • Mutating: sort, reverse, splice, push, pop, shift, unshift, fill. Non-mutating copies: toSorted, toReversed, toSpliced, with.
  • Spread and Object.assign are shallow; structuredClone is deep. Object.freeze is shallow.
  • Object keys: integer-like keys first in ascending order, then string keys in insertion order. Objects used as keys become "[object Object]".

Frequently asked questions

What is the difference between map and forEach?

map returns a new array made from what your callback returns, so use it to transform data. forEach returns undefined, so use it only for side effects, like logging or updating something outside the array.

Is the spread operator a deep copy?

No. Spread copies only the top level. Nested objects and arrays are still shared by the original and the copy. When you need a fully separate copy, use structuredClone.

Why does [10, 1, 3].sort() not sort numbers correctly?

The default sort turns values into strings and compares them one character at a time, so "10" comes before "3". Pass (a, b) => a - b to sort by the actual number.

Comments

Answered 0/0Correct 0