MCQs Of Day 8: Arrays in JavaScript
MCQs on Arrays in JavaScript
1. What is the correct syntax to create an array in JavaScript?
a) let arr = (1, 2, 3)
b) let arr = [1, 2, 3]
c) let arr = {1, 2, 3}
d) let arr = <1, 2, 3>
Answer: b) let arr = [1, 2, 3]
Explanation: Arrays in JavaScript are created using square brackets [].
2. Which method is used to add an element to the end of an array?
a) push()
b) pop()
c) shift()
d) unshift()
Answer: a) push()
Explanation: The push() method adds one or more elements to the end of an array.
3. Which of the following is used to remove the first element from an array?
a) pop()
b) push()
c) shift()
d) unshift()
Answer: c) shift()
Explanation: The shift() method removes the first element from an array.
4. Which method is used to remove the last element from an array?
a) shift()
b) pop()
c) splice()
d) slice()
Answer: b) pop()
Explanation: The pop() method removes the last element from an array.
5. Which of the following methods is used to join two arrays in JavaScript?
a) concat()
b) join()
c) push()
d) merge()
Answer: a) concat()
Explanation: The concat() method is used to merge two or more arrays into a new array.
6. What will let arr = [1, 2, 3]; arr.length; return?
a) 1
b) 3
c) undefined
d) NaN
Answer: b) 3
Explanation: The length property returns the number of elements in the array, which in this case is 3.
7. What will the following code print? let arr = [10, 20, 30]; console.log(arr[2]);
a) 20
b) 30
c) undefined
d) 10
Answer: b) 30
Explanation: Array indices are zero-based. So, arr[2] refers to the third element, which is 30.
8. Which method is used to add an element to the beginning of an array?
a) shift()
b) pop()
c) unshift()
d) splice()
Answer: c) unshift()
Explanation: The unshift() method adds one or more elements to the beginning of an array.
9. What does the splice() method do?
a) Joins two arrays
b) Adds/removes elements from an array
c) Removes a specific element from an array
d) Sorts the elements of an array
Answer: b) Adds/removes elements from an array
Explanation: The splice() method can add or remove elements from an array at a specified index.
10. Which of the following methods is used to create a shallow copy of an array?
a) slice()
b) splice()
c) concat()
d) push()
Answer: a) slice()
Explanation: The slice() method creates a shallow copy of a portion of an array without modifying the original array.
11. What is the index of the first element in an array?
a) 1
b) 0
c) -1
d) undefined
Answer: b) 0
Explanation: Arrays in JavaScript are zero-indexed, meaning the first element is at index 0.
12. What is the return value of the indexOf() method?
a) The element at the given index
b) The first index where the element is found
c) The last index where the element is found
d) undefined
Answer: b) The first index where the element is found
Explanation: The indexOf() method returns the first index of the specified element or -1 if not found.
13. What does the join() method do in arrays?
a) Combines all elements into a string
b) Merges two arrays
c) Adds an element to the array
d) Splits the array into separate arrays
Answer: a) Combines all elements into a string
Explanation: The join() method joins all elements of an array into a single string, separated by a specified separator.
14. Which of the following is used to iterate over the elements of an array?
a) forEach()
b) for()
c) map()
d) All of the above
Answer: d) All of the above
Explanation: forEach(), for(), and map() are all methods used to iterate over the elements of an array.
15. What does the map() method return?
a) A new array with the results of applying a function to every element
b) The index of an element in the array
c) The original array
d) undefined
Answer: a) A new array with the results of applying a function to every element
Explanation: The map() method creates a new array by applying a function to each element in the original array.
16. What does the filter() method do?
a) Returns a new array with elements that pass a test
b) Removes elements from an array
c) Joins two arrays
d) Reverses the array
Answer: a) Returns a new array with elements that pass a test
Explanation: The filter() method creates a new array with all elements that pass the given condition (callback function).
17. What is the purpose of the reduce() method in JavaScript?
a) To remove all elements of the array
b) To reduce an array to a single value
c) To join two arrays
d) To split an array into smaller arrays
Answer: b) To reduce an array to a single value
Explanation: The reduce() method applies a function to all elements in an array and reduces it to a single value (such as a sum or product).
18. Which method is used to check if an array contains a specific element?
a) has()
b) includes()
c) contains()
d) find()
Answer: b) includes()
Explanation: The includes() method checks if an array contains a specified element, returning true or false.
19. Which of the following can be used to sort an array of numbers in ascending order?
a) sort()
b) reverse()
c) push()
d) splice()
Answer: a) sort()
Explanation: The sort() method is used to sort the elements of an array in ascending order by default.
20. How would you find the length of an array arr?
a) arr.count
b) arr.size()
c) arr.length
d) arr.length()
Answer: c) arr.length
Explanation: The length property returns the number of elements in an array.
21. What will the following code return? let arr = [1, 2, 3]; arr[10] = 5; console.log(arr[10]);
a) undefined
b) 1
c) 5
d) NaN
Answer: c) 5
Explanation: The array arr now has an element at index 10, and that element is 5.
22. Which method is used to find the first element that satisfies a condition in an array?
a) filter()
b) some()
c) find()
d) every()
Answer: c) find()
Explanation: The find() method returns the first element in an array that satisfies a given condition.
23. What does the slice() method do in an array?
a) Adds elements to an array
b) Removes elements from an array
c) Returns a shallow copy of a portion of an array
d) Sorts the array
Answer: c) Returns a shallow copy of a portion of an array
Explanation: The slice() method returns a shallow copy of a portion of an array without modifying the original array.
24. Which of the following methods can be used to sort an array of strings in reverse order?
a) reverse()
b) sort()
c) sort().reverse()
d) unshift()
Answer: c) sort().reverse()
Explanation: The sort() method sorts an array, and reverse() can then be used to reverse the order of the sorted elements.
These MCQs test a variety of concepts related to arrays in JavaScript. The explanations provide clarity on each concept, helping students understand the fundamentals of working with arrays in JavaScript.
2/2
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are more MCQs related to arrays in JavaScript with explanations:
25. What will be the output of the following code?
javascript
Copy code
let arr = [10, 20, 30];
arr.splice(1, 1);
console.log(arr);
a) [10, 20, 30]
b) [10, 30]
c) [20, 30]
d) [10]
Answer: b) [10, 30]
Explanation: The splice() method is used to remove one element starting from index 1 (the second element), so the result is [10, 30].
26. What does the every() method do?
a) Tests whether all elements in the array pass a test
b) Tests whether any element in the array passes a test
c) Checks if all elements are unique
d) Finds the sum of all elements in the array
Answer: a) Tests whether all elements in the array pass a test
Explanation: The every() method tests whether all elements in the array satisfy a condition specified in a callback function. It returns true if all elements pass, otherwise false.
27. Which method is used to reverse the order of elements in an array?
a) reverse()
b) flip()
c) invert()
d) sort()
Answer: a) reverse()
Explanation: The reverse() method reverses the order of the elements in an array.
28. What is the value of arr[3] after the following code is executed?
javascript
Copy code
let arr = [1, 2, 3];
arr[3] = 4;
a) undefined
b) NaN
c) 4
d) 0
Answer: c) 4
Explanation: The value 4 is explicitly assigned to the index 3 in the array. So, arr[3] will return 4.
29. Which of the following array methods does not modify the original array?
a) sort()
b) reverse()
c) slice()
d) splice()
Answer: c) slice()
Explanation: The slice() method creates a shallow copy of a portion of an array and does not modify the original array.
30. How would you remove the first two elements from an array?
a) arr.splice(0, 2)
b) arr.shift(2)
c) arr.pop(2)
d) arr.slice(2)
Answer: a) arr.splice(0, 2)
Explanation: The splice() method is used to remove elements from an array. The first argument 0 specifies the starting index, and the second argument 2 specifies how many elements to remove.
31. What will the following code print?
javascript
Copy code
let arr = [10, 20, 30];
let newArr = arr.slice(1, 3);
console.log(newArr);
a) [20]
b) [20, 30]
c) [10, 20]
d) [1, 2]
Answer: b) [20, 30]
Explanation: The slice() method extracts elements starting from index 1 (inclusive) to index 3 (exclusive), resulting in [20, 30].
32. Which method returns the index of the first occurrence of a specified element in an array?
a) find()
b) indexOf()
c) includes()
d) search()
Answer: b) indexOf()
Explanation: The indexOf() method returns the index of the first occurrence of a specified element in an array, or -1 if the element is not found.
33. What is the result of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr.push(4, 5);
console.log(arr);
a) [1, 2, 3]
b) [1, 2, 3, 4, 5]
c) [4, 5, 1, 2, 3]
d) [5, 4, 1, 2, 3]
Answer: b) [1, 2, 3, 4, 5]
Explanation: The push() method adds 4 and 5 to the end of the array. The updated array is [1, 2, 3, 4, 5].
34. Which method is used to combine multiple arrays or values into one array?
a) join()
b) concat()
c) merge()
d) append()
Answer: b) concat()
Explanation: The concat() method is used to merge two or more arrays into a new array.
35. What will the following code print?
javascript
Copy code
let arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
a) [0, 1, 2, 3]
b) [1, 2, 3, 0]
c) [0, 2, 3]
d) [1, 0, 2, 3]
Answer: a) [0, 1, 2, 3]
Explanation: The unshift() method adds an element to the beginning of an array. The updated array becomes [0, 1, 2, 3].
36. What does the findIndex() method do?
a) Finds the first element in an array
b) Finds the index of the first element that satisfies a condition
c) Finds the last element in an array
d) Finds the index of the last element that satisfies a condition
Answer: b) Finds the index of the first element that satisfies a condition
Explanation: The findIndex() method returns the index of the first element in the array that satisfies a provided testing function.
37. How can you remove all elements from an array?
a) arr.removeAll()
b) arr.clear()
c) arr.length = 0
d) arr.pop()
Answer: c) arr.length = 0
Explanation: Setting the length property of an array to 0 removes all elements from the array.
38. What will be the result of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr[5] = 6;
console.log(arr);
a) [1, 2, 3, 6]
b) [1, 2, 3, undefined, undefined, 6]
c) [1, 2, 3, 5]
d) [1, 2, 3, null]
Answer: b) [1, 2, 3, undefined, undefined, 6]
Explanation: JavaScript arrays are sparse, meaning if you assign a value to an index beyond the current length, the intermediate elements are undefined. In this case, indices 3 and 4 are undefined.
39. What does the some() method do?
a) Tests if any element in the array passes the test
b) Tests if all elements in the array pass the test
c) Checks if an element exists in the array
d) Filters elements in the array
Answer: a) Tests if any element in the array passes the test
Explanation: The some() method tests whether at least one element in the array satisfies the provided testing function. It returns true if any element passes, otherwise false.
40. What does the flat() method do?
a) Flattens a multidimensional array into a one-dimensional array
b) Removes elements from an array
c) Adds elements to an array
d) Sorts the array
Answer: a) Flattens a multidimensional array into a one-dimensional array
Explanation: The flat() method is used to flatten a multidimensional array into a one-dimensional array.
More MCQs could be created based on array iteration methods, deep cloning, and other array-related concepts, such as destructuring and working with multidimensional arrays, to cover all the key aspects of array handling in JavaScript.
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are more MCQs with explanations on arrays in JavaScript:
41. What will be the output of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr.shift();
console.log(arr);
a) [1, 2, 3]
b) [2, 3]
c) [3]
d) [1, 2]
Answer: b) [2, 3]
Explanation: The shift() method removes the first element of the array, so the updated array becomes [2, 3].
42. Which method can be used to find the last element in an array?
a) last()
b) lastElement()
c) arr[arr.length - 1]
d) arr.pop()
Answer: c) arr[arr.length - 1]
Explanation: To access the last element of an array, we can use the arr[arr.length - 1] syntax, which gets the element at the last index.
43. Which of the following methods can be used to add an element at the beginning of an array?
a) arr.push()
b) arr.shift()
c) arr.unshift()
d) arr.pop()
Answer: c) arr.unshift()
Explanation: The unshift() method adds an element to the beginning of an array.
44. What will the following code return?
javascript
Copy code
let arr = [1, 2, 3];
arr.splice(1, 0, 4, 5);
console.log(arr);
a) [1, 4, 5, 2, 3]
b) [1, 2, 3, 4, 5]
c) [1, 4, 5]
d) [1, 2, 3]
Answer: a) [1, 4, 5, 2, 3]
Explanation: The splice() method is used here to insert 4 and 5 at index 1, shifting the elements to the right. The updated array is [1, 4, 5, 2, 3].
45. What will be the output of the following code?
javascript
Copy code
let arr = [10, 20, 30];
let newArr = arr.slice(1, 2);
console.log(newArr);
a) [20]
b) [10]
c) [30]
d) [20, 30]
Answer: a) [20]
Explanation: The slice() method returns a shallow copy of the array from index 1 (inclusive) to index 2 (exclusive). Thus, the result is [20].
46. What will the following code print?
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) [1, 4, 9, 16]
d) [1, 2, 3, 4, 5]
Answer: a) [2, 4, 6, 8]
Explanation: The map() method creates a new array by calling the provided function (which multiplies each element by 2) on every element in the original array.
47. Which method allows you to modify the contents of an array by removing and/or replacing elements?
a) push()
b) pop()
c) shift()
d) splice()
Answer: d) splice()
Explanation: The splice() method can remove, replace, or add elements in an array at any index, modifying the original array.
48. What will the following code print?
javascript
Copy code
let arr = [1, 2, 3];
let result = arr.concat([4, 5]);
console.log(result);
a) [1, 2, 3, [4, 5]]
b) [4, 5, 1, 2, 3]
c) [1, 2, 3, 4, 5]
d) [1, 2, 3]
Answer: c) [1, 2, 3, 4, 5]
Explanation: The concat() method is used to merge arrays. It combines the two arrays into one without modifying the original arrays.
49. Which of the following array methods will change the original array?
a) concat()
b) map()
c) filter()
d) reverse()
Answer: d) reverse()
Explanation: The reverse() method modifies the original array by reversing its elements in place.
50. What does the fill() method do?
a) Fills the array with random values
b) Fills the array with a specific value from a start index to an end index
c) Fills the array with null values
d) Fills the array with a series of values based on a given function
Answer: b) Fills the array with a specific value from a start index to an end index
Explanation: The fill() method fills all elements in an array from a start index to an end index with a given value.
51. Which method would you use to check if an array contains a specific element?
a) find()
b) includes()
c) indexOf()
d) search()
Answer: b) includes()
Explanation: The includes() method checks if a specified element is present in an array and returns true or false.
52. What does the reduce() method do?
a) Combines all elements in an array into a single value based on a given function
b) Removes elements from an array based on a condition
c) Maps each element of the array into a new array
d) Filters out elements that do not meet a condition
Answer: a) Combines all elements in an array into a single value based on a given function
Explanation: The reduce() method applies a function to each element in the array, reducing it to a single accumulated value.
53. Which of the following is true about multidimensional arrays in JavaScript?
a) They can only contain numbers
b) They are created using new Array()
c) They can contain arrays as elements
d) They do not support iteration
Answer: c) They can contain arrays as elements
Explanation: Multidimensional arrays are arrays that contain other arrays as elements. This allows them to store data in a matrix-like structure.
54. How do you create an array with 5 undefined elements?
a) let arr = new Array(5);
b) let arr = [undefined, undefined, undefined, undefined, undefined];
c) let arr = [5];
d) let arr = Array(5);
Answer: a) let arr = new Array(5);
Explanation: The new Array(5) creates an array with 5 empty slots, which are undefined by default.
55. What is the default value of an array element if the element is not explicitly assigned a value?
a) null
b) undefined
c) 0
d) NaN
Answer: b) undefined
Explanation: When an element is not explicitly assigned a value, JavaScript sets it to undefined.
56. What will the following code return?
javascript
Copy code
let arr = [1, 2, 3];
arr.push([4, 5]);
console.log(arr);
a) [1, 2, 3, 4, 5]
b) [1, 2, 3, [4, 5]]
c) [1, 2, 3, 4, 5, 6]
d) [[1, 2, 3], [4, 5]]
Answer: b) [1, 2, 3, [4, 5]]
Explanation: The push() method adds an array as an element at the end, resulting in a nested array [4, 5] within the main array.
These are more MCQs on arrays in JavaScript, covering array methods, manipulation, and iteration. They are designed to assess the understanding of arrays in JavaScript at the beginner level.
4o mini
You said:
more
ChatGPT said:
ChatGPT
Here are more MCQs with explanations on arrays in JavaScript:
57. What will be the result of the following code?
javascript
Copy code
let arr = [10, 20, 30, 40];
arr.pop();
console.log(arr);
a) [10, 20, 30]
b) [10, 20, 30, 40]
c) [20, 30, 40]
d) []
Answer: a) [10, 20, 30]
Explanation: The pop() method removes the last element from the array, so the array becomes [10, 20, 30].
58. Which array method would you use to remove the first element from an array?
a) shift()
b) pop()
c) unshift()
d) splice()
Answer: a) shift()
Explanation: The shift() method removes the first element from an array and shifts all other elements one position to the left.
59. What does the concat() method do in JavaScript?
a) Combines multiple arrays into one
b) Adds an element to the beginning of an array
c) Removes the last element of an array
d) Returns the index of a specified element
Answer: a) Combines multiple arrays into one
Explanation: The concat() method is used to merge two or more arrays into a new array without modifying the original arrays.
60. What will be the output of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
a) [1, 2, 3, 0]
b) [0, 1, 2, 3]
c) [1, 0, 2, 3]
d) [0, 2, 3]
Answer: b) [0, 1, 2, 3]
Explanation: The unshift() method adds the element 0 at the beginning of the array, resulting in [0, 1, 2, 3].
61. Which of the following methods will return a new array without modifying the original array?
a) pop()
b) shift()
c) slice()
d) reverse()
Answer: c) slice()
Explanation: The slice() method returns a shallow copy of a portion of an array without modifying the original array.
62. What is the purpose of the splice() method in JavaScript?
a) Adds elements to the end of an array
b) Adds or removes elements from any position in the array
c) Joins multiple arrays together
d) Reverses the order of elements in an array
Answer: b) Adds or removes elements from any position in the array
Explanation: The splice() method allows you to add, remove, or replace elements in an array at a specific index.
63. Which of the following statements correctly creates an empty array?
a) let arr = [];
b) let arr = new Array();
c) let arr = new Array(5);
d) All of the above
Answer: d) All of the above
Explanation: All three ways are valid methods to create an empty array in JavaScript.
64. What will be the result of this code?
javascript
Copy code
let arr = [1, 2, 3];
arr.splice(1, 1, 4, 5);
console.log(arr);
a) [1, 4, 5, 3]
b) [1, 4, 5]
c) [1, 2, 3, 4, 5]
d) [1, 4, 5, 2, 3]
Answer: a) [1, 4, 5, 3]
Explanation: The splice() method starts at index 1, removes 1 element, and adds 4 and 5. The updated array becomes [1, 4, 5, 3].
65. Which method returns the index of the first occurrence of a specified element in an array?
a) indexOf()
b) includes()
c) findIndex()
d) lastIndexOf()
Answer: a) indexOf()
Explanation: The indexOf() method returns the first index of a specified element in an array, or -1 if the element is not found.
66. What does the join() method do in JavaScript?
a) Combines elements of an array into a string
b) Joins two arrays
c) Adds a specified element to the array
d) Returns a new array of elements that match a condition
Answer: a) Combines elements of an array into a string
Explanation: The join() method joins all elements of an array into a single string, separated by a specified separator (or a comma by default).
67. What will be the output of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr.push(4);
arr.push(5);
console.log(arr);
a) [1, 2, 3, 4, 5]
b) [4, 5, 1, 2, 3]
c) [1, 2, 3]
d) [5, 4, 1, 2, 3]
Answer: a) [1, 2, 3, 4, 5]
Explanation: The push() method adds 4 and 5 to the end of the array, so the result is [1, 2, 3, 4, 5].
68. What is the correct way to iterate through all elements of an array using a for loop?
a)
javascript
Copy code
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
b)
javascript
Copy code
for (let i = 1; i < arr.length; i++) {
console.log(arr[i]);
}
c)
javascript
Copy code
for (let i = 0; i <= arr.length; i++) {
console.log(arr[i]);
}
d)
javascript
Copy code
for (let i = 1; i <= arr.length; i++) {
console.log(arr[i]);
}
Answer: a)
javascript
Copy code
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Explanation: A for loop starts at index 0 and goes up to arr.length - 1. The condition i < arr.length ensures that the loop does not exceed the array length.
69. Which method allows you to remove an element from the beginning of an array and returns it?
a) shift()
b) pop()
c) slice()
d) splice()
Answer: a) shift()
Explanation: The shift() method removes and returns the first element of an array, shifting the remaining elements to lower indexes.
70. What will be the output of the following code?
javascript
Copy code
let arr = [1, 2, 3, 4];
let newArr = arr.slice(1, 3);
console.log(newArr);
a) [2, 3]
b) [1, 2]
c) [3, 4]
d) [2, 3, 4]
Answer: a) [2, 3]
Explanation: The slice() method extracts elements starting from index 1 (inclusive) to index 3 (exclusive), resulting in [2, 3].
71. What does the map() method do in JavaScript?
a) Filters elements from an array based on a condition
b) Iterates over each element and applies a function to return a new array
c) Returns a string representation of an array
d) Joins all elements of an array into a string
Answer: b) Iterates over each element and applies a function to return a new array
Explanation: The map() method creates a new array by applying a function to each element of the original array.
72. Which of the following methods will change the original array?
a) slice()
b) concat()
c) sort()
d) map()
Answer: c) sort()
Explanation: The sort() method sorts the array in place, modifying the original array.
73. What is the output of the following code?
javascript
Copy code
let arr = [1, 2, 3, 4];
let result = arr.pop() + arr.pop();
console.log(result);
a) 5
b) 7
c) 8
d) 4
Answer: b) 7
Explanation: The pop() method removes the last element of the array. In this case, it removes 4 and 3, then adds them together to give 7.
74. What does the forEach() method do in JavaScript?
a) Returns a new array with the results of calling a function on each element
b) Executes a function once for each element in an array
c) Filters out elements that do not meet a condition
d) Sorts the array in ascending order
Answer: b) Executes a function once for each element in an array
Explanation: The forEach() method iterates over each element in the array and executes the given function for each element.
75. Which method can be used to find the last index of a specified element in an array?
a) lastIndexOf()
b) indexOf()
c) findIndex()
d) searchIndex()
Answer: a) lastIndexOf()
Explanation: The lastIndexOf() method returns the last index of a specified element in an array, or -1 if the element is not found.
These additional questions should provide a deeper understanding of array methods and concepts in JavaScript!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
Great! If you need any further explanations, more MCQs, or additional help, feel free to ask!
4o mini
You said:
yes
ChatGPT said:
ChatGPT
It seems like you're asking for more help! How can I assist you further? Would you like more MCQs, assignments, or another topic?
4o mini
You said:
no give more 25 from same above topic
ChatGPT said:
ChatGPT
Here are 25 additional multiple-choice questions (MCQs) on the topic of Arrays in JavaScript:
76. Which method would you use to add one or more elements to the end of an array in JavaScript?
a) pop()
b) shift()
c) push()
d) unshift()
Answer: c) push()
Explanation: The push() method adds one or more elements to the end of an array and returns the new length of the array.
77. Which of the following methods removes the first element from an array?
a) pop()
b) shift()
c) push()
d) unshift()
Answer: b) shift()
Explanation: The shift() method removes the first element from an array and returns that removed element.
78. What does the slice() method do?
a) Changes the array by removing elements
b) Returns a shallow copy of a portion of an array
c) Adds elements to the beginning of an array
d) Reverses the elements in the array
Answer: b) Returns a shallow copy of a portion of an array
Explanation: The slice() method returns a new array containing a portion of the original array, without modifying the original array.
79. Which method would you use to remove elements from an array, starting at a specific index?
a) pop()
b) shift()
c) splice()
d) slice()
Answer: c) splice()
Explanation: The splice() method changes an array by removing or replacing existing elements and/or adding new elements.
80. What is the result of the following code?
javascript
Copy code
let arr = [1, 2, 3];
arr.pop();
console.log(arr);
a) [1, 2]
b) [2, 3]
c) [1, 2, 3]
d) []
Answer: a) [1, 2]
Explanation: The pop() method removes the last element of the array, which in this case is 3, resulting in [1, 2].
81. Which method can be used to add an element to the beginning of an array?
a) pop()
b) push()
c) unshift()
d) shift()
Answer: c) unshift()
Explanation: The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
82. What is the result of the following code?
javascript
Copy code
let arr = [10, 20, 30];
arr.splice(1, 1, 15, 25);
console.log(arr);
a) [10, 20, 30]
b) [10, 15, 25, 30]
c) [10, 25, 30]
d) [15, 25, 30]
Answer: b) [10, 15, 25, 30]
Explanation: The splice() method removes one element at index 1 (which is 20) and replaces it with 15 and 25.
83. Which method checks if a certain element is present in an array?
a) indexOf()
b) includes()
c) lastIndexOf()
d) search()
Answer: b) includes()
Explanation: The includes() method checks if a specified element exists in an array and returns true if found, otherwise false.
84. Which of the following is the correct way to iterate through all elements of an array using a for loop?
a) for (let i = 0; i <= arr.length; i++)
b) for (let i = 0; i < arr.length; i++)
c) for (let i = 1; i < arr.length; i++)
d) for (let i = 0; i > arr.length; i++)
Answer: b) for (let i = 0; i < arr.length; i++)
Explanation: This is the correct syntax for iterating through an array using a for loop, as the loop should run from index 0 to the last index of the array (arr.length - 1).
85. What does the forEach() method return when used on an array?
a) A new array with transformed elements
b) The array itself
c) undefined
d) A boolean indicating whether the operation was successful
Answer: c) undefined
Explanation: The forEach() method performs a function on each element of the array, but it does not return anything. It returns undefined.
86. Which method can you use to create a new array by transforming elements of the original array?
a) map()
b) filter()
c) forEach()
d) reduce()
Answer: a) map()
Explanation: The map() method creates a new array by applying a function to each element of the original array.
87. What does the find() method do in an array?
a) Returns the index of the first element that meets a condition
b) Returns the first element that meets a condition
c) Modifies the array by removing elements
d) Returns an array of all elements that meet a condition
Answer: b) Returns the first element that meets a condition
Explanation: The find() method returns the first element in the array that satisfies the provided testing function.
88. What will be the output of the following code?
javascript
Copy code
let arr = [1, 2, 3, 4, 5];
let result = arr.slice(1, 3);
console.log(result);
a) [1, 2]
b) [2, 3]
c) [3, 4]
d) [1, 2, 3]
Answer: b) [2, 3]
Explanation: The slice() method returns a shallow copy of a portion of the array, from index 1 to 3 (not including 3), resulting in [2, 3].
89. Which method is used to remove the last element of an array and return that element?
a) push()
b) shift()
c) pop()
d) unshift()
Answer: c) pop()
Explanation: The pop() method removes and returns the last element of an array.
90. How do you check the length of an array in JavaScript?
a) array.size()
b) array.length()
c) array.length
d) array.count()
Answer: c) array.length
Explanation: The length property of an array returns the number of elements in the array.
91. Which method is used to combine two or more arrays into one?
a) concat()
b) merge()
c) join()
d) combine()
Answer: a) concat()
Explanation: The concat() method is used to merge two or more arrays into a single array without changing the original arrays.
92. What does the filter() method do?
a) Returns a new array with all elements that pass a test
b) Removes elements from the array
c) Returns a new array with the transformed elements
d) Loops through each element in the array
Answer: a) Returns a new array with all elements that pass a test
Explanation: The filter() method creates a new array with all elements that pass the provided test.
93. What is the result of the following code?
javascript
Copy code
let arr = [1, 2, 3, 4];
arr.unshift(0);
console.log(arr);
a) [0, 1, 2, 3, 4]
b) [1, 2, 3, 4, 0]
c) [4, 3, 2, 1]
d) [0, 1, 2]
Answer: a) [0, 1, 2, 3, 4]
Explanation: The unshift() method adds the element 0 to the beginning of the array.
94. What will be the output of the following code?
javascript
Copy code
let arr = [5, 10, 15];
arr[1] = 20;
console.log(arr);
a) [5, 10, 15]
b) [5, 20, 15]
c) [20, 10, 15]
d) [5, 20]
Answer: b) [5, 20, 15]
Explanation: The element at index 1 is changed from 10 to 20, resulting in [5, 20, 15].
95. Which method can you use to combine all elements of an array into a single string?
a) join()
b) toString()
c) concat()
d) combine()
Answer: a) join()
Explanation: The join() method combines all elements of an array into a string, with an optional separator.
96. Which of the following methods is used to reduce an array to a single value?
a) reduce()
b) filter()
c) map()
d) forEach()
Answer: a) reduce()
Explanation: The reduce() method applies a function to each element of the array to reduce it to a single value.
97. What is the default value of arr.length for an empty array?
a) null
b) 0
c) undefined
d) false
Answer: b) 0
Explanation: For an empty array, the length property is 0.
98. Which of the following is the correct way to access the last element of an array?
a) arr[arr.length]
b) arr[arr.length - 1]
c) arr[0]
d) arr[1]
Answer: b) arr[arr.length - 1]
Explanation: To access the last element of an array, you use arr[arr.length - 1] because array indices are zero-based.
99. What happens if you try to access an array element with an index that is out of bounds?
a) The program throws an error
b) It returns undefined
c) It returns null
d) It throws a warning
Answer: b) It returns undefined
Explanation: If an array element is accessed with an index that is out of bounds, it returns undefined.
100. Which of the following methods is used to find the index of an element in an array?
a) indexOf()
b) find()
c) search()
d) location()
Answer: a) indexOf()
Explanation: The indexOf() method returns the index of the first occurrence of an element in an array, or -1 if the element is not found.
