Welcome to JavaScript Quiz on Arrays. This quiz aims to test your understanding of JavaScript Array and methods. Each question is followed by four options, and an explanation is provided for each correct answer to help you grasp the concept better. Let's dive in!
1. How do you create an empty array in JavaScript?
a)
let arr = new Array();
b)
let arr = [];
c)
let arr = {};
d)
let arr = null;
Answer:
b)
let arr = [];
Explanation:
The correct way to create an empty array in JavaScript is by using square brackets [].
2. What is the index of the first element in an array?
a) 0
b) 1
c) -1
d) 10
Answer:
a) 0
Explanation:
In JavaScript, arrays are zero-indexed, which means the first element is accessed using index 0.
3. Which method do you use to add elements to the end of an array?
a) add()
b) push()
c) append()
d) concat()
Answer:
b) push()
Explanation:
The push() method is used to add elements to the end of an array in JavaScript.
4. How do you remove the last element from an array?
a) removeLast()
b) deleteLast()
c) pop()
d) splice()
Answer:
c) pop()
Explanation:
The pop() method removes the last element from an array and returns that element.
5. How do you check if a variable is an array in JavaScript?
a) isArray(x)
b) x.isArray()
c) x instanceof Array
d) typeof Array
Answer:
a) isArray(x)
Explanation:
The 'isArray()' method is used to check if a variable is an array in JavaScript.
6. What will be the output of the following code snippet?
let fruits = ['apple', 'banana', 'orange'];
fruits.pop();
console.log(fruits.length);
a) 0
b) 1
c) 2
d) 3
Answer:
c) 2
Explanation:
The pop() method removes the last element from the array. After executing fruits.pop(), the array will have two elements left, 'apple' and 'banana'. The length property returns the number of elements in the array after the 'orange' element has been popped off which is 2.
7. How do you check if an element exists in an array?
a) Using the check() method
b) Using the search() method
c) Using the includes() method
d) Using the exists() method
Answer:
c) Using the includes() method
Explanation:
The includes() method is used to check if an element exists in an array. It returns true if the element is found and false otherwise.
8. What method do you use to join all elements of an array into a single string?
a) join()
b) concat()
c) merge()
d) combine()
Answer:
a) join()
Explanation:
The join() method is used to concatenate all the elements of an array into a single string using a specified separator.
9. How do you remove elements from an array, starting from a specific index?
a) removeFromIndex()
b) splice()
c) cut()
d) deleteFromIndex()
Answer:
b) splice()
Explanation:
The splice() method is used to remove elements from an array, starting from a specific index.
10. How do you find the index of a specific element in an array?
a) findIndex()
b) indexOf()
c) searchIndex()
d) getElementIndex()
Answer:
b) indexOf()
Explanation:
The indexOf() method is used to find the index of a specific element in an array. It returns the first index at which the element is found or -1 if it's not present.
11. What method do you use to create a new array by applying a function to each element in the existing array?
a) apply()
b) map()
c) transform()
d) forEach()
Answer:
b) map()
Explanation:
The map() method creates a new array by applying a callback function to each element of the original array.
12. What does the forEach() method do?
a) Iterates over the array and executes a function once for each element.
b) Splits the array into multiple smaller arrays.
c) Sorts the elements of the array in ascending order.
d) Reverses the order of the elements in the array.
Answer:
a) Iterates over the array and executes a function once for each element.
Explanation:
The forEach() method is used to execute a provided function once for each element in the array.
13. How can you sort the elements of an array in descending order?
a) array.sort(descending)
b) array.reverse()
c) array.sort().reverse()
d) array.sort((a, b) => b - a)
Answer:
d) array.sort((a, b) => b - a)
Explanation:
To sort the elements in descending order, use the sort() method with a compare function that returns b - a.
14. What method is used to remove elements from an array based on a condition and return the removed elements as a new array?
a) removeIf()
b) filter()
c) extract()
d) deleteIf()
Answer:
b) filter()
Explanation:
The filter() method creates a new array with all elements that pass the test implemented by the provided function and returns that new array.
15. What is the output of the following code?
var numbers = [1, 2, 3, 4, 5];
var sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum);
a) 15
b) 10
c) 0
d) 30
Answer:
a) 15
Explanation:
The reduce() method reduces the array to a single value by executing a reducer function on each element. In this case, it calculates the sum of all elements.
Conclusion:
Congratulations on completing the JavaScript Arrays Quiz! Array is an essential data structure in JavaScript, and mastering them is crucial for developing efficient and functional applications. Regular practice and exposure to real-world scenarios will strengthen your skills in working with arrays and other JavaScript concepts.
Keep exploring and building exciting projects to solidify your understanding! Happy coding!
Comments
Post a Comment
Leave Comment