C++ Programming Questions and Answers – More Containers

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “More Containers”.

1. Which container is used to store elements as key-value pair?
a) map
b) multimap
c) unordered map
d) all of the mentioned
View Answer

Answer: d
Explanation: C++ provides these three containers(map, multimap and unordered map) to store elements as key-value pair.
advertisement

2. Which container can have the same keys?
a) map
b) multimap
c) unordered map
d) set
View Answer

Answer: b
Explanation: C++ provide multimap container that is used to make map that can contain same keys i.e. {a: 5} and {a:10} both can exist.

3. Which container is best to keep the collection of distinct elements?
a) multimap
b) heap
c) set
d) queue
View Answer

Answer: c
Explanation: C++ provides a set container to store a collection of distinct elements. This container behaves similar to mathematical sets.

4. Which container is used to keep priority based elements?
a) queue
b) stack
c) set
d) priority queue
View Answer

Answer: d
Explanation: C++ provides priority queue container that stores elements based on their priority. For example, if the absolute value is the priority then -6 will be kept before 4 in the priority queue.
advertisement

5. Sets are implemented using _______________________
a) binary search tree
b) red black tree
c) avl tree
d) heap
View Answer

Answer: a
Explanation: Sets are implemented using the search tree so that we can check the presence of any element to be inserted in O(logn) time in order to remove conflicts between elements.

6. Unordered map is implemented using _________________
a) binary search tree
b) red black tree
c) heap
d) hash table
View Answer

Answer: d
Explanation: As unordered map has no order of keys therefore hash table is used to store key-value pairs in a hash table.

7. Map is implemented using ____________________
a) binary search tree
b) red black tree
c) heap
d) hash table
View Answer

Answer: b
Explanation: The map has some order of stored keys therefore red black tree is used to maintain that order and access the elements as soon as possible.
advertisement

8. Which of the following is correct about the map and unordered map?
a) Ordering of keys in maps whereas no such order in the unordered map
b) Maps are implemented red-black trees whereas unordered map are implemented using hash tables
c) Average search time in the unordered map is O(1) whereas it is O(logn) in case of maps
d) All of the mentioned
View Answer

Answer: d
Explanation: All the above mentioned points are correct about maps and unordered maps. Maps uses red-black tree whereas unordered map uses hash tables therefore the average search time for the unordered map is O(1) whereas it is O(logn) in case of maps.

9. Which of the following queue container can expand or shrink from both directions?
a) deque
b) queue
c) priority queue
d) stack
View Answer

Answer: a
Explanation: Deque is a short form for a doubly ended queue which can be expanded and shrinked from any side of the queue either from the front or from the back.

10. Which of the following is correct about map and multimap?
a) Map can have same keys whereas multimap cannot
b) Implementation of maps and multimap are different
c) Multimap can have same keys whereas the map cannot
d) Average search time of map is greater than multimap
View Answer

Answer: c
Explanation: Multimap is similar to map, the only difference that they have is that in multimap elements can have the same keys where in the map we have only one to one key-value pair correspondence.
advertisement

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn