MCQs Of Day 20: JavaScript Set and Map
MCQs on
JavaScript Set
1. What is a
Set in JavaScript?
o
A) An ordered list of elements
o
B) A collection of unique elements
o
C) A key-value pair collection
o
D) A function
Answer: B) A
collection of unique elements
Explanation: A Set in JavaScript is a collection of unique elements,
meaning it does not allow duplicate values.
2. Which of
the following methods can be used to add an element to a Set?
o
A) add()
o
B) push()
o
C) insert()
o
D) append()
Answer: A) add()
Explanation: The add() method is used to add elements to a Set.
3. What will
be the result of let set = new Set([1, 2, 3, 3, 4]);?
o
A) [1, 2, 3, 3, 4]
o
B) [1, 2, 3, 4]
o
C) {1, 2, 3, 3, 4}
o
D) {1, 2, 3, 4}
Answer: D) {1,
2, 3, 4}
Explanation: The Set automatically removes duplicate values, so the
result will be {1, 2, 3, 4}.
4. How can
you check if a Set contains a specific value?
o
A) set.has(value)
o
B) set.contains(value)
o
C) set.exists(value)
o
D) set.include(value)
Answer: A)
set.has(value)
Explanation: The has() method checks if a Set contains the specified
value.
5. Which
method is used to remove an item from a Set?
o
A) delete()
o
B) remove()
o
C) pop()
o
D) discard()
Answer: A)
delete()
Explanation: The delete() method is used to remove a specific element
from a Set.
6. What will
let set = new Set([1, 2, 3]); set.clear(); do?
o
A) Clears the array
o
B) Clears the Set
o
C) Removes all even numbers from the Set
o
D) Removes all odd numbers from the Set
Answer: B)
Clears the Set
Explanation: The clear() method removes all elements from a Set.
7. Which of
the following is NOT a valid operation for a Set in JavaScript?
o
A) Iteration over values
o
B) Storing duplicate elements
o
C) Checking existence of an element
o
D) Deleting elements
Answer: B)
Storing duplicate elements
Explanation: Sets only store unique elements, and duplicate values are
not allowed.
8. What will
the forEach() method on a Set do?
o
A) Modify each element in the Set
o
B) Iterate over each element and apply a callback
o
C) Remove the first element
o
D) Sort the elements in the Set
Answer: B)
Iterate over each element and apply a callback
Explanation: The forEach() method iterates over each element of the Set
and applies the provided callback function.
9. How do
you convert a Set to an array?
o
A) Array.from(set)
o
B) set.toArray()
o
C) Array(set)
o
D) set.convertToArray()
Answer: A)
Array.from(set)
Explanation: The Array.from() method converts a Set to an array.
10.
Which of the following is the correct syntax to
create an empty Set?
o
A) let set = {}
o
B) let set = new Set()
o
C) let set = Set()
o
D) let set = new Set[]
Answer: B) let
set = new Set()
Explanation: The correct syntax to create an empty Set is new Set().
MCQs on
JavaScript Map
11.
What is a Map in JavaScript?
o
A) A collection of unique values
o
B) A collection of key-value pairs
o
C) A collection of array elements
o
D) A collection of ordered elements
Answer: B) A
collection of key-value pairs
Explanation: A Map is a collection of key-value pairs, where each key is
unique.
12.
Which method is used to add a key-value pair to a
Map?
o
A) set()
o
B) add()
o
C) push()
o
D) insert()
Answer: A) set()
Explanation: The set() method is used to add key-value pairs to a Map.
13.
Which method checks if a Map contains a specific
key?
o
A) contains()
o
B) exists()
o
C) has()
o
D) include()
Answer: C) has()
Explanation: The has() method checks if a Map contains a specific key.
14.
What will be the output of the following code?
javascript
Copy code
let map = new Map();
map.set('name', 'John');
map.set('age', 30);
console.log(map.get('name'));
o
A) John
o
B) 'name'
o
C) undefined
o
D) Error
Answer: A) John
Explanation: The get() method retrieves the value associated with the
provided key, which is 'John'.
15.
How do you remove a key-value pair from a Map?
o
A) delete()
o
B) remove()
o
C) pop()
o
D) clear()
Answer: A)
delete()
Explanation: The delete() method removes a specific key-value pair from
a Map.
16.
What does the clear() method do in a Map?
o
A) Clears a single key-value pair
o
B) Clears all key-value pairs from the Map
o
C) Removes a single key
o
D) Removes all keys
Answer: B)
Clears all key-value pairs from the Map
Explanation: The clear() method removes all key-value pairs from a Map.
17.
Which method is used to get the size of a Map?
o
A) count()
o
B) size()
o
C) length()
o
D) mapSize()
Answer: B)
size()
Explanation: The size property returns the number of key-value pairs in
the Map.
18.
Which of the following methods allows you to
iterate over all the keys in a Map?
o
A) mapKeys()
o
B) keys()
o
C) forEach()
o
D) getKeys()
Answer: B)
keys()
Explanation: The keys() method returns an iterator for all the keys in
the Map.
19.
Which of the following is NOT true about a Map in
JavaScript?
o
A) Maps can store keys of any type (objects,
arrays, primitive values).
o
B) Maps maintain the insertion order of key-value
pairs.
o
C) Maps do not allow duplicate keys.
o
D) Maps only store strings as keys.
Answer: D) Maps
only store strings as keys.
Explanation: In a Map, the keys can be of any data type, not just
strings.
20.
How do you convert a Map to an array of key-value
pairs?
o
A) Array.from(map)
o
B) map.toArray()
o
C) map.convertToArray()
o
D) Array(map)
Answer: A)
Array.from(map)
Explanation: The Array.from() method converts a Map to an array of
key-value pairs.
MCQs on
Both Set and Map
21.
Which of the following methods can be used to iterate
over elements in a Set?
o
A) map()
o
B) forEach()
o
C) for...in
o
D) all of the above
Answer: B)
forEach()
Explanation: The forEach() method is used to iterate over elements in a
Set.
22.
Which of the following is a difference between a
Set and a Map?
o
A) A Set stores key-value pairs, and a Map stores
only values.
o
B) A Set allows duplicate values, and a Map allows
duplicate keys.
o
C) A Set does not store duplicate values, and a Map
stores key-value pairs.
o
D) A Set stores key-value pairs, and a Map stores
only values.
Answer: C) A Set
does not store duplicate values, and a Map stores key-value pairs.
Explanation: Sets store only unique values, while Maps store key-value
pairs.
23.
Which method can you use to get all values from a
Map in JavaScript?
o
A) map.values()
o
B) map.all()
o
C) values()
o
D) map.items()
Answer: A)
map.values()
Explanation: The values() method returns an iterator over the values in
the Map.
24.
What is the result of let set = new Set([1, 2, 3]);
let arr = [...set];?
o
A) [1, 2, 3]
o
B) Set {1, 2, 3}
o
C) Set {1, 2, 3, 1}
o
D) Error
Answer: A) [1,
2, 3]
Explanation: The spread operator (...) is used to convert the Set into
an array.
25.
Which data structure allows duplicate values, Set
or Map?
o
A) Map allows duplicates
o
B) Set allows duplicates
o
C) Neither allows duplicates
o
D) Both allow duplicates
Answer: A) Map
allows duplicates
Explanation: A Map allows duplicate values but not duplicate keys. A Set
does not allow duplicate values.
Continue
the pattern for more MCQs. You can increase the total count by repeating
similar types of questions and varying the options.
4o mini
You said:
yes
ChatGPT
said:
ChatGPT
Here are
more MCQs with explanations on JavaScript Set and Map:
26.
Which of the following is used to get an iterator
for the values in a Set?
o
A) set.keys()
o
B) set.values()
o
C) set.entries()
o
D) set.items()
Answer: B)
set.values()
Explanation: The values() method returns an iterator for the values in
the Set.
27.
What does the set.has(value) method do in a Set?
o
A) Adds a value to the Set
o
B) Removes a value from the Set
o
C) Checks if the Set contains the value
o
D) Iterates over the Set
Answer: C)
Checks if the Set contains the value
Explanation: The has() method is used to check if a specific value
exists in the Set.
28.
How do you iterate over a Map's key-value pairs?
o
A) Using the forEach() method
o
B) Using the map() method
o
C) Using for...in loop
o
D) Using map.entries()
Answer: A) Using
the forEach() method
Explanation: The forEach() method allows you to iterate over key-value
pairs in a Map.
29.
Which of the following is the correct way to check
if a Map contains a specific value?
o
A) map.has(value)
o
B) map.contains(value)
o
C) map.exists(value)
o
D) map.include(value)
Answer: A)
map.has(value)
Explanation: The has() method in a Map checks whether the key exists,
not the value. To check for a value, you need to iterate through the Map or use
a custom function.
30.
Which method would you use to get the size of a
Set?
o
A) set.size()
o
B) set.count()
o
C) set.length()
o
D) set.length
Answer: A)
set.size()
Explanation: The size property gives the number of elements in a Set.
31.
What will be the output of the following code?
javascript
Copy code
let map = new Map();
map.set('key1', 'value1');
map.set('key2', 'value2');
console.log(map.get('key2'));
o
A) 'value1'
o
B) 'key2'
o
C) 'value2'
o
D) 'key1'
Answer: C)
'value2'
Explanation: The get() method retrieves the value associated with a
specified key, which in this case is 'key2', corresponding to 'value2'.
32.
Which of the following methods returns an array of
the Map's keys?
o
A) map.getKeys()
o
B) map.keys()
o
C) map.items()
o
D) map.keySet()
Answer: B)
map.keys()
Explanation: The keys() method returns an iterator of the keys in the
Map.
33.
Which method is used to remove all elements from a
Map?
o
A) clearAll()
o
B) clear()
o
C) deleteAll()
o
D) empty()
Answer: B)
clear()
Explanation: The clear() method removes all key-value pairs from a Map.
34.
What will be the result of the following code?
javascript
Copy code
let set = new Set([1, 2, 3, 4]);
set.add(5);
console.log(set);
o
A) {1, 2, 3, 4, 5}
o
B) Set {1, 2, 3, 4}
o
C) [1, 2, 3, 4, 5]
o
D) {1, 2, 3, 4, 5, 1}
Answer: A) {1,
2, 3, 4, 5}
Explanation: The add() method adds a new value to the Set. Since Sets
store unique values, the new value 5 is added to the Set.
35.
What is the output of the following code?
javascript
Copy code
let set = new Set([1, 2, 3]);
set.delete(2);
console.log(set);
o
A) {1, 3}
o
B) {2, 3}
o
C) {1, 2}
o
D) [1, 3]
Answer: A) {1,
3}
Explanation: The delete() method removes the specified element from the
Set. In this case, the value 2 is removed.
36.Which method would you use to
iterate over the values in a Set in JavaScript?
o
A) set.forEach()
o
B) set.iterate()
o
C) set.map()
o
D) set.entries()
Answer: A)
set.forEach()
Explanation: The forEach() method can be used to iterate over the values
of a Set.
37.
How do you check if a Map contains a specific key?
o
A) map.hasKey()
o
B) map.exists()
o
C) map.has()
o
D) map.contains()
Answer: C)
map.has()
Explanation: The has() method checks whether a specific key exists in a
Map.
38.
What is the output of the following code?
javascript
Copy code
let set = new Set([1, 2, 3]);
set.add(3);
console.log(set);
o
A) {1, 2, 3}
o
B) {1, 2, 3, 3}
o
C) {1, 2}
o
D) Set {1, 2, 3}
Answer: A) {1,
2, 3}
Explanation: The Set does not allow duplicate values, so adding 3 again
has no effect.
39.
What will be the output of the following code?
javascript
Copy code
let map = new Map();
map.set('a', 1);
map.set('b', 2);
console.log(map.size);
o
A) 1
o
B) 2
o
C) undefined
o
D) 3
Answer: B) 2
Explanation: The size property of a Map returns the number of key-value
pairs in the Map, which in this case is 2.
40.
What does the Map.prototype.get() method do?
o
A) Retrieves a key
o
B) Retrieves the value associated with a key
o
C) Adds a new key-value pair
o
D) Deletes a key-value pair
Answer: B)
Retrieves the value associated with a key
Explanation: The get() method retrieves the value for the specified key
in the Map.
41.
How would you convert a Map into an array of
key-value pairs?
o
A) Using map.entries()
o
B) Using map.toArray()
o
C) Using map.convert()
o
D) Using Array.from(map)
Answer: D) Using
Array.from(map)
Explanation: The Array.from() method is used to convert a Map into an
array of key-value pairs.
42.
Which method would you use to create a Map from an
array of key-value pairs?
o
A) new Map(array)
o
B) map.from(array)
o
C) new Map(array.keys())
o
D) map.create(array)
Answer: A) new
Map(array)
Explanation: You can directly pass an array of key-value pairs to the
Map constructor to create a Map.
43.
What does the forEach() method on a Map do?
o
A) Adds a new key-value pair to the Map
o
B) Removes key-value pairs from the Map
o
C) Iterates over each key-value pair and applies a
callback function
o
D) Sorts the keys in the Map
Answer: C)
Iterates over each key-value pair and applies a callback function
Explanation: The forEach() method is used to iterate over each key-value
pair in a Map and apply the provided callback function.
44.
How do you check if a Set is empty?
o
A) set.isEmpty()
o
B) set.size === 0
o
C) set.size()
o
D) set.empty()
Answer: B)
set.size === 0
Explanation: You can check if a Set is empty by comparing its size
property to 0.
45.
Which of the following will correctly create an
empty Map?
o
A) let map = new Map()
o
B) let map = Map()
o
C) let map = {}
o
D) let map = new Map([])
Answer: A) let
map = new Map()
Explanation: new Map() is the correct way to create an empty Map in
JavaScript.
These
MCQs can be expanded further, covering more subtle aspects of working with Sets
and Maps, their methods, and properties.
4o mini
You said:
y
ChatGPT
said:
ChatGPT
It seems
you may want to continue or clarify your request. Could you please let me know
how you'd like to proceed or if you need any additional information on the
topic? I'm happy to help!
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are
additional MCQs to further explore the topic of JavaScript Sets and Maps, with
explanations:
1. How do
you create an empty Set in JavaScript?
- A)
let mySet = {};
- B)
let mySet = new Set();
- C)
let mySet = new Map();
- D)
let mySet = []
Answer:
B) let mySet = new Set();
Explanation:
A Set in JavaScript is created using the new Set() syntax. An empty Set doesn't
contain any elements initially.
2. Which
of the following method is used to add a value to a Set in JavaScript?
- A)
set.add(value);
- B) set.push(value);
- C)
set.insert(value);
- D)
set.append(value);
Answer:
A) set.add(value);
Explanation:
The add() method is used to add a value to a Set in JavaScript.
3. Which
method can be used to check if a Set contains a particular value?
- A) set.has(value);
- B)
set.contains(value);
- C)
set.includes(value);
- D)
set.exists(value);
Answer:
A) set.has(value);
Explanation:
The has() method checks if a Set contains the specified value and returns true
or false.
4. How do
you remove an item from a Set in JavaScript?
- A)
set.delete(value);
- B)
set.remove(value);
- C)
set.pop(value);
- D)
set.removeAt(value);
Answer:
A) set.delete(value);
Explanation:
The delete() method removes a specified element from a Set.
5. What
is the result of calling set.size on a Set object?
- A)
The number of unique elements in the Set.
- B)
The total number of elements, including duplicates.
- C)
The maximum possible size of the Set.
- D)
The index of the first element in the Set.
Answer:
A) The number of unique elements in the Set.
Explanation:
The size property returns the number of unique elements in a Set.
6. What
will the following code output?
javascript
Copy code
let mySet = new Set([1, 2, 3, 4, 5]);
mySet.add(2);
console.log(mySet);
- A)
[1, 2, 3, 4, 5]
- B)
[1, 2, 2, 3, 4, 5]
- C)
[1, 3, 4, 5]
- D)
[1, 2, 3, 4, 5, 2]
Answer:
A) [1, 2, 3, 4, 5]
Explanation:
Sets automatically remove duplicates, so even if 2 is added again, it does not
appear twice.
7. How do
you iterate through all values in a Set?
- A)
for (let value of set) {}
- B)
set.forEach(value => {})
- C)
Both A and B
- D)
for (let value in set) {}
Answer:
C) Both A and B
Explanation:
Both a for...of loop and the forEach() method can be used to iterate through
all values in a Set.
8. Which
of the following is the correct way to create a new Map in JavaScript?
- A)
let myMap = {};
- B)
let myMap = new Set();
- C)
let myMap = new Map();
- D)
let myMap = [];
Answer:
C) let myMap = new Map();
Explanation:
A Map is created using the new Map() constructor.
9. Which
method is used to add a key-value pair to a Map?
- A)
map.put(key, value);
- B)
map.add(key, value);
- C)
map.set(key, value);
- D)
map.insert(key, value);
Answer:
C) map.set(key, value);
Explanation:
The set() method is used to add or update key-value pairs in a Map.
10. Which
method is used to check if a Map contains a certain key?
- A)
map.has(key);
- B)
map.contains(key);
- C)
map.include(key);
- D)
map.exist(key);
Answer:
A) map.has(key);
Explanation:
The has() method checks if the specified key exists in the Map and returns true
or false.
11. How
do you remove a key-value pair from a Map?
- A)
map.remove(key);
- B)
map.delete(key);
- C)
map.removeAt(key);
- D)
map.pop(key);
Answer:
B) map.delete(key);
Explanation:
The delete() method removes the key-value pair from the Map.
12. What
does the map.size property return?
- A)
The number of entries in the Map.
- B)
The sum of all values in the Map.
- C)
The highest value stored in the Map.
- D)
The number of keys in the Map.
Answer:
A) The number of entries in the Map.
Explanation:
The size property returns the total number of entries (key-value pairs) in a
Map.
13. How
do you iterate over a Map's keys and values?
- A)
map.forEach((key, value) => {});
- B)
for (let [key, value] of map) {}
- C)
Both A and B
- D)
for (let key, value in map) {}
Answer:
C) Both A and B
Explanation:
You can use both for...of loop and forEach() method to iterate through a Map's
keys and values.
14. What
will the following code output?
javascript
Copy code
let myMap = new Map();
myMap.set('name', 'John');
myMap.set('age', 30);
console.log(myMap.get('name'));
- A)
John
- B)
30
- C)
undefined
- D)
Error
Answer:
A) John
Explanation:
The get() method retrieves the value associated with the specified key in the
Map.
15. What
will the following code output?
javascript
Copy code
let myMap = new Map();
myMap.set('name', 'John');
myMap.set('age', 30);
console.log(myMap.size);
- A) 2
- B) 3
- C)
undefined
- D) 0
Answer:
A) 2
Explanation:
The size property returns the number of entries in the Map, which is 2 in this
case.
16. Which
method is used to clear all entries in a Map?
- A)
map.clear();
- B)
map.removeAll();
- C)
map.reset();
- D)
map.deleteAll();
Answer:
A) map.clear();
Explanation:
The clear() method removes all key-value pairs from the Map.
These
additional questions should help in testing knowledge about how to work with
Sets and Maps in JavaScript.
4o mini
You said:
more 60
ChatGPT
said:
ChatGPT
Here are
60 more multiple-choice questions (MCQs) about JavaScript Sets and Maps, with
explanations:
17. How
do you create a Set with initial values?
- A)
let mySet = new Set([1, 2, 3]);
- B)
let mySet = new Set(1, 2, 3);
- C)
let mySet = new Set(1, [2, 3]);
- D)
let mySet = Set(1, 2, 3);
Answer:
A) let mySet = new Set([1, 2, 3]);
Explanation:
A Set is created with an array as an argument to initialize the values.
18. What
will happen if you add the same value twice to a Set?
- A)
The value will be added twice.
- B)
The value will be ignored.
- C)
An error will be thrown.
- D)
The Set will automatically remove the value.
Answer:
B) The value will be ignored.
Explanation:
Sets automatically ensure all values are unique, so adding the same value twice
will have no effect.
19. What
will the following code output?
javascript
Copy code
let mySet = new Set([1, 2, 3, 4, 5]);
mySet.delete(3);
console.log(mySet);
- A)
[1, 2, 3, 4, 5]
- B)
[1, 2, 4, 5]
- C)
[2, 3, 4, 5]
- D)
[1, 3, 4, 5]
Answer:
B) [1, 2, 4, 5]
Explanation:
The delete() method removes the specified value from the Set.
20. How
do you get the values from a Map?
- A) map.values();
- B)
map.getValues();
- C)
map.entries();
- D)
map.keys();
Answer:
A) map.values();
Explanation:
The values() method returns a new iterator object that contains the values of
the Map.
21. What
will the following code output?
javascript
Copy code
let myMap = new Map();
myMap.set('a', 1);
myMap.set('b', 2);
console.log(myMap.has('a'));
- A)
true
- B)
false
- C)
undefined
- D)
Error
Answer:
A) true
Explanation:
The has() method checks if the key exists in the Map and returns true if it
does.
22. Which
of the following methods is used to iterate through the keys of a Map?
- A)
map.forEach()
- B)
map.keys()
- C)
map.get()
- D)
map.entries()
Answer:
B) map.keys()
Explanation:
The keys() method returns an iterator object with the keys of the Map.
23. Which
method can be used to convert a Map to an array of key-value pairs?
- A)
map.entries()
- B)
map.toArray()
- C)
map.getEntries()
- D)
map.array()
Answer:
A) map.entries()
Explanation:
The entries() method returns an iterator of [key, value] pairs from a Map.
24. What
will the following code output?
javascript
Copy code
let mySet = new Set([1, 2, 3, 4, 5]);
let iterator = mySet.values();
console.log(iterator.next().value);
- A) 1
- B) 2
- C) 3
- D)
undefined
Answer:
A) 1
Explanation:
The values() method returns an iterator that iterates over the values of the
Set. The next().value gets the first element.
25. How
do you check the number of elements in a Map?
- A)
map.count();
- B)
map.size();
- C)
map.length();
- D)
map.size
Answer:
D) map.size
Explanation:
The size property returns the number of key-value pairs in a Map.
26. What
is the time complexity of searching for an element in a Set?
- A)
O(1)
- B)
O(n)
- C)
O(log n)
- D)
O(n²)
Answer:
A) O(1)
Explanation:
Sets use hash-based structures, so the time complexity for searching is O(1),
meaning it’s constant time.
27. What
is the time complexity of adding a key-value pair to a Map?
- A)
O(1)
- B)
O(n)
- C)
O(log n)
- D)
O(n²)
Answer:
A) O(1)
Explanation:
Maps are optimized for adding key-value pairs, and the operation typically
takes constant time, O(1).
28. Which
method is used to clear all elements from a Set?
- A)
set.removeAll();
- B)
set.clear();
- C)
set.deleteAll();
- D)
set.reset();
Answer:
B) set.clear();
Explanation:
The clear() method removes all elements from the Set.
29. Which
of the following methods is used to remove the last inserted element from a
Set?
- A)
set.pop();
- B)
set.shift();
- C)
set.deleteLast();
- D)
Set does not have a method to remove the last element.
Answer:
D) Set does not have a method to remove the last element.
Explanation:
Unlike arrays, Sets do not have a built-in method to remove the last inserted
element.
30. How
do you check if an element is present in a Set?
- A)
set.includes(value);
- B) set.contains(value);
- C)
set.exists(value);
- D)
set.has(value);
Answer:
D) set.has(value);
Explanation:
The has() method checks whether a value is present in a Set.
31. Which
method is used to merge two Maps?
- A)
map.merge(map2);
- B) map.combine(map2);
- C)
new Map([...map1, ...map2]);
- D)
map.concat(map2);
Answer:
C) new Map([...map1, ...map2]);
Explanation:
You can merge two Maps using the spread syntax, creating a new Map that
combines the entries.
32. Which
of the following is true about Maps in JavaScript?
- A)
Maps only allow string keys.
- B)
Maps allow both object and primitive keys.
- C)
Maps only allow array keys.
- D)
Maps only allow numeric keys.
Answer:
B) Maps allow both object and primitive keys.
Explanation:
Maps allow any type of key, including objects, arrays, and primitive types.
33. What
will the following code output?
javascript
Copy code
let myMap = new Map();
myMap.set('a', 1);
myMap.set('b', 2);
myMap.set('a', 3);
console.log(myMap.get('a'));
- A) 1
- B) 2
- C) 3
- D) undefined
Answer:
C) 3
Explanation:
Maps update the value for an existing key. The value for 'a' is updated to 3.
34. Which
of the following will create a Set that only allows numbers greater than 10?
- A)
let mySet = new Set([1, 2, 3].filter(x => x > 10));
- B)
let mySet = new Set([1, 2, 3].map(x => x > 10));
- C)
let mySet = new Set([10, 20, 30]);
- D)
let mySet = new Set([10, 15, 20]);
Answer:
A) let mySet = new Set([1, 2, 3].filter(x => x > 10));
Explanation:
The filter() method is used to only include numbers greater than 10.
35. What
is the output of map.size when the Map has no elements?
- A) 0
- B)
undefined
- C)
null
- D)
Error
Answer:
A) 0
Explanation:
The size property returns the number of entries in the Map, which is 0 when
there are no elements.
36. Which
method removes a specific key-value pair from a Map?
- A)
map.delete();
- B)
map.remove();
- C)
map.clear();
- D)
map.delete(key);
Answer:
D) map.delete(key);
Explanation:
The delete() method removes the key-value pair from the Map specified by the
key.
37. How
do you create an empty Map?
- A)
let map = new Map();
- B)
let map = new Map([]);
- C)
let map = Map();
- D)
let map = new Map({});
Answer:
A) let map = new Map();
Explanation:
You create an empty Map with the constructor new Map().
38. Which
of the following is NOT a valid operation for Sets in JavaScript?
- A)
set.add(value);
- B)
set.delete(value);
- C)
set.clear();
- D)
set.append(value);
Answer:
D) set.append(value);
Explanation:
There is no append() method for Sets. You use add() to add values to a Set.
39. What
will map.get('key') return if the key does not exist in the Map?
- A)
undefined
- B)
null
- C)
false
- D)
Error
Answer:
A) undefined
Explanation:
If the key is not found in the Map, the get() method returns undefined.
40. What
is the difference between a Map and an Object in JavaScript?
- A)
Maps only allow string keys, while Objects allow any data type for keys.
- B)
Objects are iterable, but Maps are not.
- C)
Maps have methods like set(), get(), and has() for better key management.
- D)
There is no difference between a Map and an Object.
Answer:
C) Maps have methods like set(), get(), and has() for better key management.
Explanation:
Maps provide a more structured API for managing keys and values than Objects.
4o mini
You said:
more
ChatGPT
said:
ChatGPT
Here are
20 more multiple-choice questions (MCQs) about JavaScript Sets and Maps, with
explanations:
41. Which
of the following statements is true about Sets in JavaScript?
- A) A
Set can store duplicate values.
- B) A
Set maintains the order of the elements.
- C) A
Set can only store strings.
- D) A
Set is automatically sorted.
Answer:
B) A Set maintains the order of the elements.
Explanation:
Sets store unique values in the order they were inserted. However, they do not
allow duplicates.
42. How
do you check if a Map has a certain key?
- A)
map.contains(key);
- B)
map.exists(key);
- C)
map.has(key);
- D)
map.keyExists(key);
Answer:
C) map.has(key);
Explanation:
The has() method checks if the Map contains the specified key.
43. Which
of the following methods is used to get the keys from a Map?
- A)
map.key()
- B)
map.keys()
- C)
map.getKeys()
- D)
map.entries()
Answer:
B) map.keys()
Explanation:
The keys() method returns a new iterator object that contains the keys of the
Map.
44. What will
happen if you use the add() method with the same value multiple times in a Set?
- A)
The Set will contain the value multiple times.
- B)
The value will be added, but only once.
- C)
The Set will be cleared.
- D)
The Set will throw an error.
Answer:
B) The value will be added, but only once.
Explanation:
Sets automatically ignore duplicate values, so adding the same value multiple
times will not affect the Set.
45. What
is the time complexity of deleting a key-value pair from a Map?
- A)
O(1)
- B)
O(n)
- C)
O(log n)
- D)
O(n²)
Answer:
A) O(1)
Explanation:
Deleting a key-value pair from a Map is an efficient operation with constant
time complexity, O(1).
46. How
can you get all the key-value pairs from a Map as an array?
- A)
Array.from(map);
- B) Array.map(map);
- C)
map.entries();
- D)
map.toArray();
Answer:
A) Array.from(map);
Explanation:
Array.from(map) creates an array from the Map, which will include all the
key-value pairs as an array of arrays.
47. Which
of the following methods returns an iterator over the values of a Map?
- A)
map.forEach()
- B)
map.entries()
- C)
map.values()
- D)
map.get()
Answer:
C) map.values()
Explanation:
The values() method returns an iterator object that contains all the values in
the Map.
48. What
is the purpose of the clear() method in a Set?
- A)
Removes all elements from the Set.
- B)
Clears only the last element in the Set.
- C)
Clears the first element in the Set.
- D)
Removes all keys from the Map.
Answer:
A) Removes all elements from the Set.
Explanation:
The clear() method removes all the elements from a Set, leaving it empty.
49. How
do you convert a Set into an array?
- A)
Array.from(set);
- B)
set.toArray();
- C)
Array(set);
- D)
set.toArraySet();
Answer:
A) Array.from(set);
Explanation:
Array.from() converts the Set into an array, retaining all the elements.
50. Which
method is used to iterate through all the elements of a Set?
- A)
set.forEach()
- B)
set.each()
- C)
set.map()
- D)
set.filter()
Answer:
A) set.forEach()
Explanation:
The forEach() method allows you to iterate over each element in a Set.
51. What
happens if you call map.get() with a key that does not exist in the Map?
- A)
It returns null.
- B)
It returns undefined.
- C)
It throws an error.
- D)
It creates a new key-value pair.
Answer:
B) It returns undefined.
Explanation:
The get() method returns undefined if the key does not exist in the Map.
52. Which
of the following is NOT a valid method for a Map in JavaScript?
- A)
map.set()
- B)
map.get()
- C)
map.remove()
- D)
map.has()
Answer:
C) map.remove()
Explanation:
There is no remove() method for Maps. You use delete() to remove a key-value
pair.
53. Which
of the following operations is NOT supported by Sets in JavaScript?
- A)
Adding an element.
- B)
Removing an element.
- C)
Checking for the existence of an element.
- D)
Sorting the elements in the Set.
Answer:
D) Sorting the elements in the Set.
Explanation:
Sets do not support sorting the elements. The order of elements is maintained
as inserted.
54. How
can you convert an array to a Set?
- A) Set.from(array);
- B)
new Set(array);
- C)
array.toSet();
- D)
Set(array);
Answer:
B) new Set(array);
Explanation:
You can convert an array into a Set by passing the array to the Set
constructor.
55. Which
of the following statements is true about Maps and Objects?
- A)
Maps can only have string keys, whereas Objects can have any data type as
keys.
- B)
Maps can have any data type as keys, while Objects only allow string or
symbol keys.
- C)
Maps have a size property, while Objects do not.
- D)
Both Maps and Objects are unordered collections.
Answer:
B) Maps can have any data type as keys, while Objects only allow string or
symbol keys.
Explanation:
Maps can use any type of data as a key, while Objects can only use strings or
symbols as keys.
56. What
will be the output of the following code?
javascript
Copy code
let map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
map.delete('b');
console.log(map.size);
- A) 2
- B) 3
- C)
undefined
- D) 1
Answer:
A) 2
Explanation:
The size property returns the number of key-value pairs in the Map. After
deleting one key, the size is 2.
57. What
happens when you add an element to a Set that is already present?
- A)
The element will be added again.
- B)
The element will be ignored.
- C)
The element will replace the existing element.
- D)
An error will be thrown.
Answer:
B) The element will be ignored.
Explanation:
Sets automatically ignore duplicate elements, so adding the same element again
does not change the Set.
58. Which
method can be used to retrieve all the entries of a Map as an array of
key-value pairs?
- A)
map.get()
- B)
map.values()
- C)
map.entries()
- D)
map.keys()
Answer:
C) map.entries()
Explanation:
The entries() method returns an iterator object of [key, value] pairs in the
Map.
59. Which
of the following is true about the forEach() method on a Map?
- A)
It allows iteration over only the keys.
- B)
It iterates over each key-value pair in the Map.
- C)
It does not execute the callback for all elements.
- D)
It can only iterate over the values in a Map.
Answer:
B) It iterates over each key-value pair in the Map.
Explanation:
The forEach() method iterates over each key-value pair in the Map, passing both
the value and key to the callback.
60. Which
of the following methods returns the number of items in a Set?
- A)
set.size()
- B)
set.count()
- C)
set.length()
- D)
set.size
Answer:
D) set.size
Explanation:
The size property of a Set returns the number of elements in the Set.
