Last Updated on May 22, 2022 by Admin 2

Associative STL Containers M2 Test

  1. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };   
       string words[] = { "three", "nine", "zero", "two", "one", "four", "five" };   
       map < int, string > m;
       for (int i = 0; i < 7; i++)
           m.insert (pair < int, string> (mynumbers[i], words[i]));   //LINE I
       if(m[10] == "ten")    //LINE II
           cout << "tenth element";
       for(map < int, string >::iterator i = m.begin (); i != m.end (); i++)
           cout << i->second <<", ";
       cout << m.size ();     //LINE III   
       return 0;
    }
    • the exception will be thrown at line LINE II
    • program outputs: zero, one, two, three, four, five, nine, , 7
    • program outputs: one, two, three, four, five, nine, , 7
    • compilation error in LINE III
    • program outputs: zero, one, two, three, four, five, nine, , 8
    • compilation error in LINE I
  2. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <list>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 5 };   
       list < int >v (mynumbers, mynumbers + 7);   //LINE I  
       multiset < int >s1 (v.begin (), v.end ());
       if(s1.count (5) == 1)    //LINE II
           s1.erase (5);
       for(multiset < int >::iterator i = s1.begin (); i != s1.end (); i++)
           cout << *i <<", ";   
       return 0;
    }
    • program outputs: 0, 1, 2, 3, 4, 5, 5, 9,
    • the exception will be thrown at line LINE I
    • program outputs: 0, 1, 2, 3, 4, 9,
    • compilation error in LINE II
    • program outputs: 0, 1, 2, 3, 4, 5, 9,
  3. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 7 };   
       vector< int >v (mynumbers, mynumbers + 7);    
       multiset < int >s1 (v.begin (), v.end ());
       s1.insert (v.begin (), v.end ());
       s1.insert (v.begin (), v.end ());   //LINE I
       pair < multiset < int >::iterator, multiset < int >::iterator > range;
       range = s1.equal_range (5);
       while (range.first != range.second)
          {
              cout << *range.first << ", ";
              range.first++;
          }   
       return 0;
    }
    • program outputs: 5, 5, 9, 9,
    • program outputs: 5, 5,
    • program outputs: 5, 5, 5,
    • compilation error in LINE I
    • program outputs: 5, 7, 9,
    • program outputs: 5,
  4. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };   
       string words[] = { "three", "nine", "zeor", "two", "one", "four", "five" };
       map < int, string > m;
       for (int i = 0; i < 10; i++)
          m.push_back (pair < int, string > (mynumbers[i], words[i]));   //LINE I
       for (map < int, string >::iterator i = m.begin (); i != m.end(); i++)   //LINE II
          cout << i->first <<", ";
       return 0;
    }
    • compilation error in LINE I
    • compilation error in LINE II because of redeclaration of i variable
    • program outputs: 0, 1, 2, 3, 4, 5, 9,
    • program outputs: 3, 9, 0, 2, 1, 4, 5,
    • the exception will be thrown at line LINE I
  5. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };   
       vector<int> v(mynumbers, mynumbers+7);
       set<int> s1(v.begin(),v.end());
       s1.insert(v.begin(),v.end());
       s1.insert(v.begin(),v.end());//LINE I
       set<int>::iterator found = s1.find(9);
       for (; found!=s1.end(); ++found)
          cout << *found <<", ";
       return 0;
    }
    • program outputs: 9, 0, 2, 1, 4, 5,
    • program outputs: 3, 9, 0, 2, 1, 4, 5,
    • code will not compile
    • the exception will be thrown at line LINE I
    • program outputs: 9,
  6. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <vector>
    #include <string>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       string words[] = { "three", "nine", "zero", "two", "one", "four", "five" };   
       map < int, string > m;
       for (int i = 0; i < 7; i++)
           m.insert (pair < int, string > (mynumbers[i], words[i]));   //LINE I   
       m[0] = "ten";           //LINE II
       m.insert (pair < int, string > (1, "eleven"));   //LINE III
       for (map < int, string >::iterator i = m.begin (); i != m.end (); i++)
          cout << i->second <<", ";
       return 0;
    }
    • program outputs: ten, one, two, three, four, five, nine,
    • the exception will be thrown at line LINE III
    • program outputs: zero, one, two, three, four, five, nine, ten, eleven
    • compilation error in LINE II
    • program outputs: three, nine, zero, two, one, four, five,
    • program outputs: three, nine, zero, two, one, four,
    • the exception will be thrown at line LINE I
    • program outputs: ten, one, two, three, four, five, nine, eleven
  7. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    #include <functional>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int >v (mynumbers, mynumbers + 7);
       multiset < int > s1 (v.begin (), v.end ());  
       multiset < int, greater < int >>s2 (v.begin (), v.end ());   //LINE I
       s2.insert (9);          //LINE II
       for (multiset < int >::iterator i = s1.begin (); i != s1.end (); i++)
           cout << *i << ", ";
       for (multiset< int, greater < int >>::iterator i = s2.begin ();
            i != s2.end (); i++)
          cout << *i <<", ";
       cout << endl;
       return 0;
    }
    • compilation error in LINE I
    • the exception will be thrown at line LINE II
    • 0, 1, 2, 3, 4, 5, 9, 9, 5, 4, 3, 2, 1, 0, 9,
    • 0, 1, 2, 3, 4, 5, 9, 9, 9, 5, 4, 3, 2, 1, 0,
  8. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <vector>
    #include <sstream>
    #include <string>
    using namespace std;
    int
    main()
    {
      int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
      vector < int >v (mynumbers, mynumbers + 7);
      multimap < int, string >m;
      for (vector< int >::iterator i = v.begin (); i != v.end (); i++)
      {
        stringstream s;
       s << *i << *i;
       m.insert (pair < int, string > (*i, s.str ()));
      }
      for (multimap< int, string >::iterator i = m.begin (); i != m.end (); i++)
      {
        cout << i->first <<", "; //LINE I
      }
      return 0;
    }
    • program outputs: 00, 11, 22, 33, 44, 55, 99,
    • compilation error in LINE I
    • program outputs: 0, 1, 2, 3, 4, 5, 9,
    • program outputs: 00, 11, 22, 33, 44, 55,
    • program outputs: 11, 22, 33, 44, 55, 99,
  9. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <vector>
    #include <sstream>
    #include <string>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 3, 2, 1, 4, 5 };
       vector < int >v (mynumbers, mynumbers + 7);
       map< int, string >m;
       for (vector< int >::iterator i = v.begin (); i != v.end (); i++)
         {
            stringstream s;      
            s << *i; 
            m.insert (pair < int, string > (*i, s.str ()));
          }
    pair < map < int, string >::iterator, map < int, string >::iterator > range;
    range = m.equal_range (3);
    map < int, string >::iterator i = range.first; //LINE I
       for (; i != range.second; i++)
          {
             cout << i->first <<", ";
           }
       return 0;
    }
    • program outputs:3,
    • compilation error in LINE I
    • program outputs: 3, 3,
    • program outputs: 3, 4, 5, 9,
    • program outputs: 4, 5, 9,
    • program outputs: 3, 3, 4, 5, 9,
  10. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <vector>
    #include <sstream>
    #include <string>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int >v (mynumbers, mynumbers + 7);
       multimap< int, string >m;
       for (vector< int >::iterator i = v.begin (); i != v.end (); i++)
         {
            stringstream s;      
            s << *i << *i << *i; 
            m.insert (pair < int, string > (*i, s.str ()));   //LINE I
          }
    pair < multimap < int, string >::iterator, multimap < int, string >::iterator > range;   //LINE I
    range = m.equal_range (4);
    for (multimap < int, string >::iterator i = range.first; i != range.second; i++)
        {
           cout << i->first <<", ";
        }
       return 0;
    }
    • the exception will be thrown at line LINE I
    • the exception will be thrown at line LINE II
    • program outputs:4, 5, 9
    • program outputs:4,
    • program outputs:4, 9 
  11. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int
    main()
    {
      int mynumbers[] = {3, 9, 0, 2, 1, 4, 5};
      set <int>s1 (mynumbers, mynumbers + 7);
      multiset <int> s2 (s1.begin(), s1.end());
      s2.insert(s1.begin(), s1.end());
      s2.erase(s2.lower_bound(1), s2.upper_bound(4)); //LINE I
      for(multiset<int> :: iterator i = s2.begin(); i != s2.end(); i++ )
      {
       cout << *i <<", ";
      }
      return 0;
    }
    • the output will be 0, 0, 5, 5, 9, 9,
    • the output will be 0, 1, 4, 5, 9, 0, 1, 4, 5, 9,
    • the exception will be thrown at line LINE I
    • the output will be 0, 5, 9,
    • the output will be 0, 1, 2, 3, 4, 5, 9,
    • the output will be 0, 1, 4, 5, 9,
  12. Which changes, introduced independently, will allow the code to compile and display zero, one, two, five, nine,?

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    class A
    {
      int a;
    public:
      A (int a):a (a){}
      int getA () const
     {
        return a;
     }
     bool operator < (const A & b) const
      {
        return a < b.a;
      }                 //LINE I
    };
    int main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       string words[] = { "three", "nine", "zero", "two", "one", "four", "five" };
       map < A, string > m;
       for (int i = 0; i < 7; i++)
           {
              m.insert (pair < A, string > (A (mynumbers[i]), words[i]));
            }
       m.erase(m.lower_bound(3), m.upper_bound(4));     //LINE II
       map < A, string >::iterator i = m.begin();
       for (; i != m.end(); i++)
            cout << i->second << ", ";
       return 0;
    }
    • change LINE II to m.erase(m.lower_bound(3), m.upper_bound(5));
    • change code at LINE I to bool operator < (const A & b) const { return b.a
    • change LINE II to m.erase(m.lower_bound(3), m.upper_bound(4));
    • code compiles and executes successfully with expected result
    • code compiles and executes successfully but results are other than expected
  13. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    int
    main()
    {
      int mynumbers[] = {3, 9, 0, 2, 1, 4, 5};
      string words[] = {"three", "nine", "zero", "two", "one", "four", "five"};
      multimap<int, string> m;
      for(int i = 0; i<7; i++)
        m.insert(pair<int,string>(mynumbers[i],words[i]));
      if(m.find(0)->second == "zero") //LINE I
        cout << "found expected ";
      for(multimap<int,string> ::iterator i = m.begin(); i != m.end(); i++)
        cout << i->second <<", ";
      cout << m.size();
      return 0;
    }
    • compilation error in LINE I
    • program outputs: found expected zero, one, two, three, four, five, nine, 7
    • program outputs: found expected zero, one, two, three, four, five, nine, 8
    • the exception will be thrown at line LINE I
    • program outputs: zero, one, two, three, four, five, nine, 8
  14. Which changes, introduced independently, will allow the code to compile and display zero, one, nine,? Choose all that apply.

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    class A
    {
      int a;
    public:
      A (int a):a (a){}
      int getA () const
     {
        return a;
     }
     //LINE I
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       string words[] = { "three", "nine", "zero", "two", "one", "four", "five" };
       multimap < A, string > m;
       for (int i = 0; i < 7; i++)       
              m.insert (pair < A, string > (A (mynumbers[i]), words[i]));       
       m.erase(m.lower_bound(2), m.upper_bound(5));     
       m.erase(m.lower_bound(2), m.upper_bound(5)); 
       multimap < A, string >::iterator i = m.begin(); //LINE II
       for (; i != m.end(); i++)
            cout << i->second << ", ";
       cout << end1;
       return 0;
    }
    • multimap::itemrator i=m.end(); inserted at LINE II
    • bool operator < (const A & b) const { return b.a inserted at LINE I
    • code compiles and executes successfully
    • bool operator < (const A & b) const { return a inserted at LINE I
  15. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       string words[] = { "three", "nine", "zero", "two", "one", "four", "fine" };   
       multimap < int, string >m;  
       for (int i = 0; i < 3; i++)
           m.push_back (pair < int, string> (mynumbers[i], words[i]));   //LINE I
     for (multimap < int, string >::iterator i = m.begin (); i != m.end(); i++)   
           cout << i->first<<" ";  //LINE II
      return 0;
    }
    • the exception will be thrown at line LINE I
    • the exception will be thrown at line LINE II
    • program outputs: 0, 1, 2,
    • compilation error in LINE I
    • compilation error in LINE II
    • program outputs: 0, 1, 2, 3,
  16. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    template < class T > void
    print (T start, T end)
    {
        while (start != end)
          {
             std::cout << *start << ", ";
             start++;
          }
    }
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 7 };
       vector < int >v (mynumbers, mynumbers + 7);   
       multiset < int >s1 (mynumbers, mynumbers + 7);  
       for (int i = 9; i > 0; i)
          {
             double x = s1.pop ();   //LINE I
             v.push_back (i + x);    //LINE II
          }
      print(v.begin(), v.end());
      cout << endl;
      return 0;
    }
    • the exception will be thrown at line LINE II
    • compilation error in LINE I
    • program outputs: 3, 9, 0, 2, 1, 4, 5, 12, 14, 4, 5, 3, 5, 5,
    • program outputs: 3, 9, 0, 2, 1, 4, 5, 7, 12, 16, 5, 6, 4, 6, 6, 7,
    • compilation error in LINE I
    • the exception will be thrown at line LINE I, because of too many calls to pop method 
  17. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <list>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 4 };
       list< int >v (mynumbers, mynumbers + 7);    //LINE I   
       set< int >s1 (v.begin (), v.end ());
       if (s1.count (4) == 2)
         {
             s1.erase (4);
         }  
       for (set < int >::iterator i = s1.begin(); i != s1.end(); i++)
          {
             cout << *i << ", ";
          }
      return 0;
    }
    • program outputs: 0, 1, 2, 3, 5, 9, 4
    • compilation error in LINE I
    • program outputs: 0, 1, 2, 3, 5, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 9, 4,
  18. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 5 };
       string words[]=
         { "three", "nine", "zero", "two", "one", "four", "five", "five" };   
       map < int, string > m;
       for (int i = 0; i < 8; i++)     {
            m.insert (pair < int, string > (mynumbers[i], words[i]));
         }
       if (m.count (5) == 2)
           m.erase (2);  
       for (map < int, string >::iterator i = m.begin(); i != m.end(); i++)
          {
             cout << i->first << ", ";
          }
      return 0;
    }
    • program outputs: zero, one, three, four, five, nine
    • program outputs: zero, one, two, three, four, five, nine
    • program outputs: 0, 1, 3, 5, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 5, 9,
  19. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, };
       vector < int >v (mynumbers, mynumbers + 7);
       set < int >s1 (v.begin (), v.end ());
       s1.insert (v.begin (), v.end ());
       s1.insert (10);
       s1.erase (s1.lower_bound (4), s1.upper_bound (6));
       s1.insert (v.begin (), v.end ());  
       for (set < int >::iterator i = s1.begin(); i != s1.end(); i++)      
             cout << *i << ", ";      
      return 0;
    }
    • program outputs: 0, 1, 2, 3, 4, 5, 9, 10,
    • program outputs: 0, 1, 2, 3, 4, 5, 10,
    • size of s1 set is 7
    • size of s1 set is 8
  20. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 7 };
       multiset < int >s1 (mynumbers, mynumbers + 7);   //LINE I
       s1.insert (s1.find (3), 6);   //LINE II  
       for (multiset < int >::iterator i = s1.begin(); i != s1.end(); i++)      
             cout << *i << ", ";      
      return 0;
    }
    • the exception will be thrown at line LINE I
    • compilation error in LINE II
    • program outputs: 0, 1, 2, 3, 6, 4, 5, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 6, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 6, 7, 9,
    • program outputs: 0, 1, 2, 3, 4, 5, 9,