STL Sequential Containers M1 Test

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

    #include <deque>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (T & start, T & end)
    {
        for (; start != end; ++start)
           {
               cout << *start << " ";
           }
        return cout;
    }
    int 
    main()
    {
       int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       deque < int >d1 (tab, tab + 10);
       deque < int >d2;
       deque < int >::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 ()) << end1;     //LINE II
       return 0;
    }
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1
    • code will not compile due to error in line LINE II
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10
    • the result of program execution is unpredictable or there might be run time exception
    • code will not compile due to error in line LINE I
    • three months
  2. What will happen when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
        T tmp = start;
        for (; tmp != end; ++tmp)
           {
               cout << *tmp << " ";
           }
        return cout;
    }
    int 
    main()
    {
       int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       vector < int >d1 (tab, tab + 10);
       vector < int >d2;
       v2.reserve (10);
       while (!v1.empty ())
         {
            v2.insert (v2.begin (), v1.pop_back ());
         }
       print (v2.rbegin (), v2.rend ()) << ": " << v2.size () << end1;     
       return 0;
    }
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 20
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 : 20
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 : 10
    • code will not compile
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 10
  3. Which of the following examples shows the proper way to create a new stack container assuming all necessary declarations have been performed. Choose all that apply:

    • stack s;
    • deque d; stack s(d);
    • list l; stack s(l);
    • vector v; stack s(v);
    • deque d; stack s(d.begin(), d.end());
  4. Which methods from std::deque class can be used to check if there are elements in the container. Choose all:

    • size()
    • there is not such method
    • empty()
    • clear()
    • is_empty()
  5. What will happen when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp!= end; ++tmp)
      {
        cout << *tmp << " "; //LINE II
      }
      return cout;
    }
    
    class A
    {
      public:
        int a;
    public:
      A (int a) :a (a)
      {
      }
    };
    ostream & operator<< (const A & o, ostream & c)
    {
      c << o.a;
      return c;
    }
    int
    main()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      vector < A >v1 (tab, tab + 10); //LINE I
      v1.insert (v1.end (), A (0));
      print (v1.begin (), v1.rend ()) << endl;
      return 0;
    }
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 0
    • program will run successfully and display: 0 1 2 3 4 5 6 7 8 9 10
    • code will not compile due to error in line marked LINE II
    • code will not compile due to error in line marked LINE I
    • program will run successfully and display: 0 1 2 3 4 5 6 7 8 9 10
  6. Which sentences are 100% true about the code below when control reaches return:

    #include <vector>
    #include <iostream>
    using namespace std;
    int main()
    {
         vector<int>    v1(4, 3);
         v1.push_back(4);
         for(vector<int>::iterator i = v1.rbegin(); i != v1.rend(); ++i)
         {
             cout << *i << " ";
         }
        cout<< endl;
        return 0;
    }
    • an interchange file
    • value returned by size() is the same for vector v1 and v2
    • an interchange file
    • a source file
  7. What will happen when you attempt to compile and run the following code?

    #include <stack>
    #include <deque>
    #include <iostream>
    
    using namespace std;
    int
    main()
    {
       int t[] = { 1, 5, 3, 4, 2 };
       deque < int >d (t, t + 5);
       stack < int >s (d);
       cout << s.top ()<< " ";
       d.push_front (6);
       cout<< s.top () << endl;
       return 0;
    }
    • program will cause runtime exception
    • program will display: 2 6
    • program will display: 1 1
    • program will display: 1 6
    • program will display: 2 2
  8. What will happen when you attempt to compile and run the following code? Choose all that apply:

    #include <list>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp != end; ++tmp)
      {
        cout << *tmp << " ";
      }
      return cout;
    }
    class A
    {
    public:
      int a;
    public:
      A (int a):a (a)
      {
      }
    };
    
    void
    fill (const int table[], unsigned size, vector < A * >&v)
    {
      for (unsigned i = 0; i < size; ++i)
      {
        v.push_back (new A(table[i]));
      }
    }
    
    ostream & operator<< (ostream & c, const A & o)
    {
      c <<
      o.
      a;
      return c;
    }
    int
    main ()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      vector < A * >v1;
      fill (tab, 10, v1);
      vector < A * >::iterator it;
      list < A > I1;
      for (it = v1.begin (); it != v1.end (); ++it)
      {
        I1.push_front (**it);
      }
      print (I1.begin (), I1.end ()) << endl ;
      return 0; //LINE I
    }
    • when program reaches LINE I there will be 20 objects of type A
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10
    • when program reaches LINE I there will be 10 objects of type A
    • code will not compile
    • program will run successfully but displayed result is unpredictable
  9. What will happen when you attempt to compile and run the following code? 

    #include <list>
    #include <deque>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp != end; ++tmp)
      {
        cout << *tmp << " ";
      }
      return cout;
    }
    class A
    {
      public:
      int a;
      public:
      A (int a):a (a)
      {
      }
      A (const A & a)
      {
      }
    };
    
    ostream & operator<< (ostream & c, const A & o)
    {
      c << o.a;
      return c;
    }
    
    int
    main ()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      list < A > I1 (tab, tab + 10);
      deque < A > d1;
      list < A >::iterator it;
      for (it = I1.begin (); it != I1.end (); ++it)
      {
        d1.insert (d1.begin (), it[0]);
      }
      print (d1.begin (), d1.end ()) << endl;
      return 0;
    }
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1
    • program will run successfully but displayed result is unpredictable
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10
    • code will not compile
  10. What will happen when you attempt to compile and run the following code? 

    #include <list>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp != end; ++tmp)
      {
        cout << *tmp << " ";
      }
      return cout;
    }
    int
    main ()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      list < int > l1 (tab, tab + 10);
      list < int > l2;
      l2.resize (10);
      while (!l1.empty ())
      {
        l2.insert (l2.end (), l1.front ());
        l1.pop_front ();
      }
      print (l2.begin (), l2.end ()) << ": "<< l2.size () << endl;
      return 0;
    }
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 20
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 10
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 : 10
    • none of these
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 : 20
    • code will not compile
  11. Which statements is true about the code below?

    #include <vector>
    #include <iostream>
    using namespace std;
    int main()
    {
      vector<int> v1(4, 3);
      v1.push_back(4);
      for(vector<int>::iterator i = v1.rbegin(); i != v1.rend(); ++i)
     {
       cout << *i << " ";
      }
    cout<< endl;
    return 0;
    }
    • program displays 4 3 3 3 3
    • program displays 4 4 4 4
    • program will not compile
    • program displays 3 3 3 3 4
  12. 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:

    • deque d; priority_queue q(d.begin(), d.end());
    • vector v; priority_queue q(v);
    • deque d; priority_queue q(d);
    • queue q;
    • list 1; priority_queue q(1);
  13. Which container class can be used as underlying container for queue:

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

    #include <list>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp != end; ++tmp)
       {
         cout << *tmp << " ";
       }
      return cout;
      }
    class A
    {
    public:
      int a;
    public:
      A (int a):a (a)
      {
      }
    };
    ostream & operator<< (ostream & c, const A & o)
    {
       c << o.a;
       return c;
    }
    int
    main ()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      list < A > l1 (tab, tab + 10);
      list < A > l2;
      list < A >::iterator it;
      for (it = l1.beign (); it != l1.end (); ++it)
      {
        l2 .push_front (it);
      }
      print (l2.begin (), l2.end ()) << endl;
      return 0;
    }
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10
    • code will not compile
    • program will run successfully but displayed result is unpredictable
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1
  15. What will happen when you attempt to compile and run the following code? 

    #include <deque>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (T const &start, T const &end)
    {
      for (T i = start; i != end; ++i)
      {
        cout << *i << " ";
      }
      return cout;
    }
    int
    main ()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       deque < int > d1 (tab, tab + 10);
    
      deque < int >::const_iterator it = d1.begin () + 3;
      d1.erase (it, it + 1);
      print (d1.begin (), d1.end ());
      d1.clear ();
      cout << d1.size () << endl;
      return 0;
    }
    • program will run successfully and display: 1 2 3 5 6 7 8 9 10 9
    • program will run successfully and display: 1 2 3 6 7 8 9 10 8
    • program will run successfully and display: 1 2 3 6 7 8 9 10 0
    • program will run successfully and display: 1 2 3 5 6 7 8 9 10 0  
    • code will not compile
  16. What will happen when you attempt to compile and run the following code?

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

    #include <vector>
    #include <iostream>
    using namespace std;
    template < typename T > ostream & print (T & start, T & end)
    {
       for (; start != end; ++start)
          {
             cout << *start << " ";
           }
       return cout;
    }
    int 
    main()
    {
        int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        vector < int > v1 (tab, tab + 10);
        vector < int > v2;
        vector < int >::iterator it;
        for (it = v1.begin (); it != v1.end (); ++i)
          { 
             v2.push_back (v1[v1.end () - it - 1]);  //LINE I
           }
        print (v2.rbegin (), v2.rend ()) << endl;    //LINE II
        return 0;
    }
    • program will run successfully and display 10 9 8 7 6 5 4 3 2 1
    • code will not compile due to error in line LINE II
    • the result of program execution is unpredictable or there might be run time exception
    • code will not compile due to error in line LINE I
    • program will run successfully and display 1 2 3 4 5 6 7 8 9 10
  18. Which methods from std::list class can delete all the elements from the collection in collection in one call:

    • empty()
    • there is no such method
    • erase()
    • delete()
    • clear()
  19. What will happen when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    template < typename T > ostream & print (const T & start, const T & end)
    {
      T tmp = start;
      for (; tmp != end; ++tmp)
      {
        cout << *tmp << " ";
      }
      return cout;
    }
    class A
    {
    public:
      int a;
    public:
      A (int a):a (a)
      {
      }
    };
    ostream & operator<< (ostream & c, const A & o)
    {
      c << o.a;
      return c;
    }
    fill (const int table[], unsigned size, vector < A * >&v)
    {
      for (unsigned i = 0; i < size; ++i)
      {
        v.push_back (new A (table[i])); //LINE I
      }
    }
    void
    del (A * p)
    {
    ​​  delete p;
    }
    int
    main()
    {
      int tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      vector < A * > v1;
      fill (tab, 10, v1);
      print (v1.rbegin (), v1.rend ())<< endl; //LINE II
      for_each (v1.begin (), v1.end (), del);
      return 0;
    }
    • code will not compile due to error in line marked LINE II
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1
    • none of these
    • code will not compile due to error in line marked LINE I
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10
  20. What will happen when you attempt to compile and run the following code?

    #include<vector>
    #include<iostream>
    using namespace std;
    template<typename T> ostream & print(const T & start, const T & end)
    {
      T tmp = start;
      for(; tmp != end; ++tmp)
      {
        cout << *tmp <<" "; //LINE II
      }
      return cout;
    }
    class A
    {
    public:
      int a;
    public:
      A (int a):a(a){}
    };
    ostream & operator << (const A & o, ostream & c)
    {
      c<< o.a;
      return c;
    }
    int
    main()
    {
      int tab[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      vector <A> v1 (tab, tab + 10); //LINE I
      v1.insert (v1.end(), A(0));
      print(v1.begin(), v1.end()) <<endl;
      return 0;
    }
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 20
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 10
    • code will not compile
    • program will run successfully and display: 10 9 8 7 6 5 4 3 2 1 : 10
    • program will run successfully and display: 1 2 3 4 5 6 7 8 9 10 : 20
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments