Course: JavaScript

Progress (0%)

  1. Foundation

  2. Numbers

  3. Strings

  4. Conditions

  5. Loops

  6. Arrays

  7. Functions

  8. Objects

  9. Exceptions

  10. HTML DOM

  11. CSSOM

  12. Events

  13. Drag and Drop

  14. opt Touch Events

  15. Misc

  16. Project: Analog Clock

JavaScript Array MethodsQuiz

Quiz 11 20 questions

Prerequisites for the quiz

  1. JavaScript Array Methods
  2. JavaScript Arrays Basics
  3. All previous chapters

Are you ready?

20 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the following code log?
var a = [1, 2];
var b = a.concat(1, [3, 1]);

console.log(b);
For a given array a, a.concat() creates a copy of a and adds all the given arguments at the end of that copy. If an argument is an array itself, the elements of that array are added, instead of the array itself.

This means that in the code above, a.concat(1, [3, 1]) adds 1, 3 and 1 to a copy of a, to ultimately give the array [1, 2, 1, 3, 1]. For more information, refer to JavaScript Array Methods — concat().
What does the following code log?
var a = [0, 0];
a.concat([1, 1]);

console.log(a);
The concat() method does not mutate the array it is called on. Hence, in the code above, logging a would simply output the same array [0, 0] stored in the variable at the start of the script. This goes with choice (A). For more information, refer to JavaScript Array Methods — concat().
What does the following code log?
var arr = new Array(5);
arr.fill(0);

console.log(arr);
Given an array arr, arr.fill(x) fills it with a given value x upto the length of the array. In the code above, this means that the empty array arr is filled up with 0s to give [0, 0, 0, 0, 0]. This goes with choice (A). For more information, refer to JavaScript Array Methods — fill().
What does the following code log?
var arr = [];
arr.fill(10, 0, 5);

console.log(arr);
Calling fill() on an empty array has no consequence at all. The code above, therefore, simply logs the same value stored in arr at the start of the script i.e. []. This goes with choice (A). For more information, refer to JavaScript Array Methods — fill().
What does the following code log?
var arr = [1, 3, 5, 7];

console.log(arr.slice());
The slice() array method slices a subarray out of the calling array. When called without any arguments, it simply returns a copy of the whole array. Hence, the code above logs [1, 3, 5, 7] which goes with choice (B). For more information, refer to JavaScript Array Methods — slice().
For a given array arr, arr == arr.slice() returns true. True or false?
Calling arr.slice() on an array arr would return a copy of arr with a different address in memory. This means that arr == arr.slice() would return false since both the given arrays are different references. Hence, the correct choice is (B). For more information, refer to JavaScript Array Methods — slice().
What does the following code log?
var arr = [1, 4, 9];

console.log(arr.slice(1));
Calling slice() with a single integer argument slices the calling array from that very index upto the end of the array. Consequently, in the code above, arr.slice(1) slices out the portion [4, 9] from arr and returns it, which ultimately gets logged. Hence, the correct choice is (B). For more information, refer to JavaScript Array Methods — slice().
How to add an element x to the beginning of the array arr?

Note that arr might already be filled with data.

The correct way to add an element x to the beginning of an array arr is to call arr.unshift(x). Since this option does not exist in the list of options here, the correct choice is (D). For more information, refer to JavaScript Array Methods — unshift().
How to remove an element x from the beginning of an array arr?
Removing an element from the beginning of an array arr is as simple as calling arr.shift(). This goes with choice (A). For more information, refer to JavaScript Array Methods — shift().

As long as choice (B) is concerned, there is no such array method in JavaScript.
For a given array arr, arr.push(x) adds x to the end of arr. True or false?
arr.push(x) indeed works by adding the element x to the end of arr. Hence, the statement above is true, which goes with choice (A). For more information, refer to JavaScript Array Methods — push().
How to remove an element x from the end of an array arr?
Removing the last element from an array arr could be done in two ways: by calling arr.pop() or by reducing the length of arr by 1 i.e arr.length - 1. For more information, refer to JavaScript Array Methods — pop().
How to add an element x in the array arr at the position i, shifting any existing elements to the right?
The splice() method serves to add/remove elements from any given position i in an array, while shifting subsequent elements up or down by one position. The correct choice in this case is, therefore, (C). For more information, refer to JavaScript Array Methods — splice().

Choice (A) is wrong in this case since it replaces the first element of arr with the value x; it does not add a new element. And choice (B) shows a method that just doesn't exist in JavaScript.
How to remove an element x at the index i from an array arr, shifting all the subsequent elements down by one position?
The splice() method serves to add/remove elements from any given position i in an array, while shifting subsequent elements up or down by one position. Hence, the correct choice is (D). For more information, refer to JavaScript Array Methods — splice().
When called on an array arr, the splice() method mutates arr. True or false?
The splice() method mutates the calling array, which simply means that the statement above is true. This goes with choice (A). For more information, refer to JavaScript Array Methods — splice().
What does the following code log?
var arr = [1, 3, 5];
arr.reverse();

console.log(arr);
The reverse() method reverses an array, in-place. This means that the original array is modified after the method's invocation. Hence, in the code above, [5, 3, 1] is logged, and this goes with choice (B). For more information, refer to JavaScript Array Methods — reverse().
What does the following code log?
var arr = [1, 10, 100, 1000];

console.log(arr.indexOf(10));
arr.indexOf(val) returns the index of the first occurrence of val in arr. In the code above, arr.indexOf(10) would return 1, which goes with choice (C). For more information, refer to JavaScript Array Methods — indexOf().
What does the following code log?
function lengthGreaterThan3(str) {
    return str.length > 3;
}

var strList = ['cpp', 'cs', 'html', 'js', 'py'];

console.log(strList.some(lengthGreaterThan3));
console.log(strList.every(lengthGreaterThan3));
arr.some(callbackFn) returns true when at least one element exists in the calling array for which callbackFn returns true. Similarly, arr.every(callbackFn) returns true when for every element, in the calling array, callbackFn returns true.

In the code above, there is at least one string in strList that has length greater than 3, likewise the first call to some() returns true. However, because not all strings have such a length, the call to every() evaluates down to false. This goes with choice (B). For more information, refer to JavaScript Array Methods — some() and JavaScript Array Methods — every().
How to find the index of an element x's second occurrence in an array arr, given that there is always at least one occurrence of x?
To find the second occurrence of x in arr, we have to start from the position next to the position where its first occurrence is found. This simply translates to the expression given in choice (C). For more information, refer to JavaScript Array Methods — indexOf().
How to convert the array [1, 3, 5] into the string '1, 3, 5'?

Note that there is a space after each , character.

The separator here is the string ', ', and so to convert the array arr into a string representation we ought to use the join() method. For more information, refer to JavaScript Array Methods — join().
What does the following code log?
function square(n) { return n ** 2; }

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

console.log(arr.map(square));
console.log(arr);
The map() array method creates a new array and fills it with the return values of a callback function as invoked on each element in the calling array. Note that map() does not mutate the original array.

In the code above, this simply means that the call to map() returns [1, 4, 9, 16, 25], and then the next log returns the same old value stored in arr i.e. [1, 2, 3, 4, 5]. Hence, the correct choice is (C). For more information, refer to JavaScript Array Methods — map().