C++ Map and Multimap

This quiz tests your knowledge of C++ Map and Multimap containers and their associated operations. It contains 10 MCQs.

Last Updated :
Discuss
Comments

Question 1

What will be the output of the following C++ program?

#include <bits/stdc++.h>
using namespace std;
int main()
{

multimap<int, int> mp;

mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 1, 20 });
mp.insert({ 5, 50 });

auto it = mp.equal_range(1);


for (auto itr = it.first; itr != it.second; ++itr) {
 cout << itr->first
  << '\t' << itr->second << '\n';
}
return 0;
}
  • 1 40
    1 20
     

  • 3 60
    1 20
     

  • 1 40
    2 30
     

  • 3 60
    5 50
     

Question 2

What will be the output of the following C++ program?

#include <iostream>
#include <map>
using namespace std;
int main()
{
multimap<char, int> sample;

sample.insert(make_pair('a', 10));
sample.insert(make_pair('b', 20));
sample.insert(make_pair('b', 30));
sample.insert(make_pair('c', 40));
sample.insert(make_pair('c', 50));

cout << sample.rbegin()->first << " = " << sample.rbegin()->second;
}
  • c = 50

  • c = 40

  • b = 30

  • a = 10

Question 3

Can you use the [] operator to access elements in a std::multimap?

  • Yes, it returns a reference to the element with the given key

  • Yes, it returns a pointer to the element with the given key

  • No, it is not supported by std::multimap

  • It depends on the implementation of std::multimap

Question 4

How can you retrieve all the elements with a given key in a std::multimap?

  • By calling the find() function multiple times until it returns an invalid iterator

  • By calling the search() function with the key as an argument

  • By calling the equal_range() function with the key as an argument

  • By calling the get() function with the key as an argument

Question 5

What is the return type of the equal_range() function in a std::multimap?

  • A boolean value indicating whether the element was found

  • A reference to the element

  • A pointer to the element

  • A pair of iterators to the first and last element with the given key

Question 6

How is the std::map class implemented in C++?

  • As a linked list

  • As an array

  • As a red-black tree

  • As a hash table

Question 7

What is the output of the following code?

#include <iostream>
#include <map>
int main() {
 std::map<int, int> m {{1, 2}, {3, 4}, {5, 6}};
 m[3] = 5;
 for (auto it = m.begin(); it != m.end(); ++it) {
   std::cout << it->first << " " << it->second << std::endl;
 }
 return 0;
}
  • 1 2
    3 5
    5 6
     

  • 1 2
    3 4
    5 6
     

  • 1 2
    4 5
    5 6
     

  • 1 4
    3 5
    5 6
     

Question 8

Which of the following statements about the map container class in the STL is NOT true?

  • A map is an ordered container that stores only unique key value pairs.

  • A map is implemented as a linked list.

  • A map does not allow element modification or removal.

  • A map has constant time complexity for element access.

Question 9

What is the time complexity of clearing all elements from a map in the STL?

  • O(1)

  • O(n)

  • O(log n)

  • O(n log n)

Question 10

What is the time complexity of erasing an element from a map in the STL? 
 

  • O(1)

  • O(n)

  • O(log n)

  • O(n log n)

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion