JavaScript Arrays & Array Methods MCQs

1 min read

JavaScript arrays are dynamic, high-level list-like data structures designed to store ordered collections of data types under a single variable. They come equipped with a rich suite of prototype methods categorized into mutating operations (such as push, pop, splice, and sort) and non-mutating operations (such as slice, concat, map, and filter). Modern ECMAScript enhancements have expanded this toolkit with non-mutating copy alternatives like toSorted(), alongside declarative iteration techniques (reduce, every, some, flatMap). Understanding how reference mutability, execution callback parameters, and index boundaries behave is essential for writing robust code and succeeding in technical interviews.

1. Which method adds one or more elements to the end of an array and returns its new length?

a) push()
b) pop()
c) shift()
d) unshift()
Correct Answer: a) push()
Explanation:
The push() method appends elements to the end of an array and mutates the original array, returning the new length.

2. What does the Array.prototype.map() method return?

a) A single accumulated value
b) A boolean indicating if any element passes a test
c) A new array populated with the results of calling a provided function on every element
d) The original array modified in place
Correct Answer: c) A new array populated with the results of calling a provided function on every element
Explanation:
map() creates a brand-new array containing the results of applying the callback function to each element of the calling array.

3. Which method removes the first element from an array and returns that removed element?

a) pop()
b) shift()
c) unshift()
d) slice()
Correct Answer: b) shift()
Explanation:
The shift() method removes the first index element, shifts subsequent indices down by one, and returns the removed value.

4. How does Array.prototype.filter() differ from Array.prototype.find()?

a) filter() returns a new array of all matching elements, whereas find() returns only the first single matching element
b) filter() mutates the original array, whereas find() does not
c) filter() only works on numbers, whereas find() works on strings
d) There is no functional difference
Correct Answer: a) filter() returns a new array of all matching elements, whereas find() returns only the first single matching element
Explanation:
filter() iterates through the entire array collecting all elements that satisfy the condition, while find() stops immediately upon finding the first match.

5. What is the output of evaluating [1, 2, 3].reduce((acc, curr) => acc + curr, 10)?

a) 6
b) 10
c) 16
d) [1, 2, 3, 10]
Correct Answer: c) 16
Explanation:
The initial value of accumulator is set to 10. Adding 1, 2, and 3 to 10 gives 16.

6. Which method flattens nested arrays up to a specified depth?

a) concat()
b) flat()
c) splice()
d) join()
Correct Answer: b) flat()
Explanation:
The flat() method creates a new array with sub-array elements concatenated recursively up to the specified depth.

7. What does Array.prototype.forEach() return?

a) A new mapped array
b) Undefined
c) A boolean
d) The original array
Correct Answer: b) Undefined
Explanation:
forEach() is designed strictly for side effects and always returns undefined, unlike map() or filter().

8. What is the primary difference between slice() and splice()?

a) slice() mutates the array, while splice() does not
b) slice() returns a shallow copy of a portion without mutating, whereas splice() mutates the array by removing or replacing elements in place
c) splice() only works on typed arrays
d) slice() adds elements to the start
Correct Answer: b) slice() returns a shallow copy of a portion without mutating, whereas splice() mutates the array by removing or replacing elements in place
Explanation:
slice() is non-mutating and extracts a subset, while splice() modifies the source array directly.

9. Which method checks if *every* element in an array passes the test implemented by a callback?

a) some()
b) every()
c) filter()
d) includes()
Correct Answer: b) every()
Explanation:
The every() method returns true only if all elements pass the test; otherwise, it returns false.

10. How do you check if a primitive value exists inside an array?

a) Array.prototype.has()
b) Array.prototype.exists()
c) Array.prototype.includes()
d) Array.prototype.contains()
Correct Answer: c) Array.prototype.includes()
Explanation:
includes() determines whether an array includes a certain value, returning true or false accordingly (and correctly handles NaN).

11. What does the pop() method do?

a) Removes the last element from an array and returns it
b) Removes the first element from an array and returns it
c) Adds an element to the end
d) Adds an element to the start
Correct Answer: a) Removes the last element from an array and returns it
Explanation:
pop() mutates the array by removing its final element and returning that element.

12. Which method adds one or more elements to the *beginning* of an array?

a) push()
b) pop()
c) shift()
d) unshift()
Correct Answer: d) unshift()
Explanation:
unshift() inserts elements at the start of an array, shifting existing elements to higher indices, and returns the new length.

13. What does Array.prototype.some() return if at least one element passes the test?

a) true
b) false
c) The matching element
d) An array of matches
Correct Answer: a) true
Explanation:
some() tests whether at least one element passes the callback test, returning true immediately upon success.

14. How does Array.prototype.findIndex() behave when no elements match the condition?

a) Returns null
b) Returns undefined
c) Returns -1
d) Throws an error
Correct Answer: c) Returns -1
Explanation:
findIndex() returns the index of the first element that satisfies the testing function, or -1 if no elements pass.

15. What is the default sorting behavior of the Array.prototype.sort() method when called without a compare function?

a) Numerical ascending order
b) Alphabetical order by converting elements to strings
c) Random order
d) Descending order
Correct Answer: b) Alphabetical order by converting elements to strings
Explanation:
Without a compare function, sort() converts elements to strings and compares their UTF-16 code unit values, which can lead to unexpected numerical sorts (e.g., '10' comes before '2').

16. Which method joins all elements of an array into a string using a specified separator?

a) concat()
b) join()
c) toString()
d) split()
Correct Answer: b) join()
Explanation:
The join() method creates and returns a new string by concatenating all elements in an array, separated by commas or a specified separator string.

17. What does Array.isArray(value) return if the argument is a standard array?

a) false
b) true
c) "array"
d) "object"
Correct Answer: b) true
Explanation:
Array.isArray() reliably determines whether the passed value is an array, returning true for arrays and false otherwise.

18. How can you create a shallow copy of an array in modern JavaScript?

a) Using the spread operator [...arr]
b) Using arr.slice()
c) Using Array.from(arr)
d) All of the above
Correct Answer: d) All of the above
Explanation:
Spread syntax, slice(), and Array.from() are all standard, efficient ways to create shallow copies of arrays.

19. What is the purpose of Array.prototype.fill()?

a) To fill all elements of an array from a start index to an end index with a static value
b) To fill empty array slots with random numbers
c) To resize an array dynamically
d) To map new values using a callback
Correct Answer: a) To fill all elements of an array from a start index to an end index with a static value
Explanation:
fill() mutates the array by replacing elements with a static value between specified start and end positions.

20. What does Array.prototype.reverse() do?

a) Returns a new reversed array without mutating the original
b) Reverses an array *in place* and returns the mutated array
c) Reverses only the first half of an array
d) Throws a TypeError if the array has numbers
Correct Answer: b) Reverses an array *in place* and returns the mutated array
Explanation:
reverse() mutates the calling array directly by reversing the order of its elements in place.

21. What is the return value of Array.prototype.indexOf() if the search element is not found?

a) null
b) undefined
c) -1
d) false
Correct Answer: c) -1
Explanation:
indexOf() returns the first index at which a given element can be found, or -1 if it is not present.

22. Which method combines two or more arrays into a new combined array without mutating the originals?

a) push()
b) concat()
c) splice()
d) join()
Correct Answer: b) concat()
Explanation:
concat() is a non-mutating method used to merge arrays, returning a new combined array.

23. What does the flatMap() method do?

a) First maps each element using a mapping function, then flattens the result into a new array of depth 1
b) Flattens an array and then sorts it
c) Filters an array and maps it simultaneously
d) Converts nested objects into flat key-value pairs
Correct Answer: a) First maps each element using a mapping function, then flattens the result into a new array of depth 1
Explanation:
flatMap() is identical to a map() followed by a flat() of depth 1, but is more efficient when combined.

24. How does the at() method (introduced in ES2022) differ from bracket notation arr[index]?

a) It supports negative integers to count backward from the last element
b) It only works on positive integers
c) It mutates the array
d) It returns a promise
Correct Answer: a) It supports negative integers to count backward from the last element
Explanation:
arr.at(-1) returns the last element of the array, whereas arr[-1] with bracket notation evaluates to undefined.

25. What are ES2023 immutable array methods that return new arrays instead of mutating in place?

a) push(), pop(), shift()
b) toSorted(), toReversed(), toSpliced(), with()
c) map(), filter(), reduce()
d) slice(), concat(), flat()
Correct Answer: b) toSorted(), toReversed(), toSpliced(), with()
Explanation:
ES2023 added copy-by-default (immutable) counterparts to traditional mutating methods: toSorted(), toReversed(), toSpliced(), and with().

26. What does the with(index, value) method (ES2023) return?

a) The modified original array
b) A new array with the element at the specified index replaced by the given value
c) The replaced element
d) A boolean indicating success
Correct Answer: b) A new array with the element at the specified index replaced by the given value
Explanation:
with() is a safe, immutable way to update a single index in an array without mutating the original structure.

27. What is a 'sparse array' in JavaScript?

a) An array containing only boolean values
b) An array that has empty slots (holes) where indices have no assigned elements
c) An array with a length of zero
d) An array stored in compressed memory
Correct Answer: b) An array that has empty slots (holes) where indices have no assigned elements
Explanation:
Sparse arrays contain empty slots created via omissions or explicit array constructor sizing (e.g., new Array(5)), which are often skipped by methods like map() and forEach().

28. What happens to the length property of an array when you explicitly set arr.length = 0?

a) Nothing happens
b) All elements are removed, effectively clearing the array
c) It throws a TypeError
d) It converts the array into an object
Correct Answer: b) All elements are removed, effectively clearing the array
Explanation:
Setting an array's length property to 0 is a classic, high-performance way to truncate and empty an array.

29. What is the result of deleting an element using the `delete arr[0]` operator?

a) The element is removed and indices are shifted down
b) The element is deleted, leaving an empty slot (hole) and keeping the array length unchanged
c) The array is completely cleared
d) Throws a syntax error
Correct Answer: b) The element is deleted, leaving an empty slot (hole) and keeping the array length unchanged
Explanation:
The delete operator removes a property without updating the length or shifting indices, turning the array sparse.

30. How does Array.from() differ from creating an array with the Array() constructor?

a) Array.from() creates an array from iterable or array-like objects, whereas Array(3) creates a sparse array with 3 empty slots
b) Array.from() only works on strings
c) Array(3) is non-mutating while Array.from() is mutating
d) There is no difference
Correct Answer: a) Array.from() creates an array from iterable or array-like objects, whereas Array(3) creates a sparse array with 3 empty slots
Explanation:
Passing a single number to new Array(3) creates 3 empty holes, whereas Array.from({length: 3}) populates undefined or mapped values.

31. What does Array.of(1, 2, 3) produce?

a) A sparse array of length 3
b) [1, 2, 3]
c) An error
d) A string '1,2,3'
Correct Answer: b) [1, 2, 3]
Explanation:
Array.of() creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

32. What does Array.prototype.copyWithin() do?

a) Copies a sequence of array elements to another position within the same array in place
b) Copies an array to a completely separate memory heap
c) Creates a deep clone of an array
d) Duplicates an array and appends it to the end
Correct Answer: a) Copies a sequence of array elements to another position within the same array in place
Explanation:
copyWithin() shallow copies part of an array to another location in the same array without modifying its length, returning it mutated.

33. What does the entries() method return when called on an array?

a) An array of values
b) An iterator yielding key/value pairs [index, element] for each index
c) An array of indices
d) A boolean
Correct Answer: b) An iterator yielding key/value pairs [index, element] for each index
Explanation:
arr.entries() returns a new Array Iterator object that contains the key/value pairs for each index in the array.

34. What does the keys() method return when called on an array?

a) An iterator yielding the keys (indices) of the array
b) An array of values
c) The object keys
d) Undefined
Correct Answer: a) An iterator yielding the keys (indices) of the array
Explanation:
arr.keys() returns a new Array Iterator object that contains the keys (indices) for each index in the array.

35. What does the values() method return when called on an array?

a) An iterator yielding the values for each index in the array
b) An array of keys
c) A string representation
d) A boolean
Correct Answer: a) An iterator yielding the values for each index in the array
Explanation:
arr.values() returns a new Array Iterator object that iterates through the values of each index.

36. How do you evaluate if two distinct array references contain the exact same elements in JavaScript?

a) arr1 === arr2
b) arr1 == arr2
c) By comparing stringified representations or writing a custom deep equality check
d) arr1.equals(arr2)
Correct Answer: c) By comparing stringified representations or writing a custom deep equality check
Explanation:
Arrays are reference types in JavaScript, so `===` checks reference equality, not structural equality.

37. What is the result of [1, 2] + [3, 4] in JavaScript?

a) [1, 2, 3, 4]
b) "1,23,4"
c) TypeError
d) 7
Correct Answer: b) "1,23,4"
Explanation:
The `+` operator triggers type coercion, converting both arrays to strings via toString() and concatenating them into "1,23,4".

38. What does the code `[..."hello"]` evaluate to?

a) "hello"
b) ["h", "e", "l", "l", "o"]
c) [ "hello" ]
d) TypeError
Correct Answer: b) ["h", "e", "l", "l", "o"]
Explanation:
Strings are iterables in JavaScript, so spreading a string unpacks each character into an array.

39. What is array destructuring used for?

a) Extracting values from arrays into distinct variables using syntax like `const [a, b] = arr;`
b) Deleting array elements
c) Merging two arrays
d) Sorting array elements
Correct Answer: a) Extracting values from arrays into distinct variables using syntax like `const [a, b] = arr;`
Explanation:
Array destructuring is a convenient ES6 syntax expression to unpack values from arrays into separate variables.

40. How do you skip elements during array destructuring?

a) Using commas without variable names, e.g., `const [a, , c] = arr;`
b) Using `null` placeholders
c) Using `undefined` keywords
d) It is not possible
Correct Answer: a) Using commas without variable names, e.g., `const [a, , c] = arr;`
Explanation:
Empty slots in array destructuring assignment patterns (using extra commas) allow skipping elements.

41. What does the rest parameter syntax (`...rest`) do in array destructuring?

a) Collects all remaining elements of the array into a new array
b) Deletes the rest of the array
c) Reverses the rest of the array
d) Throws a syntax error
Correct Answer: a) Collects all remaining elements of the array into a new array
Explanation:
The rest element collects any remaining unpacked elements into a new array container.

42. What is the time complexity of pushing an element to the end of a standard JavaScript array?

a) O(n)
b) O(1) amortized
c) O(log n)
d) O(n^2)
Correct Answer: b) O(1) amortized
Explanation:
Array push operations run in constant amortized time because underlying dynamic allocations occur infrequently.

43. What is the time complexity of shifting an element from the beginning of a standard JavaScript array (`arr.shift()`)?

a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
Correct Answer: b) O(n)
Explanation:
shift() requires re-indexing every single remaining element in the array down by one position, resulting in O(n) linear time.

44. What are TypedArrays in JavaScript?

a) Arrays that can store mixed data types like strings and booleans
b) Array-like views onto an underlying binary data buffer (e.g., Float32Array, Uint8Array)
c) Arrays validated by TypeScript compiler
d) Arrays that cannot be mutated
Correct Answer: b) Array-like views onto an underlying binary data buffer (e.g., Float32Array, Uint8Array)
Explanation:
TypedArrays provide mechanism for reading and writing raw binary data streams in memory.

45. What is the result of `[1, 2, 3].map(parseInt)` in JavaScript?

a) [1, 2, 3]
b) [1, NaN, NaN]
c) [1, 0, 0]
d) TypeError
Correct Answer: b) [1, NaN, NaN]
Explanation:
map passes (element, index, array) to parseInt. parseInt(1, 0) is 1, parseInt(2, 1) is NaN, and parseInt(3, 2) evaluates to NaN because 3 is invalid in base 2.

46. Which array method can be used to check if an array contains a specific element, returning true or false?

a) indexOf()
b) includes()
c) find()
d) search()
Correct Answer: b) includes()
Explanation:
includes() is specifically designed to return a boolean indicating whether a value exists in the array.

47. How do you empty an array while maintaining references pointing to it from other variables?

a) arr = []
b) arr.length = 0
c) arr.clear()
d) delete arr
Correct Answer: b) arr.length = 0
Explanation:
Setting length to 0 clears the array contents in place, preserving any other variable references pointing to that exact array object.

48. What does Array.prototype.findLast() (ES2023) do?

a) Returns the value of the last element in the array that satisfies the testing function
b) Returns the index of the last element
c) Reverses the array and finds the first element
d) Deletes the last element
Correct Answer: a) Returns the value of the last element in the array that satisfies the testing function
Explanation:
findLast() iterates backwards from the end of the array to find and return the last matching element.

49. What does Array.prototype.findLastIndex() (ES2023) return?

a) The index of the last element that passes the test, or -1 if none match
b) The total count of matching elements
c) The first element's index
d) Undefined
Correct Answer: a) The index of the last element that passes the test, or -1 if none match
Explanation:
findLastIndex() searches backward and returns the index of the last matching element.

50. Can standard JavaScript arrays contain mixed data types (e.g., numbers, strings, objects, functions)?

a) No, all elements must be of the same type
b) Yes, JS arrays are heterogeneous and can store any mix of data types
c) Only numbers and strings are allowed
d) Only objects and arrays are allowed
Correct Answer: b) Yes, JS arrays are heterogeneous and can store any mix of data types
Explanation:
Unlike typed arrays or arrays in strongly-typed languages, JavaScript arrays can hold elements of any data type simultaneously.

51. What is the return type of Array.prototype.reduce()?

a) Always an array
b) Always a number
c) Whatever type the accumulator evaluates to after processing
d) Always a boolean
Correct Answer: c) Whatever type the accumulator evaluates to after processing
Explanation:
reduce() can return a number, string, object, boolean, or array depending on how the accumulator is initialized and updated.

52. What happens if you call reduce() on an empty array without providing an initial value?

a) Returns undefined
b) Returns 0
c) Throws a TypeError
d) Returns []
Correct Answer: c) Throws a TypeError
Explanation:
Calling reduce() on an empty array with no initial value provides no starting element, causing a TypeError.

53. How do you clone a multidimensional array deeply without retaining inner object/array references?

a) Using [...arr]
b) Using arr.slice()
c) Using JSON.parse(JSON.stringify(arr)) or a recursive deep-clone utility
d) Using Array.from(arr)
Correct Answer: c) Using JSON.parse(JSON.stringify(arr)) or a recursive deep-clone utility
Explanation:
Spread, slice, and Array.from() only create shallow copies; nested arrays/objects must be deep cloned separately.

54. What does arr.slice() with no arguments do?

a) Clears the array
b) Returns a shallow copy of the entire array
c) Returns an empty array
d) Throws a TypeError
Correct Answer: b) Returns a shallow copy of the entire array
Explanation:
Calling slice() with no parameters extracts from index 0 to the end, producing a complete shallow clone.

55. What is the role of the second parameter in arr.splice(start, deleteCount, item1, item2)?

a) The number of elements to remove from the array starting at the start index
b) The ending index of the slice
c) The value to fill into the array
d) The sorting direction
Correct Answer: a) The number of elements to remove from the array starting at the start index
Explanation:
deleteCount specifies how many elements should be removed from the start index during a splice operation.

56. What is the output of [3, 1, 10, 2].sort() without a comparator?

a) [1, 2, 3, 10]
b) [1, 10, 2, 3]
c) [10, 3, 2, 1]
d) [3, 2, 1, 10]
Correct Answer: b) [1, 10, 2, 3]
Explanation:
Default sorting converts numbers to strings, comparing code units ('10' comes before '2').

57. How do you correctly sort numbers numerically in ascending order using sort()?

a) arr.sort()
b) arr.sort((a, b) => a - b)
c) arr.sort((a, b) => b - a)
d) arr.numericalSort()
Correct Answer: b) arr.sort((a, b) => a - b)
Explanation:
A comparator function returning a negative, zero, or positive value based on (a - b) ensures proper numerical ascending order.

58. What does the expression `Array(3).fill(0)` produce?

a) []
b) [0, 0, 0]
c) [3, 3, 3]
d) TypeError
Correct Answer: b) [0, 0, 0]
Explanation:
Array(3) creates 3 empty slots, and .fill(0) replaces them with the number 0, yielding [0, 0, 0].

59. What is the return value of arr.push(10) when called on [1, 2]?

a) [1, 2, 10]
b) 3
c) 10
d) undefined
Correct Answer: b) 3
Explanation:
The push() method returns the new length of the array, which is 3.

60. Which method can remove elements from the middle of an array and insert new ones simultaneously?

a) slice()
b) splice()
c) concat()
d) shift()
Correct Answer: b) splice()
Explanation:
splice() is versatile, allowing simultaneous removal and insertion of elements at any index.

61. What does arr.toString() return for an array [1, 'apple', true]?

a) A JSON string
b) "1,apple,true"
c) An object representation
d) TypeError
Correct Answer: b) "1,apple,true"
Explanation:
toString() returns a string representing the specified array and its elements separated by commas.

62. Can you use `break` or `continue` statements inside an Array.prototype.forEach() loop?

a) Yes
b) No, it throws a SyntaxError
c) Yes, but only break
d) Yes, but only continue
Correct Answer: b) No, it throws a SyntaxError
Explanation:
forEach() takes a callback function, so break and continue cannot be used inside it directly (use for...of or standard for loops instead).

63. Which loop structure allows the use of `break` and `continue` with arrays?

a) forEach()
b) map()
c) for...of loop
d) filter()
Correct Answer: c) for...of loop
Explanation:
The for...of loop iterates over iterable array values and fully supports control flow statements like break and continue.

64. What is the output of `[1, 2, 3].includes(2, 1)`?

a) false
b) true
c) 1
d) 2
Correct Answer: b) true
Explanation:
The second parameter (1) specifies the starting index for the search. Searching for 2 starting from index 1 returns true.

65. What does `[5, 12, 8, 130, 44].findIndex(num => num > 10)` return?

a) 12
b) 1
c) 3
d) 0
Correct Answer: b) 1
Explanation:
The first element greater than 10 is 12, which is located at index 1.

66. What is the purpose of the accumulator in Array.prototype.reduce()?

a) To store the running total or final aggregated result returned across iterations
b) To filter out unwanted elements
c) To reverse the array
d) To store array indices
Correct Answer: a) To store the running total or final aggregated result returned across iterations
Explanation:
The accumulator accumulates the callback's return values across each element of the array.

67. How do you convert an array-like object (like NodeList or arguments) into a true JavaScript array?

a) Array.from()
b) [...nodeList]
c) Array.prototype.slice.call(nodeList)
d) All of the above
Correct Answer: d) All of the above
Explanation:
Array.from(), spread syntax, and old-school slice.call() all successfully convert array-like objects into genuine arrays.

68. What is the result of `[1, [2, [3, 4]]].flat(2)`?

a) [1, 2, [3, 4]]
b) [1, 2, 3, 4]
c) [1, [2, 3, 4]]
d) TypeError
Correct Answer: b) [1, 2, 3, 4]
Explanation:
Passing a depth of 2 flattens sub-arrays nested up to two levels deep, resulting in a fully flattened [1, 2, 3, 4].

69. What does `[1, 2, 3].slice(-2)` return?

a) [1, 2]
b) [2, 3]
c) [3]
d) []
Correct Answer: b) [2, 3]
Explanation:
Negative indices in slice() specify an offset from the end of the array, extracting the last two elements.

70. Which array method returns the index of the *last* occurrence of a specified value?

a) indexOf()
b) lastIndexOf()
c) findIndex()
d) findLastIndex()
Correct Answer: b) lastIndexOf()
Explanation:
lastIndexOf() searches the array backward, returning the index of the last matching occurrence.

71. What happens when you pass a negative start index to splice(startIndex, deleteCount)?

a) It throws an error
b) It counts backward from the end of the array to determine the starting point
c) It ignores the negative sign
d) It starts at index 0
Correct Answer: b) It counts backward from the end of the array to determine the starting point
Explanation:
Splice accepts negative start indexes, treating them as offset indices from the array's end.

72. What is the output of `[].pop()`?

a) null
b) undefined
c) 0
d) TypeError
Correct Answer: b) undefined
Explanation:
Popping an empty array does nothing and returns undefined without throwing an error.

73. What is the output of `[].shift()`?

a) null
b) undefined
c) 0
d) TypeError
Correct Answer: b) undefined
Explanation:
Shifting an empty array returns undefined safely.

74. Which method is best suited to transform every element of an array into a different structure?

a) filter()
b) forEach()
c) map()
d) reduce()
Correct Answer: c) map()
Explanation:
map() is specifically built for transforming 1:1 elements into a new array.

75. Which method is best suited to reduce an array of items down to a single summary value?

a) map()
b) filter()
c) reduce()
d) some()
Correct Answer: c) reduce()
Explanation:
reduce() accumulates array items into a single final output value.

76. What does `Array(5)` create in JavaScript?

a) [0, 0, 0, 0, 0]
b) [undefined, undefined, undefined, undefined, undefined]
c) A sparse array with length 5 and no assigned elements
d) An error
Correct Answer: c) A sparse array with length 5 and no assigned elements
Explanation:
Passing a single numeric argument to the Array constructor creates an empty sparse array with the specified length.

77. How can you verify if a variable is an array in older JavaScript environments lacking Array.isArray?

a) typeof arr === 'array'
b) Object.prototype.toString.call(arr) === '[object Array]'
c) arr instanceof Object
d) arr.constructor === String
Correct Answer: b) Object.prototype.toString.call(arr) === '[object Array]'
Explanation:
Object toString tagging is the classic, foolproof way to verify array types across execution contexts and frames.

78. What does `[1, 2, 3].concat([4, 5], 6)` return?

a) [1, 2, 3, 4, 5, 6]
b) [[1, 2, 3], [4, 5], 6]
c) [1, 2, 3, [4, 5], 6]
d) TypeError
Correct Answer: a) [1, 2, 3, 4, 5, 6]
Explanation:
concat() flattens arrays passed as arguments one level deep while merging values.

79. What does `[1, 2, 3].reverse()` do to the original array?

a) Returns a new reversed array without modifying the original
b) Mutates the original array in place and returns it
c) Throws an error
d) Returns undefined
Correct Answer: b) Mutates the original array in place and returns it
Explanation:
reverse() is a mutating method that reverses array element order in place.

80. Which method creates a new array with all elements that pass a test implemented by a callback function?

a) map()
b) filter()
c) reduce()
d) find()
Correct Answer: b) filter()
Explanation:
filter() collects all elements returning true into a new filtered array.

81. What is the return value of `[5, 10, 15].every(n => n > 0)`?

a) false
b) true
c) 15
d) undefined
Correct Answer: b) true
Explanation:
Every element in the array is strictly greater than 0, so every() returns true.

82. What is the return value of `[5, 10, -3].some(n => n < 0)`?

a) false
b) true
c) -3
d) undefined
Correct Answer: b) true
Explanation:
-3 is less than 0, satisfying the condition for at least one element, so some() returns true.

83. What is the output of `[ 'a', 'b', 'c' ].join('-')`?

a) ["a-b-c"]
b) "a-b-c"
c) "a,b,c"
d) TypeError
Correct Answer: b) "a-b-c"
Explanation:
join('-') concatenates the array elements into a string separated by hyphens.

84. What does `[1, 2, 3].indexOf(2)` return?

a) 0
b) 1
c) 2
d) -1
Correct Answer: b) 1
Explanation:
The number 2 is located at index 1 of the array.

85. What does `[NaN].indexOf(NaN)` evaluate to?

a) 0
b) -1
c) undefined
d) NaN
Correct Answer: b) -1
Explanation:
indexOf() uses strict equality (===) under the hood, and NaN !== NaN, so indexOf cannot locate NaN.

86. What does `[NaN].includes(NaN)` evaluate to?

a) false
b) true
c) 0
d) -1
Correct Answer: b) true
Explanation:
Unlike indexOf(), includes() correctly handles NaN using SameValueZero equality semantics.

87. How do you remove the element at a specific index `i` without leaving empty holes?

a) delete arr[i]
b) arr.splice(i, 1)
c) arr.pop()
d) arr.shift()
Correct Answer: b) arr.splice(i, 1)
Explanation:
splice(i, 1) removes 1 element at index i and shifts subsequent elements to fill the gap.

88. What is the purpose of the callback's third argument in methods like map() or forEach()?

a) The original array being traversed
b) The accumulator
c) The index
d) The length of the array
Correct Answer: a) The original array being traversed
Explanation:
Standard array method callbacks receive (element, index, array) as arguments.

89. What happens if you modify the length of an array to be smaller than its current length?

a) Elements beyond the new length are deleted
b) Nothing happens
c) Throws a RangeError
d) Elements are shifted
Correct Answer: a) Elements beyond the new length are deleted
Explanation:
Truncating an array's length property deletes all elements at indices greater than or equal to the new length.

90. What is the output of `Array.isArray([])`?

a) false
b) true
c) "array"
d) "object"
Correct Answer: b) true
Explanation:
Array.isArray() correctly identifies an array literal as an array.

91. Which of the following creates a new array with elements sorted in ascending order without mutating the original (ES2023)?

a) arr.sort()
b) arr.toSorted()
c) arr.sorted()
d) arr.orderBy()
Correct Answer: b) arr.toSorted()
Explanation:
toSorted() is the modern, immutable counterpart to sort().

92. Which of the following creates a new array with elements reversed without mutating the original (ES2023)?

a) arr.reverse()
b) arr.toReversed()
c) arr.invert()
d) arr.flip()
Correct Answer: b) arr.toReversed()
Explanation:
toReversed() returns a new array with elements in reverse order without modifying the source array.

93. What is the primary benefit of ES2023 immutable array methods?

a) They execute twice as fast
b) They prevent accidental state mutation bugs in functional programming and state management libraries
c) They eliminate memory leaks
d) They work on strings directly
Correct Answer: b) They prevent accidental state mutation bugs in functional programming and state management libraries
Explanation:
Returning new copies avoids side effects and unintended mutations, making state changes predictable.

94. What does `[1, 2, 3].copyWithin(0, 1)` return?

a) [2, 3, 3]
b) [1, 2, 3]
c) [2, 1, 3]
d) [3, 2, 1]
Correct Answer: a) [2, 3, 3]
Explanation:
copyWithin copies elements from index 1 onward to index 0, mutating the array in place to [2, 3, 3].
Next →: JavaScript Asynchronous Programming
NewJavaScript DOM Manipulation MCQs

JavaScript DOM Manipulation MCQs

The Document Object Model (DOM) is a cross-platform programming interface that treats HTML and XML documents as a hierarchical tree…

By MCQs Generator
NewPython OOP MCQs

Python OOP MCQs

Object-Oriented Programming (OOP) in Python is a programming paradigm that uses classes and objects to model real world entities, promoting…

By MCQs Generator
NewPython Functions MCQs

Python Functions MCQs

Functions in Python are reusable blocks of code designed to perform a specific task, promoting code modularity, readability, and DRY…

By MCQs Generator