JavaScript Arrays MCQ

1. What is the purpose of an array in JavaScript?

a) To store multiple functions
b) To organize code into reusable blocks
c) To store multiple values in a single variable
d) To execute a sequence of operations

Answer:

c) To store multiple values in a single variable

Explanation:

Arrays in JavaScript are special variables that can hold more than one value, allowing for efficient storage and management of multiple values.

2. How do you create an array in JavaScript using an array literal?

a) var array_name = (item1, item2, …)
b) var array_name = [item1, item2, …]
c) var array_name = {item1, item2, …}
d) var array_name = new Array(item1, item2, …)

Answer:

b) var array_name = [item1, item2, …]

Explanation:

An array literal is created using square brackets [] and can include multiple items, separated by commas.

3. How do you access the first element of an array named 'fruits'?

a) fruits[1]
b) fruits.first
c) fruits[0]
d) fruits.at(1)

Answer:

c) fruits[0]

Explanation:

Array elements are accessed using their index number, starting with 0 for the first element.

4. How can you change the value of the first element in an array named 'cars'?

a) cars.first = 'new value'
b) cars[0] = 'new value'
c) cars.set(0, 'new value')
d) cars.change(0, 'new value')

Answer:

b) cars[0] = 'new value'

Explanation:

Array elements can be changed by accessing the element via its index and assigning a new value.

5. What does the toString() method do when called on an array?

a) Changes all elements to string type
b) Converts the array to a single string with elements separated by commas
c) Returns the type of the array as a string
d) Converts the first element of the array to a string

Answer:

b) Converts the array to a single string with elements separated by commas

Explanation:

The toString() method converts an array into a string of comma-separated array values.

6. What does the 'length' property of an array return?

a) The total memory size of the array
b) The number of elements in the array
c) The last index of the array
d) The length of the first element in the array

Answer:

b) The number of elements in the array

Explanation:

The length property returns the length of the array, which is the number of elements it contains.

7. How do you access the last element of an array named 'fruits'?

a) fruits.last
b) fruits[fruits.length]
c) fruits[fruits.length – 1]
d) fruits.end

Answer:

c) fruits[fruits.length – 1]

Explanation:

The last element of an array can be accessed by using the index fruits.length – 1.

8. Which is a common method to loop through all elements in an array named 'fruits'?

a) fruits.each(function(value) { … })
b) for(let i = 0; i < fruits.length; i++) { … }
c) fruits.loop(function(value) { … })
d) foreach(var fruit in fruits) { … }

Answer:

b) for(let i = 0; i < fruits.length; i++) { … }

Explanation:

A common method to loop through all elements in an array is using a for loop with the array's length property.

9. How do you add a new element to an array named 'fruits'?

a) fruits.add('new element')
b) fruits[fruits.length] = 'new element'
c) fruits.append('new element')
d) fruits.push('new element')

Answer:

d) fruits.push('new element')

Explanation:

The push() method is used to add a new element to the end of an array.

10. What is a key difference between arrays and objects in JavaScript?

a) Arrays use numbered indexes; objects use named indexes
b) Arrays can only store strings; objects can store any data type
c) Arrays are mutable; objects are immutable
d) Arrays are not a type of object; objects are a type of array

Answer:

a) Arrays use numbered indexes; objects use named indexes

Explanation:

A key difference between arrays and objects in JavaScript is that arrays use numbered indexes, whereas objects use named indexes.

11. How can you create an array using the JavaScript new Array() constructor?

a) var array_name = new Array(item1, item2, …)
b) var array_name = Array(item1, item2, …)
c) var array_name = newArray(item1, item2, …)
d) var array_name = Array.new(item1, item2, …)

Answer:

a) var array_name = new Array(item1, item2, …)

Explanation:

The new Array() constructor can be used to create an array, though using array literals is recommended for simplicity and readability.

12. How can you check if a variable is an array in JavaScript?

a) Using the typeof operator
b) Using the isArray() method
c) Checking if the variable has a length property
d) Using the instanceof operator

Answer:

b) Using the isArray() method

Explanation:

To check if a variable is an array, the Array.isArray() method is used, as the typeof operator will return 'object' for arrays.

13. What is the outcome of using the typeof operator on an array in JavaScript?

a) "array"
b) "object"
c) "list"
d) "collection"

Answer:

b) "object"

Explanation:

The typeof operator returns 'object' for arrays in JavaScript because arrays are a special type of object.

14. Why is it not recommended to use arrays with named indexes in JavaScript?

a) Named indexes convert the array into an object
b) Named indexes are not supported in JavaScript
c) Named indexes decrease array performance
d) JavaScript treats arrays with named indexes as associative arrays

Answer:

a) Named indexes convert the array into an object

Explanation:

Using named indexes in an array effectively converts it into an object, as JavaScript does not support arrays with named indexes.

15. What is a characteristic of associative arrays in JavaScript?

a) They are a special type of array with key-value pairs
b) They are the same as regular arrays
c) JavaScript converts them into objects
d) They are used for complex data structures

Answer:

c) JavaScript converts them into objects

Explanation:

While many languages support associative arrays (arrays with named indexes), JavaScript does not. Using named indexes in an array will convert it into an object.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top