JavaScript Sets MCQ

1. What is a Set in JavaScript?

a) A collection of unique values
b) A collection of key-value pairs
c) A type of function
d) A type of loop

Answer:

a) A collection of unique values

Explanation:

In JavaScript, a Set is an object that allows you to store unique values of any type, whether primitive values or object references.

2. How do you create a Set in JavaScript?

a) let mySet = Set();
b) let mySet = new Set();
c) let mySet = createSet();
d) let mySet = {};

Answer:

b) let mySet = new Set();

Explanation:

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

3. Can a JavaScript Set contain duplicate values?

a) Yes
b) No
c) Only if they are objects
d) Only if they are primitives

Answer:

b) No

Explanation:

Sets in JavaScript are collections of unique values, so they cannot contain duplicates.

4. How can you add a value to a Set in JavaScript?

a) mySet.add(value);
b) mySet.push(value);
c) mySet.set(value);
d) mySet.insert(value);

Answer:

a) mySet.add(value);

Explanation:

The add method is used to add a new element with a specified value to a Set in JavaScript.

5. How can you remove a value from a Set in JavaScript?

a) mySet.remove(value);
b) mySet.delete(value);
c) mySet.clear(value);
d) mySet.pop(value);

Answer:

b) mySet.delete(value);

Explanation:

The delete method is used to remove a specified value from a Set.

6. What method is used to clear all values from a Set in JavaScript?

a) mySet.clear();
b) mySet.empty();
c) mySet.removeAll();
d) mySet.deleteAll();

Answer:

a) mySet.clear();

Explanation:

The clear method removes all elements from a Set, effectively clearing it.

7. How can you check if a value is present in a Set in JavaScript?

a) mySet.contains(value);
b) mySet.includes(value);
c) mySet.has(value);
d) mySet.exists(value);

Answer:

c) mySet.has(value);

Explanation:

The has method is used to check if a Set contains a specified element.

8. What does the size property of a Set in JavaScript represent?

a) The total memory size of the Set
b) The number of values in the Set
c) The maximum capacity of the Set
d) The number of unique types of values in the Set

Answer:

b) The number of values in the Set

Explanation:

The size property returns the number of values in a Set.

9. How can you iterate over the values in a Set in JavaScript?

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

Answer:

d) Both a and b

Explanation:

You can iterate over a Set using a for loop or the forEach method.

10. What type of values can you store in a Set in JavaScript?

a) Only strings and numbers
b) Only objects
c) Any type, including objects and primitive types
d) Only primitive types

Answer:

c) Any type, including objects and primitive types

Explanation:

JavaScript Sets can store any type of values, whether they are primitive values or object references.

11. Can a Set in JavaScript have a length property?

a) Yes
b) No
c) Only if it has more than one value
d) Only if it's converted to an array

Answer:

b) No

Explanation:

JavaScript Sets do not have a length property; instead, they have a size property.

12. What happens when you try to add a duplicate value to a Set in JavaScript?

a) The Set keeps both values
b) The Set replaces the old value with the new one
c) The Set ignores the duplicate value
d) An error is thrown

Answer:

c) The Set ignores the duplicate value

Explanation:

If you try to add a duplicate value to a Set, the Set simply ignores it since all values in a Set must be unique.

13. Is the order of elements preserved in a JavaScript Set?

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

Answer:

a) Yes, in the order of insertion

Explanation:

Elements in a Set are iterated in the order of their insertion.

14. How do you convert a Set to an Array in JavaScript?

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

Answer:

d) Both a and c

Explanation:

A Set can be converted to an Array using the Array.from method or the spread operator […mySet].

15. Can a JavaScript Set be nested within another Set?

a) Yes
b) No
c) Only if it contains primitive values
d) Only if it contains objects

Answer:

a) Yes

Explanation:

Sets in JavaScript can contain any type of values, including other Sets, allowing them to be nested.

Leave a Comment

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

Scroll to Top