Last Updated on May 22, 2022 by Admin 2

Non-modifying STL Operations M3 Test

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

    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    using namespace std;
    bool
    identical (int a, int b)
    {
       return b == a;       //LINE I
    }
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };   
       int othernumbers[] = { 3, 9, 0, 3, 1, 4, 3, 6, 6, 9, 8, 3 };    
       vector < int > v1 (mynumbers, mynumbers + 12); 
       deque < int > d1 (othernumbers, othernumbers + 12);
       pair < deque < int >::iterator, vector < int >::iterator > result;   //LINE II
       result = mismatch (d1.begin (), d1.end (), v1.begin (), identical);    //LINE III 
       if(result.first == d1.end () && result.second == v1.end())
           cout << "identicaln";
       else
           cout << "Not identicaln";   
       return 0;
    }
    • the exception will be thrown at line LINE III
    • compilation error in LINE III
    • compilation error in LINE II
    • the program outputs Identical
    • compilation error in LINE I
    • the program outputs Not identical
  2. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    void 
    myprint (int i)
    {
      cout << i << ", ";       //Line I
    }
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, };   
       vector < int > v1 (mynumbers, mynumbers + 7); 
       set < int > s1 (mynumbers, mynumbers + 7);
       deque < int > d1 (mynumbers, mynumbers + 7);
       d1.pop_front ();      //Line II
       for_each (v1.begin (), v1.end (), myprint);   //Line III
       for_each (s1.begin (), s1.end (), myprint);
       for_each (d1.begin (), d1.end (), myprint);   
       return 0;
    }
    • the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 3, 9, 0, 2, 1, 4, 5,
    • the exception will be thrown at LINE III
    • compilation error in LINE I
    • the exception will be thrown at LINE II
    • compilation error in LINE II
    • the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 9, 0, 2,  1, 4, 5,
    • the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9, 3, 9, 0, 2, 1, 4,
  3. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <set>
    using namespace std;
    class A
    {
       int a;
    public:
       A (int a):a (a){}
     operator int () const
      {
         return a;
      }               //LINE I
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };      
       set < A > s (mynumbers, mynumbers + 12); 
       cout << equal (s.begin (), s.end (), s.begin ()) << endl;   //LINE II   
       return 0;
    }
    • compilation error in LINE I
    • the program outputs 1
    • compilation error in LINE II 
    • the program outputs false
    • the program outputs true 
    • the exception will be thrown at line LINE I
    • the program outputs 0
  4. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <set>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };      
       vector < int > v1 (mynumbers, mynumbers + 12); 
       set < int > s1 (mynumbers, mynumbers + 12);   
       v1.push_back (10);     //LINE I
       pair < set < int >::iterator, vector < int >::iterator > resultSet = mismatch (s1.begin (), s1.end (), v1.begin ()); //LINE II
       cout << *resultSet.first << ", " << *resultSet.second << endl;   //LINE III   
       return 0;
    }
    • the program outputs 10, 10
    • the program outputs 0, 3
    • the exception will be thrown at LINE II
    • compilation error in LINE I
    • compilation error in LINE II
    • compilation error in LINE III
    • the program outputs 0, 0
    • the program outputs 0, 10
  5. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <set>
    using namespace std;
    class A
    {
       int a;
    public:
       A (int a):a (a){}
    int getA () const
    {
       return a;
    }
    void setA (int a)
    {
       this-> a = a;
    }
    bool operator < (const A & b) const
     {
        return a < b.a;
      }
    };
    class Founder
    {
      A val;
    public:
       Founder (A & v):val (v){}
       bool operator () (A & v)
       {
          return (v.getA () == val.getA ());
       }
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };      
       vector < A > v1 (mynumbers, mynumbers + 7); //LINE I
       set < A > s1 (mynumbers, mynumbers + 7);   
       A a (5);  
       Founder f (a);
       find_if (s1.begin (), s1.end (), f.val);     //LINE II
       if (find_if (v1.begin (), v1.end (), f) != v1.end ())
        {                                           //LINE III
           cout << "Found!n";
        }
        else
         {
            cout << "Not found!n";
         }
       return 0;
    }
    • compilation error in LINE III
    • compilation error in LINE II
    • it will display Found!
    • compilation error in LINE I
    • it will display Not Found!
    • it will not compile successfully
  6. What will happen when you attempt to compile and run the following code?

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

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    class A
    {
       int a;
       int getA () const
       {
         return a;
       }
       void setA (int a)
        {
          this->a = a;
        }
    public:
       A (int a):a (a)
      {
      }
      bool operator == (A & b)
       {
       return a == b.a;
      }
    };
    struct Comparer
      {
        bool operator () (const A & a, const A & b)
         {
           return a.getA () == b.getA ();
         };            //LINE I
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };      
       vector < A > v (mynumbers, mynumbers + 7);   
       vector < A >::iterator it;
       A m1[] = { A (2), A (3), A(4) };
       if = find_end (v.begin(), v.end (), m1, m1 + 3, Comparer ()); //LINE II   
       cout << "Found at position: " << it + v.begin () << end1;  //LINE III
       return 0;
    }
    • program outputs: Found at position: 7
    • program outputs: Found at position: 0
    • program outputs: Found at position: 1
    • compilation error in LINE II
    • compilation error in LINE III
    • compilation error in LINE I
    • program outputs: Found at position: 2
  8. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <deque>
    using namespace std;
    class A
    {
       int a;
    public: 
       A (int a):a (a)
       {
       }
       int getA () const
        {
           return a;
        }
       void setA (int a)
        {
          this->a = a;
        }
    };
    int 
    main()
    {
      int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
      deque < int > d (mynumbers, mynumbers + 12);
      int count = count (d.begin (), d.end (), 6); //LINE I
      cout << count << endl;
      return 0;
    }
    • the program outputs 7
    • the program outputs 3
    • the program outputs 1
    • compilation error in LINE I
    • the program outputs 2
    • the exception will be thrown at LINE I
    • program executes will  and outputs nothing 
  9. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 2, 2, 5 };      
       vector < int > v (mynumbers, mynumbers + 7); 
       vector < int >::iterator it = v.begin ();    
       while ((it = adjacent_find (it, v.end())) != v.end ())
        {
           cout << it - v.begin () << ", ";
           it--;                //LINE II
        }
       cout << endl;
       return 0;
    }
    • program outputs: 2, 3,
    • program outputs: 5, 6,
    • program will run forever
    • compilation error in LINE II
    • compilation error in LINE I 
    • program outputs: 3, 4,
    • program outputs: 2, 2, 2,
  10. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    class A 
    {
      int a;
    public:
         A (int a):a (a) 
        { 
        }
    int getA () const 
     {
        return a;
     }
    int setA (int a)
     {
       this->a = a;
     }
     bool operator ==(A & b)
     {
        return a == b.a;
     }
    };
    struct Compare
    {
       bool operator () (const A & a, const A & b)
        {
          return a.getA () == b.getA ();
        }
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };      
       vector < A > v (mynumbers, mynumbers + 7); 
       vector < A >::iterator it;    
       A m1[] = { A (2), A (3), A(4) };
       it = search (v.begin (), v.end (), m1, m1 + 3, Compare ());  //LINE I
       cout << "First found at position: " << it - v.begin () << endl;   //LINE II
       return 0;
    }
    • the program outputs 5
    • the exception will be thrown at LINE I
    • compilation error in LINE I
    • the program outputs 6
    • the program outputs 7
    • compilation error in LINE II
    • the program outputs 8
  11. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <set>
    using namespace std;
    class A 
    {
      int a;
    public:
        A (int a):a (a) 
        { 
        }
    int getA () const 
     {
        return a;
     }
    void setA (int a)
     {
       this->a = a;
     }
     bool operator < (const A & b) const
     {
        return a < b.a;
     }         //LINE I
    };
    struct Compare
    {
       bool operator () (const A a)
        {
          return (a.getA () < 6);
        }
    };
    int 
    main()
    {
      int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };      
      set < A > d (mynumbers, mynumbers + 12);
      int cout = count_if(d.begin(), d.end(), Compare()); //LINE II
      cout << count << endl;
      return 0;
    }
    • the program outputs 4
    • the program outputs 6
    • the program outputs 2
    • the exception will be thrown at LINE I
    • compilation error in LINE II
    • the program outputs 1
    • compilation error in LINE I
  12. Which sentences are true about the code below? Choose all that apply.

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    class Founder
    {
    public:
        int val;
        Founder(int v):val (v) { }
        bool operator () (int v)
        {
          return (v == val);
        } //LINE I
    };
    int main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int > v1 (mynumbers, mynumbers + 7);
    if (find (v1.begin(), v1.end(), 7)==
      find(v1.begin(), v1.end(), Founder(7).val)){
    //LINE II
        cout << "Found! \n";
       }
     else{
       cout << "Not found! \n";
      }
     return 0;
    }
    •  it will display Not found!
    • compilation error in LINE II
    • it will display Found!
    • compilation error in LINE I
    • it will compile successfully
  13. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int > v1 (mynumbers, mynumbers + 7);
       vector < int >::iterator it;
       int m1[] = { 9, 0, 2 };
       it = search(v1.begin (), v1.end(), m1, m1 +3);      //LINE I
       cout << "found at position:" << it - v1.begin () << endl;   //LINE II
       return 0;
    }
    • the program outputs 0
    • the program outputs 2
    • compilation error in LINE II
    • the compilation will be thrown at LINE I
    • compilation error in LINE I
    • the program outputs 1
  14. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <deque>
    using namespace std;
    class A 
    {
      int a;
    public:
        A (int a):a (a) 
        { 
        }
    int getA () const 
     {
        return a;
     }
    void setA (int a)
     {
       this->a = a;
     }
    };
    struct Equals
    {
       bool operator () (const A & a, const A & b)
        {
          return (a.getA () == b.getA ());
        }          //LINE I
    };
    int 
    main()
    {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };      
       deque < int > d (mynumbers, mynumbers + 12); 
       deque < int >::iterator it = search_n(d.begin (), d.end(), 2, 1, Equals());  //LINE II 
       cout << it - d.begin () << endl;   //LINE III
       return 0;
    }
    • compilation error in LINE II
    • the program outputs 12
    • compilation error in LINE I
    • the exception will be thrown at LINE I
    • the program outputs 4
    • the program outputs 3
    • the program outputs 11
    • compilation error in LINE II
  15. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    class A
    {
      int a;
    public:
      A (int a):a (a){}
      int getA () const
      {
        return a;
      }
      void setA (int a)
      {
        this->a = a;
      }
      bool operator==(const A &b) const
      {
        return a == b.a;
      }
    };
    bool
    compare (const A & a, const A & b)
    {
      return a == b;
    }
    int
    main()
    {
      int mynumbers[] = { 3, 9, 0, 2, 2, 2, 5 };
      vector < A > v (mynumbers, mynumbers + 7);
      vector < A >::iterator it =v.begin ();
      while ((it = adjacent_find (it, v.end(), compare)) != v.end())
      {
        cout << it - v.begin () << ", ";
        it++; //LINE II
      }
      cout << endl;
      return 0;
    }
    • the program outputs 2, 2, 2,
    • the program outputs 3, 4,
    • program will run forever
    • the program outputs 4, 5,
    • compilation error in LINE I
    • the program outputs 2, 3,
    • compilation error in LINE II
    • the exception will be thrown at LINE II
  16. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int > v (mynumbers, mynumbers + 7);
       vector < int >::iterator it;
       int m1[] = { 9, 0, 2 };
       it = find_end (v.begin (), v.end(), m1, m1 + 3);      //LINE I
       cout <<"Found at position: " << it - v.begin () << endl;   
       return 0;
    }
    • program outputs: Found at position: 9
    • no output
    • compilation error in LINE I
    • program outputs: Found at position: 1
    • program outputs: Found at position: 2
  17. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <set>
    using namespace std;
    struct Pair
    {
       bool operator () (int a)
         {
             return (a % 2) !=0;   //LINE I
         }
    };
    
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5, 6, 6, 9, 8, 2 };
       set < int > s (mynumbers, mynumbers + 12);
       int count = count_if (s.begin (), s.end(), Pair());  //LINE II
       cout << count << endl;   
       return 0;
    }
    • the program outputs 2
    • the program outputs 1
    • the compilation will be thrown at LINE II
    • the program outputs 4
    • compilation error in LINE I
    • the program outputs 3
    • compilation error in LINE II
  18. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <set>
    using namespace std;
    struct myprinter
    {
       void operator () (int i)
         {
            cout << i <<", ";
         }
    };
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       vector < int > v1 (mynumbers, mynumbers + 7);
       deque < int > d1 (mynumbers, mynumbers + 7);
       set < int > s1 (mynumbers, mynumbers + 7);
       v1.pop_back (5);   //LINE I
       for_each (s1.begin (), s1.end (), myprinter ());   //LINE II
       for_each (d1.begin (), d1.end(), *(new myprinter()));  //LINE III
       for_each (v1.begin (), v1.end(), myprinter);  //LINE IV
       return 0;
    }
    • the program outputs 3, 9, 0, 2, 1, 4, 5, 0, 1, 2, 3, 4, 5, 9,
    • the program outputs 6, 18, 0, 4, 2, 8, 10, 6, 18, 0, 4, 2, 8, 10,
    • the program outputs 6, 18, 0, 4, 2, 8, 10, 0, 2, 4, 6, 8, 10, 18,
    • compilation error in LINE III
    • compilation error in LINE I
    • compilation error in LINE II 
    • the compilation will be thrown at LINE I
  19. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <map>
    using namespace std;
    void 
    myprint (pair < int, int > i)
    {
         cout << i.first <<", ";  
    }
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       map < int, int > m;
       for (int i = 0; i < 7; i++)
         {
            m[i] = mynumbers[i];    //LINE I
         }
       for_each(m.begin (), m.end(), myprint);   //LINE II
       return 0;
    }
    • compilation error in LINE II
    • size of the map is 8
    • size of the map is 6
    • the program outputs 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
    • size of the map is 7
    • the program outputs 0
    • the compilation will be thrown at LINE I
    • the compilation will be thrown at LINE II 
    • the program outputs 0, 1, 2, 3, 4, 5, 9,
    • the program outputs 0, 1, 2, 3, 4, 5, 6,
  20. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <map>
    using namespace std;
    int 
    main() {
       int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
       map < int, int > m;
       for (int i = 0; i < 7; i++)
         {
            m[i] = mynumbers[i];   
         }
       pair < const int, int > p (4, 1);
       map < int, int >::iterator it = find (m.begin (), m.end(), p);  //LINE I
       if (it != m.end())
          cout << it->first << endl;
       else
          cout << "Not found! \n";
       return 0;
    }
    • the program outputs 1
    • the program outputs Not found!
    • the program outputs 4
    • the compilation will be thrown at LINE I
    • compilation error in LINE I
    • the program outputs 5