Advanced C++ | Summary Test

  1. What happens if you try to compile and run this program?

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

    #include <vector>
    #include <iostream>
    #include<algorithm>>
    using namespace std;
    void
    printer (int i)
    {
      cout << i <<", ";
    }
    int
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 1, 4, 5};
      vector <int> v1 (12);
      vector <int> v2 (7);
      sort (mynumbers1, mynumbers1 + 4);
      copy (mynumbers1, mynumbers1 + 4, v1.begin());
      sort (mynumbers2, mynumbers2 + 4);
      copy (mynumbers2, mynumbers2 + 4, v1.begin() + 5); //LINE I
      sort (v1.begin(), v1.end());
      merge (v1.begin() + 4, v1.begin() + 7, v1.begin() + 5,v1.begin() + 8, v2.begin()); //LINE II
      for_each (v2.begin(), v2.end(), printer);
      return 0;
    }
    • the program outputs 0, 1, 1, 2, 2, 3,
    • the program outputs 0, 0, 0, 1, 1, 2, 2,
    • the program outputs 0, 1, 1, 2, 2, 3, 0,
    • compilation error in LINE II
    • compilation error in LINE I
  3. What will happen when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    void 
    printer(int i)
    {
      cout << i <<", ";
    }
    int 
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 1, 4, 5};
      vector <int> v1 (6);
      sort (mynumbers2, mynumbers2 + 4);
      sort (mynumbers1, mynumbers1 + 4);  //LINE I
      merge (mynumbers1, mynumbers1 + 2, mynumbers2, mynumbers2 + 2, v1.begin());  //LINE II
      for_each (v1.begin(), v1.end(), printer);
      return 0;
    }
    • the program outputs 3, 9, 0, 6, 1, 4,
    • the program outputs 0, 1, 2, 4,
    • compilation error in LINE II
    • the program outputs 0, 1, 2, 4, 0, 0,
    • compilation error in LINE  I
  4.  What will happen when you attempt to compile and run the following code? Choose all that apply:

    #include <vector> 
    #include <iostream> 
    #include <algorithm> 
    using namespace std;
    void
    printer (double i)
    {
      cout << i << ", ";
    }
    bool
    compare (double a, double b)
    {
      return int (a) < int (b);
    }
    int 
    main()
    {
      double mynumbers[] = {1.11, 3.13, 2.12, 5.15, 6.16};
      vector<double> v1(mynumbers, mynumbers + 5);
      stable_sort (v1.begin(), v1.end(), compare);  //LINE I
      remove (v1.begin(), v1.end(), 5.15);  //LINE II
      for_each (v1.begin(), v1.end(), printer);
      return 0;
    }
    • size of v1 vector is 5
    • the program outputs 1.11, 2.12, 3.13, 6.16, 6.16,
    • the program outputs 1.11, 3.13, 2.12, 6.16, 6.16,
    • the program outputs 1.11, 3.13, 2.12, 6.16,
    • size of v1 vector is 6
    • compilation error in LINE II
    • compilation error in LINE I
  5. What will happen when you attempt to compile and run the following code?

    #include
    #include
    using namespace std;
    template ostream & print(T & start, T & end)
    {
      for(; start != end; ++start)
          cout << *start <<" ";
      return cout;
    }
    int main()
    {
      int tab[] = {8, 7, 6, 4, 2, 1};
      deque d1(tab, tab + 6);
      deque d2;
      deque :: iterator it;
      for(it = d1.begin(); it != d1.end(); ++it)
      {
        d2.push_back(d1[d1.end() - it -1]);  //LINE I
      }
      print(d2.rbegin(), d2.rend()) << endl;  //LINE II
      return 0;
    }
    • compilation error in LINE I
    • program outputs: 1,8,
    • program outputs: 8,7,6,4,2,1
    • program outputs: 1,2,4,6,7,8,
    • compilation error in LINE II
    • runtime error in LINE I
  6. What will happen when you attempt to compile and run the following code?

    #include <iomanip>
    #include <iostream>
    using namespace std;
    int
    main()
    {
      double goodpi = 3.141593;
      double badpi = 3.5;
      cout << goodpi << ", ";
      cout << scientific;   //LINE I
      cout << setprecision(4);  //LINE II
      cout.unsetf(ios::floatfield);
      cout << goodpi << ", ";
      cout << badpi << ", ";
      return 0;
    }
    • the program outputs 3.141593, 3.14159, 3.5,
    • compilation error in LINE II
    • compilation error in LINE I
    • the program outputs 3.14159, 3.142, 3.5,
    • the program outputs 3.14159, 3.142e+000, 3.5,
  7. Which of the following examples shows the proper way to create a new priority_queue container assuming all necessary declarations has been performed. Choose all that apply:

    • queue q; ;;;;
    • list_queue 1; priority_queue q(1); ;;;;
    • deque d; priority_queue q(d.begin(), d.end()); ;;;;
    • deque d; priority_queue q(d);
    • vector v; priority_queue q(v); ;;;;
  8. What will happen when you attempt to compile and run the following code?

    #include <vector> 
    #include <iostream> 
    #include <algorithm> 
    using namespace std; 
    void 
    printer (int i) 
    { 
       cout << i << ", "; 
    }
    int
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 2, 4, 5};
      vector <int> v1(2);
      sort (mynumbers2, mynumbers2 + 4);
      sort (mynumbers1, mynumbers1 + 4);  //LINE I
      set_intersection (mynumbers1, mynumbers1 + 3, mynumbers2, mynumbers2 + 2, v1.begin());  //LINE II
      for_each (v1.begin(), v1.end(), printer);
      return 0;
    }
    • the program outputs 2,
    • runtime error at LINE II
    • the program outputs 2, 2,
    • compilation error at LINE I
    • the program outputs 2, 0,
    • compilation error at LINE II
  9. What will happen when you attempt to compile and run the following code?

    #include <iostream> 
    #include <algorithm> 
    #include <vector> 
    using namespace std; 
    struct compare
    {
      bool operator () (int a)
      {
        return (a > 0);
      }                    //LINE I
      operator int () const
      {
        return (4);
      }                    //LINE II
    };
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1, 4, 4, 9, 7, 2};
      vector <int> v (mynumbers, mynumbers + 11);
      int count = std::count(v.begin(), v.end(), compare());  //LINE III
      cout << count << endl;
      return 0;
    }
    • compilation error in LINE I
    • the program outputs 4
    • compilation error in LINE II
    • the program outputs 3
    • compilation error in LINE III
    • runtime error at LINE III
    • the program outputs 1
  10. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 6 5 7<enter> ?

    #include <iostream> 
    #include <string> 
    using namespace std; 
    int
    main()
    {
      string s;
      cin >> s;   //LINE I
      cout <<s<<", "<<s<<", "<<endl;  //LINE II
      return 0;
    }
    • compilation error in LINE II
    • compilation error in LINE I
    • the program outputs 6, 6,
    • the program outputs 6 5 7, 6 5 7,
    • the program outputs 6, 5, 7, 6, 5, 7,
  11. What happens if you try to compile and run this program?

    #include <iostream> 
    #include <set> 
    #include <vector> 
    using namespace std; 
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4,  1,6};
      multiset <int> s1 (mynumbers, mynumbers + 6);  //LINE I
      s1.insert (s1.find(9), 5);  //LINE II
      for(multiset <int>::iterator i = s1.begin(); i != s1.end(); i++)
         cout << *i <<", ";
      return 0;
    }
    • compilation error in LINE I
    • program outputs: 1, 4, 5, 6, 6, 7, 8, 9,
    • runtime error at LINE I
    • program outputs: 1, 4, 5, 6, 7, 8, 9,
    • program outputs: 1, 4, 5, 6, 7, 8,
    • compilation error in LINE II
  12. What happens when you attempt to compile and run the following code?

    #include <iostream> 
    #include <set> 
    #include <vector> 
    using namespace std; 
    
    int 
    main()
    {
      int mynumbers[] = { 8, 9, 7, 6, 4, 1, 2 };
      vector <int> v (mynumbers, mynumbers + 6);
      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(6);
      while(range.first != range.second)
      {
        cout << *range.first <<", ";  //LINE II
        range.first++;
      }
     return 0;
    }
    •  runtime error at LINE I
    • compilation error in LINE I
    • compilation error in LINE II
    • the program outputs 6, 6, 6, 6, 
    • the program outputs 6, 6, 6,
    • the program outputs 6, 4, 1, 2,
    • runtime error at LINE II
  13. What happen when you attempt to compile and run the following code? Choose all that apply:

    #include <vector> 
    #include <iostream> 
    #include <algorithm> 
    using namespace std; 
    
    void
    printer(double i)
    {
      cout <<i<<", ";
    }
    bool
    compare (double a, double b)
    {
      return int (a) < int (b);
    }
    int
    main()
    {
      double mynumbers[] = { 1.11, 3.13, 2.12, 5.15, 6.16};
      vector <double> v1 (mynumbers, mynumbers + 5);
      stable_sort (v1.begin(), v1.end(), compare);  //LINE I
      remove (v1.begin(), v1.end(), 5.15);  //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • size of v1 vector is 5
    • the program outputs 1.11, 3.13, 2.12, 6.16,
    • compilation error in LINE II
    • compilation error in LINE I
    • size of v1 vector is 6
    • the program outputs 1.11, 3.13, 2.12, 6.16, 6.16,
    • the program outputs 1.11, 2.12, 3.13, 6.16, 6.16,
  14. What will happen when you attempt to compile and run the following code?

    #include 
    #include 
    using namespace std;
    template ostream & print(T & start, T & end)
    {
      for(; start != end; ++start)
          cout <<*start <<" ";
      return cout;
    }
    int main()
    {
      int tab[] = {8, 7, 6, 4, 2, 1};
      deque d1(tab, tab+6);
      deque d2;
      deque::iterator it;
      for(it = d1.begin(); it != d1.end(); ++it)
      {
           d2.push_back(d1[d1.end() - it -1]);  //LINE I
      }
      print(d2.rbegin(), d2.rend()) <<endl;  //LINE II
      return 0;
    }
    • program outputs: 8, 7, 6, 4, 2, 1
    • compilation error in LINE I
    • program outputs: 1, 2, 4, 6, 7, 8
    • compilation error in LINE II
    • program outputs: 1, 8
    • runtime error at LINE I
  15. 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[] = {8, 9, 7, 6, 4, 1};
      vector <int> v (mynumbers, mynumbers + 6);
      multiset <int> s1 (v.begin(), v.end());
      multiset <int, greater_equal <int>> s2 (v.begin(), v.end());  //LINE I
      s2.insert (3);    //LINE II
      for(multiset<int, greater<int>>::iterator i = s2.begin(); i != s2.end(); i++)
         cout << *i <<", ";
      cout << endl;
      return 0;
    }
    • compilation error in LINE I
    • program outputs: 9, 8, 7, 6, 4, 3, 1,
    • compilation error in LINE II
    • program outputs: 9, 8, 7, 6, 4, 1,
    • program outputs: 8, 9, 7, 6, 4, 1, 3,
    • runtime error at LINE I
  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[] = { 8, 6, 4, 1};
      vector <int> v (mynumbers, mynumbers + 6);
      set <int> s (v.begin(), v.end());
      for(int i = 4; i > 0; i)
       {
          int x = *(s.begin());  //LINE I
          s.pop();               //LINE II
          v.push_back(i + x);
        }
      print(v.begin(), v.end());
      print(s.begin(), s.end());
      cout << endl;
      return 0;
    }
    • program outputs: 8, 6, 4, 1, 8, 6, 4, 1,
    • program outputs: 8, 6, 4, 1, 
    • runtime error at LINE I
    • compilation error in LINE II
    • compilation error in LINE I
    • program outputs: 8, 6, 4, 1, 1, 4, 6, 8 
  17. What will happen when you attempt to compile and run the following code?

    #include <vector> 
    #include <iostream> 
    #include <algorithm> 
    using namespace std;
    int
    main()
    {
      int mynumbers[] = { 8, 9, 7, 6, 4, 1 };
      vector <int> v1 (mynumbers, mynumbers + 6);  //LINE I
      cout << * max_element(v1.begin(), v1.end()) <<", ";  //LINE II
      return 0;
    }
    • you can call max_element function on non ordered v1 ector
    • runtime error at LINE II
    • you can’t  call max_element function on non ordered v1 ector
    • the program outputs 8, 
    • the program outputs 9, 
    • the program outputs 1, 
    • compilation error at LINE I
  18. What will happen when you attempt to compile and run the following code? Choose all that apply:

    #include <vector> 
    #include <iostream> 
    #include <algorithm> 
    using namespace std;
    
    template <calss T> struct Out
    {
      ostream & out;
      Out(ostream & o):out (o){}
      void operator() (const T & val)
      {
        out << val <<", ";
      }
    };
    struct Sequence
    {
      int start;
        Sequence (int start):start (start){}
        int operator()()
        {
          return 2 * (start++ % 3);
        }
     };
    int
    main()
    {
       vector<int>v1 (3);
       vector <int> v2 (3);
       generate (v1.begin(), v1.end(), Sequence(5));  //LINE I
       sort (v1.rbegin(), v1.rend());
       unique_copy(v1.begin(), v1.end(), v2.begin());  //LINE II
       for_each (v1.begin(), v1.end(), Out<int>(cout));
       for_each (v2.begin(), v2.end(), Out<int>(cout));
       return 0;
    }
    • the program outputs 4, 2, 0, 0, 2, 4, 
    • the program outputs 1, 2, 4, 1, 2, 4, 
    • you can call unique_copy function on these vector (v1, v2)
    • the program outputs 4, 2, 0, 4, 2, 0, 
    • compilation error in LINE I
    • compilation error in LINE II
  19. What will happen when you attempt to compile and run the following code?

    #include <iostream> 
    
    using namespace std;
    int
    main()
    {
      cout <<31<<", ";
      cout.setf(ios::hex);     //LINE I
      cout <<31<<", ";
      cout.setf(ios::showbase);  //LINE II
      cout <<63<<", ";
      return 0;
    }
    • the program outputs 31, 0x1f, 0x3f, 
    • runtime error at LINE II
    • the program outputs 31, 31, 63, 
    • compilation error in LINE I
    • compilation error in LINE II
    • the program outputs 31, 1f, 0x3f, 
  20. What will happen when you attempt to compile and run the following code assuming that file input.txt contain following sequence: i j k?

    #include <iostream>
    #include <string>
    #include <vector>
    #include<algorithm>
    #include <iomanip>
    #include <fstream>
    using namespace std;
    void
    printer (char c)
    {
      cout << setw(2) << c <<", ";
    }
    int
    main()
    {
      ifstream inputfile("input.txt");
      vector <char> v1;
      char c;
      do
       {
         inputfile >> c;  //LINE I
         v1.push_back (c);
        }
       while(inputfile.good());  //LINE II
       inputfile.close();
       for_each (v1.begin(), v1.end(), printer);
       return 0;
    }
    • program outputs: i, j, k,  (one space after commas)
    • compilation error in LINE I
    • program outputs: i, j, k, k,  (one space after commas)
    • program runs forever without output
    • compilation error in LINE II
    • program outputs: i,  j,  k,  k,   (two space after commas)
  21. What will happen when you attempt to compile and run the following code? Choose all that apply:

    #include <vector>
    #include <iostream>
    
    int
    main()
    {
        std::vector < int >v1;
        v1.push_back(17);       //LINE I
        std::cout<<v1.front ()<<", " <<v1.back() << std::endl;   //LINE II
        return 0;
    }
    • program outputs: 0, 0,
    • code compiles and executes successfully
    • compilation error in LINE II
    • compilation error in LINE I
    • program outputs: 17, 17,
    • program outputs: 0, 17,
  22. What will happen when you attempt to compile and run the following code?

    #include<deque>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    class Pocket
    {
      int value;
    public:
      Pocket (int value):value(value){}
      int getValue() const
      {
        return value;
      }
      bool operator < (const Pocket & _Right) const
      {
        return value < _Right.value;
      }
    };
    ostream & operator << (ostream & stream, const Pocket & pocket)
    {
      stream << pocket.getValue();
      return stream;
    }
    void
    printer (Pocket i)
    {
      cout << i << ", ";
    }
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1 };
      deque <Pocket> d1 (mynumbers, mynumbers + 6);
      sort (d1.begin(), d1.end());
      d1.push_back(3);    //LINE I
      pair <deque <Pocket>::iterator, deque<Pocket>::iterator> result = equal_range(d1.begin(), d1.end(), Pocket(4));  //LINE II
      for_each(result.first, result.second, printer);
      return 0;
    }
    • runtime error at LINE I
    • the program outputs 3, 
    • compilation error in LINE I
    • runtime error at LINE II
    • compilation error in LINE II
    • the program outputs 3, 4, 
    • the program outputs 4,  
  23. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include<vector>
    #include <set>
    
    using namespace std;
    void
    printer(int i)
    {
      cout << i << ", ";
    }
    struct sequence
    {
      int val, inc;
    public:
      sequence(int s, int i):val(s), inc(i){}
      operator int() const
      {                  //LINE I
        int r = val;
            return r;
      }
    };
    int
    main()
    {
      vector <int> v1(5);
      fill(v1.begin(), v1.end(), sequence(2, 4));   //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • runtime error at LINE II
    • the program outputs2, 2, 2, 2, 2, 
    • the program outputs2, 4, 2, 4, 2, 
    • the program outputs2, 3, 4, 2, 3, 
    • compilation error in LINE II
    • compilation error in LINE I
  24. What will happen when you attempt to compile and run the following code assuming that you will enter following sequence: 6  5 7<enter>?

    #include <iostream>
    #include <string>
    using namespace std;
    int
    main()
    {
      string s;
      cin >> s;      //LINE I
      cout << s << ", " << s << ", " << endl;   //LINE II
      return 0;
    }
    • compilation error in LINE II
    • the program outputs 6, 6, 
    • the program outputs 6 5 7, 6 5 7,
    • the program outputs 6, 5, 7, 6, 5, 7,
    • compilation error in LINE I
  25. What will happen when you attempt to compile and run the following code?

    #include<vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    void
    printer (int i)
    {
      cout << i <<", ";
    }
    int 
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 2, 4, 5};
      vector<int> v1 (2);
      sort (mynumbers2, mynumbers2 + 4);
      sort (mynumbers1, mynumbers1 + 4);   //LINE I
      set_intersection (mynumbers1, mynumbers1 + 3,  mynumbers2,  mynumbers2 + 2, v1.begin());   //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • compilation error in LINE II
    • runtime error at LINE II
    • compilation error in LINE I
    • the program outputs 2, 2, 
    • the program outputs 2, 0, 
    • the program outputs 2, 
  26. What will happen when you attempt to compile and run the following code?

    #include<iomanip>
    #include <iostream>
    
    using namespace std;
    int
    main()
    {
      double goodpi = 3.141593;
      double badpi = 3.5;
      cout << goodpi <<", ";
      cout << scientific;   //LINE I
      cout << setprecision(4);  //LINE II
      cout.unsetf (ios::floatfield);
      cout << goodpi <<", ";
      cout << badpi <<", ";
      return 0;
    }
    • the program outputs 3.14159, 3.142, 3.5, 
    • compilation error in LINE I
    • the program outputs 3.14159, 3.14159, 3.5, 
    • the program outputs 3.14159, 3.142e+000, 3.5, 
    • compilation error in LINE II
  27. What will happen when you attempt to compile and run the following code?

    #include<deque>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    class Pocket
    {
      int value;
    public:
      Pocket (int value):value(value){}
      int getvalue() const
      {
        return value;
      }
      bool operator < (const Pocket & _Right) const
      {
        return value < _Right.value;
      }
    };
    ostream & operator << (ostream & stream, const Pocket & pocket)
     {
       stream << pocket.getvalue();
       return stream;
     }
    void
    printer (Pocket i)
    {
      cout << i << ", ";
    }
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1 };
      deque <Pocket> d1 (mynumbers, mynumbers + 6);
      sort (d1.begin(), d1.end());
      d1.push_back(3); //LINE I
      pair <deque <Pocket>::iterator, deque<Pocket>::iterator> result = equal_range(d1.begin(), d1.end(), Pocket(4)); //LINE II
      for_each(result.first, result.second, printer);
      return 0;
    }
    • the program outputs 4,
    • runtime error at LINE II
    • runtime error at LINE I
    • compilation error in LINE I
    • compilation error in LINE I
    • the program outputs 3,
    • the program outputs 3, 4,
  28. Which container class can be be used an underlying container for stack. Choose all that apply:

    • multimap
    • deque;;;;
    • vector;;;;
    • set;;;;
    • list;;;;
  29. What will happen when you attempt to compile and run the following code?

    #include<vector>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    class Pocket
    {
      int value;
    public:
      Pocket (int value):value(value){}
      int getValue() const
      {
        return value;
      }
      bool operator > (const Pocket & _Right) const
      {
        return value < _Right.value;
      }
    };
    ostream & operator << ( ostream & stream, const Pocket & pocket)
    {
       stream << pocket.getvalue();
       return stream;
    }
    void
    printer (Pocket i)
    {
      cout << i << ", ";
    }
    int
    main()
    {
      int mynumbers[] = {9, 7, 6, 4, 1 }; 
      vector <Pocket> s1 (mynumbers, mynumbers + 5); //LINE I
      sort (s1.begin(), s1.end(),greater<Pocket>());  //LINE II
      for_each(s1.begin (), s1.end (), printer);
      return 0;
    }
    • the program outputs 9, 7, 6, 4, 1,
    • compilation error in LINE II
    • the program outputs 4, 6, 7, 9,
    • compilation error in LINE I
    • the program outputs 1, 4, 6, 7, 9,
  30. What will happen when you attempt to compile and run the following code? Choose all that apply:

    #include<vector> 
    #include<set> 
    #include <iostream> 
    #include <algorithm> 
    
    using namespace std;
    void
    printer(int i)
    {
      cout <<i<<", ";
    }
    int 
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1};
      vector <int> v1(mynumbers, mynumbers + 6);
      sort (v1.begin(), v1.end(), greater <int>());   //LINE I
      for_each(v1.begin(), v1.end(), printer);  //LINE II
      return 0;
    }
    • the program outputs 9, 8, 7, 6, 4,
    • the program outputs 1, 4, 6, 7, 8, 9, 
    • you can call sort function on v1 vector
    • compilation error in LINE I
    • compilation error in LINE II
    • the program outputs 9, 8, 7, 6, 4, 1, 
    • runtime error at LINE I
  31. What will happen when you attempt to compile and run the following code?

    #include<deque> 
    #include <iostream> 
    #include <algorithm> 
    
    using namespace std;
    void
    printer(int i)
    {
      cout <<i<<", ";
    }
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1};
      deque <int> d1(mynumbers, mynumbers + 6);
      d1.push_back(2);   //LINE I
      sort(d1.begin(), d1.end());
      pair<deque<int>:: iterator, deque<int>:: iterator> result = equal_range (d1.begin(), d1.end(), 2);   //LINE II
      for_each(result.first, result.second, printer);
      return 0;
    }
    • compilation error in LINE II
    • the program outputs 0, 
    • the program outputs 1, 2, 
    • the program outputs 2, 
    • compilation error in LINE I
  32. What will happen when you attempt to compile and run the following code?

    #include <iostream> 
    #include <algorithm> 
    #include <vector> 
    
    using namespace std;
    struct Compare
    {
      bool operator ()(int a)
      {
        return (a > 0);
      }                        //LINE I
      operator int () const
      {
        return (4);
      }                        //LINE II
    };
    int 
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1, 4, 4, 9, 7, 2};
      vector<int> v (mynumbers, mynumbers + 11);
      int count = std::count(v.begin(), v.end(), Compare());   //LINE III
      cout << count << endl;
      return 0;
    }
    • runtime error at LINE III
    • the program outputs 1
    • compilation error in LINE II
    • compilation error in LINE III
    • the program outputs 4
    • the program outputs 3
    • compilation error in LINE I
  33. 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 Pocket
    {
      int a;
    public:
      Pocket (int a): a(a){}
      int getA () const
      {
        return a;
      }
      void setA(int a)
      {
        this->a = a;
      }
      bool operator <(const Pocket & b) const
      {
        return a < b.a;
      }
    };
    class Founder
    {
    public:
      Pocket val;
      Founder (Pocket & v):val (v){}
      bool operator ()(Pocket & v)
      {
        return (v.getA() == val.getA());
      }
    };
    int
    main()
    {
      int mynumbers[] = {8, 9, 7, 6, 4, 1};
      vector<Pocket> v1(mynumbers, mynumbers +6);   //LINE I
      set<Pocket> s1 (mynumbers, mynumbers + 6);
      Pocket a (6);
      Founder fonderA (a);
      if(find_if(v1.begin(), v1.end(), fonderA) != v1.end())  //LINE II
        cout << "Found!, ";
      else
        cout <<"Not Found!, ";
      Pocket b (5);
      Founder founderB (b);
      if(find_if (v1.begin(), v1.end(), founderB) != v1.end())  //LINE III
        cout << "Found!";
      else 
        cout <<"Not Found!";
      return 0;
    }
    • compilation error in LINE I
    • the program outputs Found!, Found!
    • the program outputs Found!, Not Found!
    • runtime error at LINE I
    • compilation error in LINE III
    • the program outputs Not Found!, Found!
    • compilation error in LINE II
  34. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm> 
    #include <vector> 
    #include <set> 
    using namespace std;
    void
    printer (int i)
    {
      cout<< i <<", ";
    }
    bool
    classifier(int v)
    {
      return v -4 > 0;
    }
    int
    main()
    {
      int mynumbers[] ={9, 7, 6, 4, 1};
      vector <int>v1 (mynumbers, mynumbers + 5);
      set<int>s1 (mynumbers, mynumbers + 5);
      replace_if(v1.begin(), v1.end(), classifier, 3);  //LINE I
      for_each(v1.begin(), v1.end(), printer);  //LINE II
      return 0;
    }
    • the program outputs 3, 3, 3, 4, 1,
    • the program outputs 9, 7, 6, 3, 3,
    • the program outputs 9, 7, 6, 4, 1,
    • you can call replace_if function with classifier function as 3rd parameter ;;;;
    • compilation error in LINE I
    • compilation error in LINE II
    • runtime erro at LINE II
  35. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <string> 
     
    using namespace std;
    template <typename T> class Pocket
    {
      T value;
    public:
      Pocket(){}
      Pocket(T value);
      T getValue()
      {
        return value;
      }
      void add(T _Right)
      {
        value += _Right;
      }
    };
    template <class T> Pocket <T> ::Pocket (T value):value (value){}
    int
    main()
    {
      Pocket<string> a("Low");
      string n ("End");
      a.add(n);   //LINE I
      cout <<a.getValue() <<a.getValue(); //LINE II
      return 0;
    }
    • compilation error in LINE I
    • program outputs: LowEnd, LowEnd
    • program outputs: LowEnd LowEnd
    • program outputs: LowEndLowEnd
    • program outputs: Low EndLow End
  36. What will happen when you attempt to compile and run the following code?

    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<functional>
    using namespace std;
    int
    main()
    {
     int mynumbers[] = {8,9,7,6,4,1};
     vector<int>v1 (mynumbers, mynumbers+ 6); //LINE I
     cout<<*max_element(v1.begin(),v1.end()) <<", "; //LINE II
     return 0;
    }
    • the program outputs 8,
    • the program outputs 1,
    • you can call max_element function on non ordered v1 vector
    • you can’t call max_element function on non ordered v1 vector
    • the program outputs 9,
    • runtime error at LINE II
    • compilation error in LINE I
  37. What will happen when you attempt to compile and run the following code assuming that file input.txt contains following sequence: i j k ?

    #include<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<iomanip>
    #include<fstream>
    using namespace std;
    void
    printer (char c)
    {
     cout <<setw(2) <<c <<", ";
    }
    
    int
    main()
    {
     ifstream inputfile("input.txt");
     vector <char> v1;
     char c;
     do
     {
      inputfile >> c; //LINE I
      v1.push_back(c);
     }
     while(inputfile.good()); //LINE II
     inputfile.close();
     for_each(v1.begin(), v1.end(), printer);
     return 0;
    }
    • program outputs: i, j, k, (one space after commas)
    • program run forever without output
    • compilation error in LINE II
    • program outputs: i,  j,  k,  k,  (two spaces after commas)
    • program outputs: i, j, k, ,k (one space after commas)
    • compilation error in LINE I
  38. What will happen when you attempt to compile and run the following code?

    #include<iostream>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    void
    printer (int i)
    {
      cout << i <<", ";
    }
    
    int
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 1, 4, 5};
      vector<int>v1 (12);
      vector<int>v2 (7);
      sort(mynumbers1, mynumbers1 + 4);
      copy(mynumbers1, mynumbers1 + 4, v1.begin());
      sort(mynumbers2, mynumbers2 + 4);
      copy(mynumbers2, mynumbers2 + 4, v1.begin() +5); //LINE I
      sort(v1.begin(), v1.end());
      merge(v1.begin() + 4, v1.begin() + 7, v1.begin() + 5, v1.begin() +8, v2.begin()); //LINE II
      for_each(v2.begin(), v2.end(),printer);
      return 0;
    }
    • compilation error in LINE II
    • the program outputs 0, 1, 1, 2, 2, 3, 0, 
    • the program outputs 0, 0, 0, 1, 1, 2, 2, 
    • the program outputs 0, 1, 1, 2, 2, 3,
    • compilation error in LINE I
  39. What will happen when you attempt to compile and run the following code?

    #include<deque>>
    #include<iostream>
    #include<algorithm>
    #include<set>
    
    using namespace std;
    
    int
    main()
    {
      int mynumbers1[] = {3, 9, 0, 2};
      int mynumbers2[] = {6, 1, 4, 5};
      sort(mynumbers1, mynumbers1 + 4);
      sort(mynumbers2, mynumbers2 + 4);
    
      deque<int>d1(mynumbers1, mynumbers1 + 3); //LINE I
      set<int>s1(mynumbers2, mynumbers2 + 3); //LINE II
      sort(d1.begin(), d1.end());
    
      cout << includes(s1.begin(), s1.end(), mynumbers1,
                       mynumbers1 + 2) <<", "<<includes(d1.begin(),
                                                        d1.end(),
                                                        mynumbers1,
                                                        mynumbers1 +
                                                        2) <<endl;
    
    return 0;
    }
    • compilation error in LINE I
    • runtime error at LINE II
    • compilation error in LINE II
    • the program outputs 0, 1,
    • the program outputs 1, 1,
    • the program outputs 1, 0,
  40. What will happen when you attempt to compile and run the following code?

    #include<vector>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    void
    printer(int v)
    {
      cout<<v<<", ";
    }
    struct Sequence
    {
      int start;
      Sequence(int start):start (start){}
      int operator() ()
      {
        return start++; //LINE I
      }
    };
    
    int
    main()
    {
      vector<int> v1(5);
      generate_n(v1.begin(), 5, Sequence(5)); //LINE II
      for_each(v1.begin(), v1.end(), printer);
      return 0;
    }
    • compilation error in LINE I
    • the program outputs 5, 5, 5, 5, 5,
    • compilation error in LINE II
    • the program outputs 5, 6, 7, 8, 9,
    • the program outputs 1, 2, 3, 4, 5,
    • runtime error at LINE II
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments