Arrays VIII
1.
How do you remove duplicate elements from an array in JavaScript?
array.unique()
Array.from(new Set(array))
array.removeDuplicates()
array.filter(item => array.indexOf(item) === array.lastIndexOf(item))
2.
How do you find the index of the first occurrence of a specific element in an array?
array.findIndexOf(element)
array.firstIndexOf(element)
array.indexOf(element)
array.index(element)
3.
What does the `Array.isArray()` method check for?
Whether the array has a specific length
Whether the array is empty
Whether the argument is an array
Whether the array is sorted
4.
How do you find the maximum value in an array in JavaScript?
Math.max(...array)
array.max()
max(array)
array.maximum()
5.
Given the array `var numbers = [1, 2, 3, 4, 5];`, what is the result of `numbers.slice(1, 4)`?
[1, 2, 3]
[1, 2, 3, 4]
[2, 3, 4]
[4, 5]
6.
What is the result of `Array.from({ length: 5 }, (_, index) => index * 2)`?
[undefined, undefined, undefined, undefined, undefined]
[2, 4, 6, 8, 10]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
7.
What is the purpose of the `Array.prototype.some()` method in JavaScript?
Checks if at least one element passes a test
Adds a new element to the array
Removes elements from the array
Sorts the elements of an array
8.
How can you reverse the order of elements in an array without using the `reverse()` method?
array.map((_, i, arr) => arr[arr.length - 1 - i])
array.reduceRight((acc, cur) => (acc.push(cur), acc), [])
array.sort((a, b) => b - a)
array.slice().reverse()
9.
What is the result of `[1, 2, 3, 4, 5].splice(2, 1)`?
[1, 2, 3, 4]
[1, 2, 4, 5]
[3]
[2, 3, 4, 5]
10.
What is the purpose of the `Array.prototype.copyWithin()` method in JavaScript?
Duplicates the entire array
Copies a sequence of elements to a specified destination index
Adds new elements to the array
Removes elements from the array
11.
Given the array `var fruits = ['apple', 'orange', 'banana'];`, what is the result of `fruits.sort()`?
['apple', 'banana', 'orange']
['banana', 'apple', 'orange']
['orange', 'banana', 'apple']
['apple', 'orange', 'banana']
12.
How can you remove the last element from an array in JavaScript?
array.splice(-1, 1)
array.pop()
array.removeLast()
array.slice(0, -1)
13.
What will the following code output: `[1, 2, 3, 4, 5].filter(num => num % 2 === 0)`?
[2, 4]
[1, 3, 5]
[2]
[4, 5]
14.
How can you convert a string to an array of characters in JavaScript?
Array.from(str)
str.split('')
str.toArray()
Array.of(str)
15.
What is the purpose of the `Array.prototype.entries()` method in JavaScript?
Returns an iterator of array elements
Returns an iterator of array indices
Returns an iterator of array values
Returns an iterator of key/value pairs
16.
How can you check if two arrays are equal in JavaScript?
array1.isEqual(array2)
array1.equals(array2)
arraysEqual(array1, array2)
JSON.stringify(array1) === JSON.stringify(array2)
17.
What will the following code output: `[1, 2, 3].reduceRight((acc, val) => acc - val)`?
0
-4
4
2
18.
How can you find the sum of all elements in an array?
array.reduce((acc, val) => acc + val, 0)
array.sum()
array.total()
array.aggregate((acc, val) => acc + val)
19.
What is the purpose of the `Array.prototype.flat()` method?
Sorts the elements of an array
Flattens a nested array
Adds new elements to the array
Removes elements from the array
20.
What does the `Array.prototype.reverse()` method do in JavaScript?
Removes elements from the array
Sorts the elements of an array
Adds new elements to the array
Reverses the order of elements in the array