C++ Unordered Map

This quiz tests your knowledge of C++ Unordered Map container and its associated operations. It contains 10 MCQs.

Last Updated :
Discuss
Comments

Question 1

What is a potential performance issue with unordered_map if a poor hash function is used?

  • Memory fragmentation.

  • Increased cache misses.

  • Performance degrades towards O(n) for lookups.

  • Increased collisions and slower lookups.

Question 2

What is the average time complexity for inserting a new element into a unordered_map?

  • O(n)

  • O(log n)

  • O(1)

  • O(n log n)

Question 3

What is the output of the program:

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_map<int, int> m = {{1, 10}, {2, 20}};
    auto it = m.find(1);
    m.erase(it);
    cout << it->second;
    return 0;
}


  • 10

  • 20

  • Undefined behavior

  • Compilation error

Question 4

How do at() and operator[] differ for missing keys?

  • at() throws exception; operator[] inserts

  • Both insert a default value

  • operator[] throws; at() inserts

  • Both throw exceptions

Question 5

What is the output of the program:

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_map<int, int> m = {{1, 10}, {2, 20}};
    m.clear();
    cout << m.size() << " " << m[1];
}


  • 0 garbage value

  • 0 0

  • 0 1

  • Error

Question 6

#include <bits/stdc++.h>

using namespace std;


int main() {

unordered_map<string, int> mp;

mp["abc"] = 1;

mp["xy"] = 2;

mp["pqr"] = 3;

auto it = mp.find("xy");

it++;

if(it != mp.end())

cout << it->first << " " << it->second;

return 0;

}

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_map<string, int> mp;
    mp["abc"] = 1;
    mp["xy"] = 2;
    mp["pqr"] = 3;
    auto it = mp.find("xy");
    it++;
    if (it != mp.end())
        cout << it->first << " " << it->second;
    return 0;
}


  • print any pair of map except ("xy", 2) this pair.

  • 3 pqr

  • Garvage values

  • Error

Question 7

What is the output of the program:

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_map<int, string> myMap;
    myMap[1] = "one";
    myMap[2] = "two";
    cout << myMap[3] << endl;
    return 0;
}


  • An empty string

  • "two"

  • undefined behavior

  • Error

Question 8

Merge two unordered map:

C++
#include <bits/stdc++.h>
using namespace std;

int main()
{
    unordered_map<int, string> map1 = {{1, "a"}, {2, "b"}};
    unordered_map<int, string> map2 = {{4, "x"}, {3, "y"}};
    map1.merge(map2);
}


  • map1 contains {1:"a", 2:"b", 3:"y", 4:"x"}, map2 is empty

  • map1 contains {1:"a", 2:"b", 3:"y"}, map2 has {2:"x"}

  • map1 remains unchanged

  • Compilation error

Question 9

Does std::unordered_map guarantee any specific order for its elements?

  • No, the order is unspecified.

  • Yes, elements are sorted by key.

  • Yes, elements are stored in insertion order.

  • Yes, elements are stored in a random order.

Question 10

If you insert a key that already exists in a unordered_map, what happens?

  • The program crashes.

  • Nothing happens; the insertion is ignored.

  • The old value is replaced with the new value.

  • The existing key-value pair is updated with the new value.

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion