Map & Set — When a Plain Object Is Not Enough — Concept Check
Questions on Map & Set — When a Plain Object Is Not Enough. Map allows any value as a key and preserves insertion order; Set stores unique values. Both are the right answer more often than the object-as-dictionary habit suggests.
Questions in this quiz
7 questions on Map & Set — When a Plain Object Is Not Enough — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You need to store user sessions with unique identifiers and maintain insertion order. Which data structure should you use?
- ASet
- BPlain Object
- CMap
- DArray
2.A developer uses a plain object to store metadata with object keys. What issue might arise?
- APlain objects don't support iteration.
- BPlain objects can't store metadata.
- CKeys are forced to strings, causing potential conflicts.
- DObjects can't be keys in a plain object.
3.You have an array of user roles with duplicates and need to check for 'admin' quickly. What should you use?
- AMap
- BSet
- CPlain Object
- DArray with .includes()
4.Why might using a WeakMap be preferable for storing data related to DOM elements?
- AIt can iterate over its entries.
- BIt allows garbage collection of keys when elements are removed.
- CIt supports non-object keys.
- DIt prevents memory leaks by storing strong references.
5.You need to track the order of API requests and ensure no duplicate entries. Which data structure should you use?
- ASet
- BPlain Object
- CArray
- DMap
6.What happens when you use a number as a key in a plain object?
- AThe number remains as a number.
- BThe number is converted to a string.
- CThe key is ignored.
- DThe object throws an error.
7.A developer uses an array to check membership frequently. What is a better alternative?
- ASet for O(1) membership testing.
- BPlain Object for string keys.
- CMap for key-value pairs.
- DArray with .includes() for simplicity.