MCQs Of Day 19: More Advanced Array Methods

Rashmi Mishra
0

   MCQs Of Day 19: More Advanced Array Methods

1. What does the map() method do in JavaScript?

a) Modifies the original array
b) Returns a new array with transformed values
c) Removes values from the array
d) Iterates through the array without changing it

Answer: b) Returns a new array with transformed values
Explanation: The map() method applies a transformation to each element in an array and returns a new array with the results. It doesn't modify the original array.


2. Which of the following methods is used to filter an array based on a condition?

a) map()
b) filter()
c) reduce()
d) forEach()

Answer: b) filter()
Explanation: The filter() method creates a new array with all elements that pass the test implemented by the provided function.


3. The reduce() method in JavaScript is used for:

a) Iterating over an array
b) Filtering out elements
c) Reducing an array to a single value
d) Mapping over elements

Answer: c) Reducing an array to a single value
Explanation: The reduce() method applies a function against an accumulator and each element in the array to reduce it to a single value.


4. Which method is used to execute a function for each element in an array?

a) map()
b) reduce()
c) forEach()
d) filter()

Answer: c) forEach()
Explanation: The forEach() method executes a provided function once for each element in the array.


5. What is the return value of the find() method?

a) A new array with the found elements
b) The first element that satisfies the condition
c) A boolean indicating if any element matches the condition
d) All elements that match the condition

Answer: b) The first element that satisfies the condition
Explanation: The find() method returns the first element that matches the provided condition. If no element matches, it returns undefined.


6. The some() method in JavaScript checks:

a) If all elements in an array satisfy a condition
b) If none of the elements in an array satisfy a condition
c) If at least one element in an array satisfies a condition
d) If the array is empty

Answer: c) If at least one element in an array satisfies a condition
Explanation: The some() method checks if at least one element in the array satisfies the given condition.


7. Which method is used to check if all elements in an array satisfy a condition?

a) every()
b) some()
c) filter()
d) map()

Answer: a) every()
Explanation: The every() method checks if all elements in the array satisfy the given condition. It returns true if all elements match, otherwise false.


8. The map() method does not modify:

a) The original array
b) The transformed array
c) The condition applied to elements
d) None of the above

Answer: a) The original array
Explanation: The map() method returns a new array with the transformed elements and does not modify the original array.


9. What will the following code return?

javascript

Copy code

let nums = [1, 2, 3, 4];

let doubled = nums.map(num => num * 2);

console.log(doubled);

a) [2, 4, 6, 8]
b) [1, 4, 9, 16]
c) [2, 4, 6, 8, 10]
d) [1, 2, 3, 4]

Answer: a) [2, 4, 6, 8]
Explanation: The map() method doubles each value in the array, resulting in [2, 4, 6, 8].


10. Which of the following is the correct syntax for reduce() method?

a) array.reduce(callback, initialValue)
b) array.reduce(callback)
c) array.reduce(initialValue, callback)
d) array.reduce()

Answer: a) array.reduce(callback, initialValue)
Explanation: The reduce() method takes a callback function and an optional initialValue. The initialValue is used to initialize the accumulator.


11. What will the following code return?

javascript

Copy code

let arr = [10, 20, 30, 40];

let sum = arr.reduce((acc, num) => acc + num, 0);

console.log(sum);

a) 0
b) 10
c) 100
d) 60

Answer: d) 100
Explanation: The reduce() method calculates the sum of the array elements: 10 + 20 + 30 + 40 = 100.


12. How does forEach() differ from map()?

a) forEach() returns a new array, but map() does not
b) forEach() does not return a new array, but map() does
c) forEach() returns the transformed values, while map() executes a function
d) Both return a new array

Answer: b) forEach() does not return a new array, but map() does
Explanation: forEach() executes a function for each element but does not return anything. In contrast, map() returns a new array with transformed values.


13. Which of the following methods does NOT modify the original array?

a) map()
b) filter()
c) reduce()
d) All of the above

Answer: d) All of the above
Explanation: map(), filter(), and reduce() do not modify the original array; they return a new array or a value.


14. What will the following code return?

javascript

Copy code

let numbers = [5, 8, 12, 20];

let divisibleByFive = numbers.filter(num => num % 5 === 0);

console.log(divisibleByFive);

a) [5, 8, 12]
b) [5, 20]
c) [5, 8, 12, 20]
d) []

Answer: b) [5, 20]
Explanation: The filter() method filters out numbers divisible by 5. The result is [5, 20].


15. What will find() return if no element satisfies the condition?

a) undefined
b) null
c) false
d) An empty array

Answer: a) undefined
Explanation: The find() method returns undefined if no element in the array satisfies the provided condition.


16. What will the following code return?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.find(num => num > 3);

console.log(result);

a) 1
b) 3
c) 4
d) undefined

Answer: c) 4
Explanation: The find() method returns the first element that satisfies the condition num > 3. Therefore, it returns 4.


17. How would you check if all numbers in an array are positive?

a) Use map()
b) Use some()
c) Use every()
d) Use filter()

Answer: c) Use every()
Explanation: The every() method checks if all elements in the array satisfy a given condition.


18. What will the following code return?

javascript

Copy code

let nums = [5, 10, 15, 20];

let result = nums.some(num => num < 10);

console.log(result);

a) true
b) false
c) undefined
d) null

Answer: a) true
Explanation: The some() method checks if any element in the array is less than 10. Since 5 satisfies the condition, it returns true.


19. Which method returns a boolean value based on a condition?

a) map()
b) filter()
c) reduce()
d) some()

Answer: d) some()
Explanation: The some() method returns true if at least one element satisfies the condition, otherwise it returns false.


20. What does map() return?

a) A boolean
b) A new array with modified elements
c) A single transformed value
d) undefined

Answer: b) A new array with modified elements
Explanation: The map() method returns a new array with the values transformed by the provided function.


21. What will be the result of the following code?

javascript

Copy code

let nums = [1, 2, 3];

let newNums = nums.map(num => num * 2);

console.log(newNums);

a) [1, 2, 3]
b) [2, 4, 6]
c) [1, 4, 9]
d) [2, 4, 6, 8]

Answer: b) [2, 4, 6]
Explanation: The map() method multiplies each element by 2, resulting in [2, 4, 6].


22. Which of the following array methods does not return a new array?

a) map()
b) reduce()
c) filter()
d) slice()

Answer: b) reduce()
Explanation: The reduce() method reduces an array to a single value, not an array.


23. What is the initial value used in the reduce() method?

a) It is always 0
b) It is provided as the second argument to the reduce() method
c) It is calculated by the browser automatically
d) It is undefined

Answer: b) It is provided as the second argument to the reduce() method
Explanation: The second argument of reduce() is the initial value for the accumulator. If not provided, the first element of the array is used.


24. What will the following code output?

javascript

Copy code

let nums = [10, 20, 30, 40];

let result = nums.reduce((acc, num) => acc + num, 100);

console.log(result);

a) 100
b) 200
c) 300
d) 150

Answer: d) 150
Explanation: The reduce() method starts with an accumulator value of 100, and adds each element: 100 + 10 + 20 + 30 + 40 = 150.


25. What will the following code return?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.map(x => x + 2);

console.log(result);

a) [1, 2, 3, 4]
b) [3, 4, 5, 6]
c) [2, 4, 6, 8]
d) [1, 2, 3]

Answer: b) [3, 4, 5, 6]
Explanation: The map() method adds 2 to each element of the array, resulting in [3, 4, 5, 6].


I'll continue in a subsequent response for the rest of the 100 MCQs.

4o mini

You said:

y

ChatGPT said:

ChatGPT

Here are the remaining 75 multiple-choice questions (MCQs) on advanced array methods in JavaScript with explanations:


26. What does the splice() method do in JavaScript?

a) Removes elements from an array
b) Adds elements to an array
c) Both removes and adds elements to an array
d) Creates a shallow copy of the array

Answer: c) Both removes and adds elements to an array
Explanation: The splice() method can remove, replace, or add elements in an array at a specific position.


27. Which of the following methods changes the original array?

a) map()
b) filter()
c) forEach()
d) splice()

Answer: d) splice()
Explanation: The splice() method modifies the original array by removing or adding elements.


28. Which method would you use to check if an array contains a specific value?

a) includes()
b) indexOf()
c) find()
d) some()

Answer: a) includes()
Explanation: The includes() method checks if an array contains a specific value and returns true or false.


29. Which of the following methods can be used to combine multiple arrays into one?

a) concat()
b) join()
c) map()
d) slice()

Answer: a) concat()
Explanation: The concat() method is used to combine two or more arrays into a single array.


30. What does the sort() method do in JavaScript?

a) Sorts an array in ascending order by default
b) Sorts an array in descending order by default
c) Sorts an array based on the given criteria
d) Sorts the elements in the order they appear

Answer: a) Sorts an array in ascending order by default
Explanation: The sort() method sorts the elements of an array in ascending order by default (as strings).


31. How does join() work in JavaScript?

a) Joins elements of an array into a string
b) Combines two arrays
c) Splits a string into an array
d) Adds an element at the end of the array

Answer: a) Joins elements of an array into a string
Explanation: The join() method joins all elements of an array into a single string, with an optional separator.


32. What does the shift() method do in JavaScript?

a) Adds an element at the beginning of an array
b) Removes the last element of an array
c) Removes the first element of an array
d) Sorts the array in ascending order

Answer: c) Removes the first element of an array
Explanation: The shift() method removes the first element of an array and returns it.


33. What does the unshift() method do in JavaScript?

a) Removes the first element of the array
b) Adds one or more elements to the beginning of an array
c) Removes the last element of the array
d) Adds one or more elements to the end of an array

Answer: b) Adds one or more elements to the beginning of an array
Explanation: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.


34. Which method is used to create a shallow copy of an array?

a) copy()
b) slice()
c) clone()
d) map()

Answer: b) slice()
Explanation: The slice() method creates a shallow copy of a portion of an array and returns it.


35. What will be the output of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let copy = arr.slice();

console.log(copy);

a) [1, 2, 3, 4]
b) undefined
c) [2, 3, 4]
d) []

Answer: a) [1, 2, 3, 4]
Explanation: The slice() method creates a shallow copy of the array, so copy will be [1, 2, 3, 4].


36. Which method would you use to extract a portion of an array without modifying the original array?

a) splice()
b) slice()
c) shift()
d) pop()

Answer: b) slice()
Explanation: The slice() method extracts a portion of an array without modifying the original array.


37. How does the reverse() method work in JavaScript?

a) Reverses the order of elements in an array
b) Sorts the array in reverse order
c) Reverses the string values inside an array
d) Reverses the values based on a condition

Answer: a) Reverses the order of elements in an array
Explanation: The reverse() method reverses the order of elements in an array, modifying the original array.


38. Which of the following methods removes the last element from an array?

a) shift()
b) pop()
c) unshift()
d) splice()

Answer: b) pop()
Explanation: The pop() method removes the last element from an array and returns it.


39. Which of the following is true about the forEach() method?

a) It creates a new array
b) It returns a boolean value
c) It executes a provided function for each array element
d) It modifies the original array

Answer: c) It executes a provided function for each array element
Explanation: The forEach() method calls a function for each element in the array but does not return anything.


40. Which array method would you use to check the index of a specific value in an array?

a) indexOf()
b) findIndex()
c) includes()
d) map()

Answer: a) indexOf()
Explanation: The indexOf() method returns the first index of the specified value or -1 if not found.


41. What is the return value of find() when no elements match the condition?

a) null
b) undefined
c) false
d) 0

Answer: b) undefined
Explanation: The find() method returns undefined if no element in the array satisfies the given condition.


42. What is the purpose of the concat() method?

a) To remove elements from an array
b) To add new elements to an array
c) To join two or more arrays
d) To modify the existing array

Answer: c) To join two or more arrays
Explanation: The concat() method is used to merge two or more arrays into one array.


43. What will the following code return?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.indexOf(3);

console.log(result);

a) 2
b) 3
c) 1
d) undefined

Answer: a) 2
Explanation: The indexOf() method returns the index of the first occurrence of 3, which is 2.


44. Which method is used to sort an array in descending order?

a) reverse()
b) sort()
c) sort((a, b) => b - a)
d) reverse().sort()

Answer: c) sort((a, b) => b - a)
Explanation: The sort() method can be used with a custom comparison function to sort in descending order.


45. What will the following code return?

javascript

Copy code

let arr = [5, 4, 3, 2, 1];

let sortedArr = arr.sort((a, b) => b - a);

console.log(sortedArr);

a) [1, 2, 3, 4, 5]
b) [5, 4, 3, 2, 1]
c) [5, 3, 2, 1, 4]
d) [4, 5, 1, 3, 2]

Answer: b) [5, 4, 3, 2, 1]
Explanation: The sort() method sorts the array in descending order based on the custom comparator (a, b) => b - a.


46. What will be the output of the following code?

javascript

Copy code

let arr = ['apple', 'banana', 'cherry'];

let result = arr.join('-');

console.log(result);

a) apple banana cherry
b) apple,banana,cherry
c) apple-banana-cherry
d) undefined

Answer: c) apple-banana-cherry
Explanation: The join() method joins the array elements into a string with the specified separator ('-' in this case).


47. What is the time complexity of the map() method?

a) O(1)
b) O(n)
c) O(n^2)
d) O(log n)

Answer: b) O(n)
Explanation: The map() method iterates through all elements in the array, resulting in a time complexity of O(n), where n is the number of elements.


48. What is the some() method used for?

a) To check if all elements in the array pass the test
b) To check if at least one element passes the test
c) To modify the elements of an array
d) To return an array of modified elements

Answer: b) To check if at least one element passes the test
Explanation: The some() method tests if at least one element in the array satisfies the provided condition and returns true or false.


I'll continue with the remaining questions in another response.

4o mini

You said:

y

ChatGPT said:

ChatGPT

Here are more multiple-choice questions (MCQs) with explanations:


49. Which of the following methods is used to transform an array into a single value?

a) map()
b) reduce()
c) filter()
d) forEach()

Answer: b) reduce()
Explanation: The reduce() method processes each element of the array and accumulates the result into a single value (e.g., sum, product).


50. What will the following code return?

javascript

Copy code

let arr = [10, 20, 30, 40];

let result = arr.reduce((acc, curr) => acc + curr, 0);

console.log(result);

a) 100
b) 30
c) 60
d) undefined

Answer: a) 100
Explanation: The reduce() method sums the values in the array, starting from the initial value 0. So, 10 + 20 + 30 + 40 = 100.


51. Which array method is used to test if all elements in the array satisfy a given condition?

a) every()
b) some()
c) find()
d) forEach()

Answer: a) every()
Explanation: The every() method tests whether all elements in the array satisfy a given condition. It returns true if all elements pass the test.


52. What does the find() method return when no element matches the condition?

a) null
b) undefined
c) false
d) 0

Answer: b) undefined
Explanation: The find() method returns undefined if no element in the array satisfies the given condition.


53. What is the difference between some() and every() methods?

a) some() tests if at least one element satisfies the condition, while every() tests if all elements satisfy the condition.
b) some() tests if all elements satisfy the condition, while every() tests if at least one element satisfies the condition.
c) some() returns a new array, while every() returns a boolean.
d) some() does not work with arrays, while every() does.

Answer: a) some() tests if at least one element satisfies the condition, while every() tests if all elements satisfy the condition.
Explanation: The some() method checks if at least one element meets the condition, and the every() method checks if every element meets the condition.


54. How does the map() method work in JavaScript?

a) It filters out elements from an array
b) It transforms each element in an array
c) It modifies the original array
d) It removes all non-numeric values from the array

Answer: b) It transforms each element in an array
Explanation: The map() method creates a new array by applying a given function to each element of the original array without modifying the original array.


55. Which of the following methods creates a shallow copy of an array?

a) map()
b) forEach()
c) slice()
d) reduce()

Answer: c) slice()
Explanation: The slice() method creates a shallow copy of a portion or the entire array.


56. Which method is used to create a new array by including only the elements that satisfy a given condition?

a) map()
b) filter()
c) reduce()
d) forEach()

Answer: b) filter()
Explanation: The filter() method creates a new array with all elements that pass a test (provided by a function).


57. What will the following code return?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.map(x => x * 2);

console.log(result);

a) [2, 4, 6, 8]
b) [1, 2, 3, 4]
c) [2, 3, 4, 5]
d) [4, 6, 8, 10]

Answer: a) [2, 4, 6, 8]
Explanation: The map() method transforms each element of the array by multiplying it by 2.


58. What will be the output of the following code?

javascript

Copy code

let arr = [10, 20, 30, 40];

let result = arr.filter(x => x > 20);

console.log(result);

a) [10, 20]
b) [30, 40]
c) [20, 30, 40]
d) [10, 30, 40]

Answer: b) [30, 40]
Explanation: The filter() method creates a new array containing elements greater than 20 (30 and 40).


59. How does the forEach() method differ from map()?

a) forEach() returns a new array, while map() does not
b) forEach() executes a function for each element, but map() modifies the original array
c) forEach() does not return a value, while map() returns a new array
d) forEach() creates a shallow copy of the array, while map() modifies the original array

Answer: c) forEach() does not return a value, while map() returns a new array
Explanation: The forEach() method does not return anything, whereas map() creates and returns a new array with the transformed values.


60. What will the following code output?

javascript

Copy code

let arr = [10, 20, 30, 40];

let result = arr.reduce((acc, curr) => acc - curr, 100);

console.log(result);

a) 60
b) 50
c) 40
d) 30

Answer: b) 50
Explanation: The reduce() method starts with an initial value of 100 and subtracts each element in the array from it, resulting in 100 - 10 - 20 - 30 - 40 = 50.


61. Which method is used to check if an array contains a specific element?

a) contains()
b) find()
c) indexOf()
d) includes()

Answer: d) includes()
Explanation: The includes() method checks if a given element exists in the array and returns true or false.


62. Which method is used to combine two arrays?

a) join()
b) concat()
c) add()
d) merge()

Answer: b) concat()
Explanation: The concat() method combines two or more arrays into one new array.


63. What will the following code return?

javascript

Copy code

let arr = [1, 2, 3];

let result = arr.reduce((acc, curr) => acc * curr, 1);

console.log(result);

a) 6
b) 3
c) 0
d) 1

Answer: a) 6
Explanation: The reduce() method multiplies all elements in the array starting with an initial value of 1, resulting in 1 * 2 * 3 = 6.


64. What is the output of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.find(x => x > 2);

console.log(result);

a) 1
b) 2
c) 3
d) undefined

Answer: c) 3
Explanation: The find() method returns the first element that satisfies the condition, in this case, the first element greater than 2, which is 3.


65. What does the sort() method do by default?

a) Sorts an array in ascending order
b) Sorts an array in descending order
c) Sorts the array as per the function passed
d) Does not sort the array

Answer: a) Sorts an array in ascending order
Explanation: The sort() method, by default, sorts the array elements in ascending order as strings.


66. What is the time complexity of map()?

a) O(n)
b) O(n^2)
c) O(1)
d) O(log n)

Answer: a) O(n)
Explanation: The map() method iterates through the array and applies a transformation function to each element, so its time complexity is O(n).


I'll continue with the remaining questions in another response!

4o mini

You said:

y

ChatGPT said:

ChatGPT

Here are the remaining MCQs continuing from the previous list:


67. What is the result of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.some(x => x > 3);

console.log(result);

a) false
b) true
c) undefined
d) NaN

Answer: b) true
Explanation: The some() method checks if at least one element satisfies the condition (x > 3). Since 4 satisfies the condition, it returns true.


68. Which method is used to iterate over an array and call a provided function once for each array element?

a) forEach()
b) map()
c) reduce()
d) filter()

Answer: a) forEach()
Explanation: The forEach() method executes a provided function once for each array element, without returning a new array.


69. What will be the output of the following code?

javascript

Copy code

let arr = [1, 2, 3];

let result = arr.map(x => x ** 2);

console.log(result);

a) [1, 2, 3]
b) [1, 4, 9]
c) [2, 4, 6]
d) [1, 2, 3, 4]

Answer: b) [1, 4, 9]
Explanation: The map() method transforms each element of the array by squaring them (x ** 2), resulting in [1, 4, 9].


70. What is the time complexity of the forEach() method?

a) O(n)
b) O(log n)
c) O(1)
d) O(n^2)

Answer: a) O(n)
Explanation: The forEach() method processes each element of the array one by one, resulting in a time complexity of O(n), where n is the number of elements in the array.


71. What will the following code output?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.every(x => x > 0);

console.log(result);

a) false
b) true
c) undefined
d) NaN

Answer: b) true
Explanation: The every() method tests if every element satisfies the condition (x > 0). Since all elements in the array are greater than 0, it returns true.


72. How does the find() method behave if multiple elements match the condition?

a) It returns the last matching element.
b) It returns the first matching element.
c) It throws an error.
d) It returns all matching elements.

Answer: b) It returns the first matching element.
Explanation: The find() method returns the first element that satisfies the provided condition, even if there are multiple matches.


73. What will be the output of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.filter(x => x < 3);

console.log(result);

a) [1, 2, 3]
b) [1, 2]
c) [3, 4]
d) [4]

Answer: b) [1, 2]
Explanation: The filter() method creates a new array with elements less than 3, resulting in [1, 2].


74. Which method will return a new array containing only the elements that satisfy a given condition?

a) map()
b) filter()
c) some()
d) forEach()

Answer: b) filter()
Explanation: The filter() method creates a new array with all the elements that pass a test (condition).


75. Which of the following methods can be used to add elements to the beginning of an array?

a) push()
b) pop()
c) unshift()
d) shift()

Answer: c) unshift()
Explanation: The unshift() method adds one or more elements to the beginning of an array, changing its length.


76. What will be the output of the following code?

javascript

Copy code

let arr = [5, 10, 15, 20];

let result = arr.map(x => x / 5);

console.log(result);

a) [1, 2, 3, 4]
b) [5, 10, 15, 20]
c) [0.2, 0.4, 0.6, 0.8]
d) [0, 1, 2, 3]

Answer: a) [1, 2, 3, 4]
Explanation: The map() method divides each element by 5, resulting in [1, 2, 3, 4].


77. Which method is used to check whether at least one element in an array satisfies a condition?

a) some()
b) every()
c) map()
d) reduce()

Answer: a) some()
Explanation: The some() method checks if at least one element satisfies the condition. It returns true if any one element passes the condition.


78. What will the following code return?

javascript

Copy code

let arr = [10, 20, 30, 40];

let result = arr.reduce((acc, curr) => acc * curr, 1);

console.log(result);

a) 24000
b) 100
c) 400
d) 30

Answer: a) 24000
Explanation: The reduce() method multiplies all elements starting with an initial value of 1, so 1 * 10 * 20 * 30 * 40 = 24000.


79. How can you modify the values in an array without changing the original array?

a) Use map()
b) Use filter()
c) Use reduce()
d) Use forEach()

Answer: a) Use map()
Explanation: The map() method transforms the elements of an array and returns a new array, without modifying the original array.


80. What will be the output of the following code?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.reduce((acc, curr) => acc + curr, 10);

console.log(result);

a) 20
b) 30
c) 40
d) 50

Answer: b) 30
Explanation: The reduce() method starts with an initial value of 10, then adds each element in the array: 10 + 1 + 2 + 3 + 4 = 30.


81. Which of the following methods does NOT modify the original array?

a) splice()
b) sort()
c) map()
d) push()

Answer: c) map()
Explanation: The map() method creates a new array and does not modify the original array.


82. Which method can be used to create a string from the elements of an array?

a) join()
b) concat()
c) reduce()
d) split()

Answer: a) join()
Explanation: The join() method combines all elements of the array into a single string.


83. What will the following code return?

javascript

Copy code

let arr = [10, 20, 30, 40];

let result = arr.find(x => x === 30);

console.log(result);

a) 20
b) 30
c) undefined
d) NaN

Answer: b) 30
Explanation: The find() method returns the first element that matches the condition (x === 30), which is 30.


84. What is the time complexity of filter()?

a) O(1)
b) O(n)
c) O(n^2)
d) O(log n)

Answer: b) O(n)
Explanation: The filter() method iterates through all elements of the array, resulting in a time complexity of O(n).


85. What will the following code output?

javascript

Copy code

let arr = [5, 10, 15, 20];

let result = arr.filter(x => x > 10);

console.log(result);

a) [5, 10]
b) [15, 20]
c) [10, 15, 20]
d) [5, 15, 20]

Answer: b) [15, 20]
Explanation: The filter() method creates a new array containing the elements greater than 10, resulting in [15, 20].


Let me know if you would like more questions!

4o mini

You said:

y

ChatGPT said:

ChatGPT

Here are more MCQs on the topics of advanced array methods:


86. What will the following code return?

javascript

Copy code

let arr = [5, 10, 15, 20];

let result = arr.reduce((acc, curr) => acc - curr, 0);

console.log(result);

a) -40
b) -30
c) 40
d) 30

Answer: b) -30
Explanation: The reduce() method starts with an initial value of 0. The calculation proceeds as 0 - 5 - 10 - 15 - 20, resulting in -30.


87. Which method allows you to apply a function to each element of an array and return a new array with the results?

a) forEach()
b) map()
c) filter()
d) every()

Answer: b) map()
Explanation: The map() method creates a new array by applying a function to each element of the array.


88. How would you find whether an element exists in an array using a callback function?

a) some()
b) find()
c) filter()
d) reduce()

Answer: a) some()
Explanation: The some() method checks if at least one element satisfies the provided condition and returns true if it does.


89. Which method would you use to create a string by concatenating all elements of an array, separated by a comma?

a) join()
b) concat()
c) reduce()
d) split()

Answer: a) join()
Explanation: The join() method joins all elements of the array into a single string with a specified separator, defaulting to a comma.


90. Which method would you use to transform each item in an array into a new value without modifying the original array?

a) forEach()
b) map()
c) filter()
d) reduce()

Answer: b) map()
Explanation: The map() method applies a function to each item of an array and returns a new array with transformed values.


91. What will be the output of the following code?

javascript

Copy code

let arr = [2, 4, 6];

let result = arr.reduce((acc, curr) => acc + curr, 1);

console.log(result);

a) 13
b) 12
c) 15
d) 14

Answer: a) 13
Explanation: The reduce() method starts with an initial value of 1, so the calculation proceeds as 1 + 2 + 4 + 6 = 13.


92. What does the every() method return when applied to an array where all elements satisfy a condition?

a) false
b) true
c) undefined
d) NaN

Answer: b) true
Explanation: The every() method returns true if all elements in the array satisfy the provided condition.


93. What is the return value of the find() method if no element matches the condition?

a) null
b) undefined
c) false
d) NaN

Answer: b) undefined
Explanation: If no element in the array satisfies the condition, the find() method returns undefined.


94. Which of the following methods would be used to apply a function to each element of an array, but it doesn't return a new array?

a) map()
b) reduce()
c) forEach()
d) filter()

Answer: c) forEach()
Explanation: The forEach() method executes a function for each element in the array but does not return a new array.


95. What is the output of the following code?

javascript

Copy code

let arr = [2, 4, 6, 8];

let result = arr.some(x => x > 6);

console.log(result);

a) false
b) true
c) undefined
d) NaN

Answer: b) true
Explanation: The some() method checks if at least one element satisfies the condition (x > 6). Since 8 is greater than 6, it returns true.


96. Which method is used to accumulate all the elements in an array into a single value?

a) map()
b) reduce()
c) filter()
d) some()

Answer: b) reduce()
Explanation: The reduce() method accumulates all elements in an array into a single value based on the provided callback function.


97. What will the following code output?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.filter(x => x > 2);

console.log(result);

a) [1, 2]
b) [3, 4]
c) [2, 3, 4]
d) [4]

Answer: b) [3, 4]
Explanation: The filter() method returns elements greater than 2, which are 3 and 4.


98. Which method would you use to check if all elements in an array pass a test?

a) forEach()
b) some()
c) map()
d) every()

Answer: d) every()
Explanation: The every() method checks if all elements in an array satisfy a condition.


99. What will the following code output?

javascript

Copy code

let arr = [1, 2, 3, 4];

let result = arr.map(x => x * 2).filter(x => x > 5);

console.log(result);

a) [2, 4, 6, 8]
b) [6, 8]
c) [1, 2, 3, 4]
d) [10, 12]

Answer: b) [6, 8]
Explanation: The map() method doubles each element, resulting in [2, 4, 6, 8], and then the filter() method keeps elements greater than 5, which are 6 and 8.


100. Which of the following methods is used to add one or more elements to the end of an array?

a) push()
b) shift()
c) unshift()
d) pop()

Answer: a) push()
Explanation: The push() method adds one or more elements to the end of an array, modifying the original array.



Post a Comment

0Comments

Post a Comment (0)