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 Arrays Quiz

Quiz 12 20 questions

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.
In JavaScript, an array can be created using the:
Both the [] literals and the Array() constructor can be used to create an array in JavaScript. Hence, the correct choice is (C). For more info, refer to JavaScript Arrays Basics.
How to access the second element of the array arr = [3, 7, 10]?
Array indices start at 0. The first element is at index 0, the second is at index 1 and so on and so forth. To access the second element, we'll use bracket notation along with the index 1. This goes with choice (D).

For more information, refer to JavaScript Arrays Basics.
Arrays can have arrays as elements. True or false?
In JavaScript, arrays can definitely contain arrays and if this is the case, we have the concept of array dimensions into the game. More info on Array Dimensions.
What would arr.indexOf(0) and arr.indexOf(1) return respectively, if arr holds the array [1, 5, 8]?
indexOf() gives the index of a given value (if found) in the array.
Which property of array objects can be used to iterate over them?
The length property of an array gives the total number of elements in it, and could likewise be used to iterate over it using a for loop.
How to replace the second element in the following array with the value 8?
var arr = [1, 2, 3];
Array indexes start at 0. Hence the second element would be at index 1. For more info on the basics of the basics of arrays head over to Creating Arrays.
For a given array arr, the expressions arr[0] and arr.shift() both do the same thing. True or false?
arr[0] merely returns back the first element in arr, whereas arr.shift() removes the first element from the array and returns it. This goes with choice (C).
What does Array(10) return?
When Array() is called with a single integer argument, as in the expression above, a sparse array is created with that many number of empty slots. Hence, Array(10) returns an array with 10 empty slots, which goes with choice (B).

Keep in mind that, internally, these empty slots aren't represented using the value undefined, although what we get back when we inspect any one of them is undefined. For more details, refer to JavaScript Arrays Basics — The Array() constructor.
What is the value of passed in the following code?
var marks = [67, 60, 89, 90, 67, 42];

var passed = marks.every(function(m) {
    return m >= 50;
});
The every() array method returns true when the provided function evaluates to true for every element in the given array, and false otherwise. In the code above, the given function doesn't evaluate to true for the element 42, likewise marks.every() returns false. This goes with choice (C). For more details, refer to JavaScript Array Methods — every().
Can we check for an array using the typeof operator? If not, then why?
We can't rely on typeof to check for arrays in JavaScript because it returns 'object' for all non-callable objects, including arrays. That is, there is no way to distinguish between an array and any other non-callable object via typeof.

Hence, the correct choice is (C). For more details, refer to JavaScript Arrays Basics — Checking for arrays for more details.
What does the following code log?
var arr = [1, 66, 20];
arr.length = 0;

console.log(arr[0]);
Setting the length property of an array to a given value constricts or expands the array to that very length. In the code above, the expression arr.length = 0 makes arr equal to []. Hence, arr[0] in the following line returns undefined which is ultimately logged in the console. This goes with choice (C).
What does the following code log?

There is no typo in the code below!

var nums = [10, 50, 88];

nums.filter(function(n) {
    return n > 10;
});

console.log(nums);
This question is fairly straightforward. In line 3, we call nums.filter() and then log nums. Since, filter() doesn't mutate the original array, nums remains as it is. Likewise, the log made is simply [10, 50, 88] — the same old value of arr. This goes with choice (D). For more details, refer to JavaScript Array Methods — filter().
Which of the following code snippets logs a sparse array?

Snippet 1:

var nums = [1, 2, 3];
delete nums[0];

console.log(nums);

Snippet 2:

var nums = [1, 2, 3];
nums.shift();

console.log(nums);
A sparse array is an array with empty slots in it. In snippet 1, after executing the statement delete nums[0], nums becomes sparse, i.e. the first slot in it becomes empty. This doesn't happen in snippet 2. Hence, the correct choice is (A). For more details, refer to JavaScript Arrays Basics — Sparse arrays.
What does the following code log?
console.log([1, 2] instanceof Array);
The instanceof operator checks whether the left operand is an instance of the right operand. In the code above, since [1, 2] is an array which is based on the Array class, true is logged. This goes with choice (A). For more details, refer to JavaScript Arrays Basics — Checking for arrays.
What does the following code log?
var nums = [1, 2, 3];
console.log(nums.map());
The map() array method requires an argument specifying the function to use to map each element in the given array to another element. Calling it without an argument throws an error. This is exactly what happens in the code above. Hence, the correct choice is (D). For more details, refer to JavaScript Array Methods — map().
For the code below, what does arr.splice(2, 0) return?
var arr = [1, 2, 3, 4, 5];
When the second argument to splice() is 0, that means that nothing is deleted in the given array. And so the return value of the method, which is an array holding all the deleted items, is simply []. Hence, arr.splice(2, 0) for the array arr above returns [], and this goes with choice (B). For more details, refer to JavaScript Array Methods — splice().
What does the following code log?
var nums = [1, 20, 3, 450, 53];

console.log(nums.sort());
When sort() is called without an argument, it sorts elements of the given array in lexicographical order and then returns it. The array nums above is actually already lexicographically sorted, hence it's returned back as it is and ultimately logged. This goes with choice (B). For more details, refer to JavaScript Array Sorting where we also explain the concept of lexicographical ordering.
Which of the following options correctly shows the array langs after the execution of the code below?
function compare(a, b) {
    if (a.year < b.year) return -1;
    if (a.year > b.year) return 1;

    if (a.name < b.name) return -1;
    if (a.name > b.name) return 1;
    return 0;
}

var langs = [
    {name: 'JavaScript', year: 1995},
    {name: 'Python', year: 1991},
    {name: 'Java', year: 1995},
    {name: 'C++', year: 1989}
];

console.log(langs.sort(compare));
The code above simply sorts langs, first based on the value of the property year of each object, and then based on the property name. That is, when multiple objects have the same year then they are sorted amongst themselves based on the value of the name property. The correct ordering is shown in choice (C). For more details on sorting arrays, refer to JavaScript Array Sorting.
What does the following code log?
var nums = [1, 3, 2];

console.log(nums.sort().reverse());
In the code above, nums.sort() returns the array [1, 2, 3], then the reverse() method called on this value returns [3, 2, 1]. So, the log made is [3, 2, 1] and this goes with choice (B). For more details on sorting arrays, refer to JavaScript Array Sorting.
The array arr shown below is dense. True or false?
var arr = [1, 2, 3];
An array is said to be dense when it doesn't have any empty slots in it. In the code above, arr doesn't have any empty slots, likewise it is a dense array. The given statement is therefore true, which goes with choice (A). For more details, refer to JavaScript Arrays Basics.