JavaScript Maps MCQ

1. What is a Map in JavaScript?

a) A function to map one value to another
b) A collection of elements indexed by a key
c) A data structure for storing geographical data
d) A method to iterate over arrays

Answer:

b) A collection of elements indexed by a key

Explanation:

In JavaScript, a Map is a collection of elements where each element is stored as a key-value pair. Maps allow efficient retrieval, addition, and deletion of elements based on their keys.

2. How do you create a new Map in JavaScript?

a) let myMap = new Map();
b) let myMap = create Map();
c) let myMap = Map();
d) let myMap = {};

Answer:

a) let myMap = new Map();

Explanation:

A Map is created using the new Map() constructor.

3. How can you add key-value pairs to a Map in JavaScript?

a) myMap.add(key, value);
b) myMap.set(key, value);
c) myMap.insert(key, value);
d) myMap.push(key, value);

Answer:

b) myMap.set(key, value);

Explanation:

The set method is used to add key-value pairs to a Map.

4. What method do you use to retrieve a value from a Map in JavaScript?

a) myMap.get(key);
b) myMap.retrieve(key);
c) myMap.pull(key);
d) myMap.find(key);

Answer:

a) myMap.get(key);

Explanation:

The get method is used to retrieve the value associated with a specific key from a Map.

5. How can you remove a key-value pair from a Map in JavaScript?

a) myMap.remove(key);
b) myMap.delete(key);
c) myMap.clear(key);
d) myMap.pop(key);

Answer:

b) myMap.delete(key);

Explanation:

The delete method is used to remove a key-value pair from a Map based on the key.

6. What does the size property of a Map object represent?

a) The total memory size of the Map
b) The number of key-value pairs in the Map
c) The maximum capacity of the Map
d) The size of the largest key in the Map

Answer:

b) The number of key-value pairs in the Map

Explanation:

The size property of a Map object returns the number of key-value pairs it contains.

7. Can a JavaScript Map contain functions as keys or values?

a) Yes, both keys and values
b) Only as values, not as keys
c) Only as keys, not as values
d) No, functions are not allowed

Answer:

a) Yes, both keys and values

Explanation:

In a Map, both keys and values can be of any data type, including functions.

8. How do you check if a Map contains a specific key in JavaScript?

a) myMap.contains(key);
b) myMap.includes(key);
c) myMap.has(key);
d) myMap.exists(key);

Answer:

c) myMap.has(key);

Explanation:

The has method is used to check if a Map contains a particular key.

9. What happens if you set a new value for an existing key in a Map?

a) The Map ignores the new value
b) The new value replaces the old value
c) An error is thrown
d) A duplicate key is created

Answer:

b) The new value replaces the old value

Explanation:

In a Map, if you set a new value for an existing key, the old value is replaced by the new value.

10. How can you iterate over the entries in a Map?

a) Using the forEach method
b) Using a for loop
c) Using the map method
d) Both a and b

Answer:

d) Both a and b

Explanation:

You can iterate over the entries in a Map using the forEach method or a for loop (e.g., for…of loop).

11. What is the purpose of the clear method in a Map?

a) To remove a specific key-value pair
b) To remove all key-value pairs
c) To check if the Map is empty
d) To reinitialize the Map

Answer:

b) To remove all key-value pairs

Explanation:

The clear method is used to remove all key-value pairs from a Map, effectively clearing it.

12. Can a JavaScript Map have non-string keys?

a) Yes, keys can be of any type
b) No, keys must be strings
c) Only numeric keys are allowed
d) Only object keys are allowed

Answer:

a) Yes, keys can be of any type

Explanation:

Unlike objects, where keys must be strings or symbols, Map keys can be of any type, including numbers, functions, and objects.

13. What is the result of calling myMap.get(nonExistentKey) in JavaScript?

a) An error is thrown
b) undefined
c) null
d) false

Answer:

b) undefined

Explanation:

If you use the get method to retrieve the value of a key that does not exist in the Map, it returns undefined.

14. Is the order of key-value pairs preserved in a JavaScript Map?

a) Yes, in the order of insertion
b) No, Maps are unordered
c) Only for string keys
d) Only for numeric keys

Answer:

a) Yes, in the order of insertion

Explanation:

Unlike objects, the order of key-value pairs in a Map is preserved in the order they were inserted.

15. How can you convert a Map to an Array in JavaScript?

a) Array.from(myMap)
b) myMap.toArray()
c) […myMap]
d) Both a and c

Answer:

d) Both a and c

Explanation:

A Map can be converted to an Array using Array.from(myMap) or the spread operator […myMap].

Leave a Comment

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

Scroll to Top