[CPP] Chapter 1 Assessment

Question 1
/ 1 pts

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;
}
  
  
  
  
  
 
Question 2
/ 1 pts

Which statement 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;
}
  
  
  
  
  
 
IncorrectQuestion 3
/ 1 pts

Which sentences are 100% true about the code below (multiple choice) when control reaches return. Choose all that apply.

#include <vector>
#include <iostream>

using namespace std;

int main ()
{
    vector<int>    v1(10, -1);
    vector<int> v2;
    v2.reserve(10);
    for(unsigned i=0; i < 10; i++)
    {
        v2.push_back(i);
    }
    return 0;
}
  
  
  
  
 
Question 4
/ 1 pts

Which sentence is 100% true about the code below when control reaches return?

#include <vector>
#include <iostream>
#include <stdexcept>

using namespace std;

int main ()
{
    int tab[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    vector<int> v1(tab, tab+10);
    vector<int> v2(v1.size(), 0);
    try
    {
        for(unsigned i=0; i<=v1.size(); ++i)
        {
            int tmp = v1[i];                // LINE I
            v1[i] = v1.at(v1.size()-i);        // LINE    II
            v1.at(i) = tmp;                    // LINE III
            cout<<v1[i] << " ";
        }
    }
    catch(...)
    {
        cout<<"Exception!"<<endl;
    }

    return 0;
}
  
  
  
  
  
 
Question 5
/ 1 pts

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()) << endl;    //LINE II
    return 0;
}
  
  
  
  
  
 
Question 6
/ 1 pts

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;
}
  
  
  
  
  
 
PartialQuestion 7
0.5 / 1 pts

Which methods from the std::deque class can be used to check if there are elements in the container? Choose all that apply.

 

  
  
  
  
  
 
Question 8
/ 1 pts

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(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};
    deque<int> d1(tab, tab+10);
    deque<int> d2;

    while(!d1.empty())
    {
        d2.push_front(d1.back());        //    LINE I
        d1.pop_front();                    //    LINE II
    }
    print(d2.begin(), d2.end())<<": "<<d2.size()<<endl;
    return 0;
}
  
  
  
  
  
 
Question 9
/ 1 pts

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> l1(tab, tab+10);
    deque<A> d1;
    list<A>::iterator it;
    for(it = l1.begin(); it != l1.end(); ++it)
    {
        d1.insert(d1.begin(), it[0]);
    }
    print(d1.begin(), d1.end())<<endl;
    return 0;
}
  
  
  
  
 
Question 10
/ 1 pts

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> l1(tab, tab+10);
    deque<A> d1;
    list<A>::iterator it;

    for(it = l1.begin(); it != l1.end(); ++it)
    {
        d1.insert(d1.begin(), it[0]);
    }
    print(d1.begin(), d1.end())<<endl;
    return 0;
}
  
  
  
  
 
Question 11
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <list>
#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};
    list<int> l1(tab, tab+10);
    list<int> l2;
    list<int>::iterator it;
    for(it = l1.begin(); it != l1.end(); ++it)
    {
        l2.push_back(l1[l1.end()-it-1]);    //LINE I
    }
    print(l2.begin(), l2.end()) << endl;    //LINE II
    return 0;
}
  
  
  
  
  
 
Question 12
/ 1 pts

What will happen when you attempt to compile and run the following code? Choose all that apply.

#include <list>
#include <iostream>
#include <functional>

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};
    list<int> l1(tab, tab+10);
    
    list<int>::const_iterator it = l1.begin()+3;    //LINE I
    l1.erase(it, advance(it,1));                    //LINE II
    print(l1.begin(), l1.end());
    l1.clear();                                        //LINE III
    cout<<l1.size()<<endl;
    return 0;
}
  
  
  
  
  
 
PartialQuestion 13
0.5 / 1 pts
Which methods from the std::list class can delete all the elements from the collection in one call? Choose all that apply.
  
  
  
  
  
 
Question 14
/ 1 pts

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;
}
  
  
  
  
  
  
 
Question 15
/ 1 pts

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.begin(); it != l1.end(); ++it)
    {
        l2.push_front(it);
    }
    print(l2.begin(), l2.end())<<endl;
    return 0;
}
  
  
  
  
 
IncorrectQuestion 16
/ 1 pts

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> l1;
    for(it = v1.begin(); it != v1.end(); ++it)
    {
        l1.push_front(**it);
    }
    print(l1.begin(), l1.end())<<endl;
    return 0;        //LINE I
}
  
  
  
  
  
 
Question 17
/ 1 pts

Which container class can be used as underlying container for priority_queue? Choose all that apply.

 

  
  
  
  
  
 
IncorrectQuestion 18
/ 1 pts
Which of the following examples show the proper way to create a new priority_queue container assuming all necessary declarations have been performed? Choose all that apply.
  
  
  
  
  
 
Question 19
/ 1 pts

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

#include <vector>
#include <queue>
#include <iostream>

using namespace std;

int main()
{
    int t[] = {3, 5, 1, 4, 2};
    vector<int>    v(t, t+5);
    priority_queue<int> q(v.begin(), v.end());
    cout<<q.top()<<" ";
    q.push(0);
    cout<<q.top()<<endl;
    return 0;
}
  
  
  
  
  
 
Question 20
/ 1 pts
Which container class can be used as underlying container for queue? Choose all that apply.
  
  
  
  
  
 
PartialQuestion 21
0.5 / 1 pts

Which of the following examples show the proper way to create a new queue container, assuming all necessary declarations have been performed? Choose all that apply.

 

  
  
  
  
  
 
Question 22
/ 1 pts

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

#include <queue>
#include <deque>
#include <iostream>

using namespace std;

int main()
{
    int t[] = {1, 5, 3, 4, 2};
    deque<int>    d(t, t+5);
    queue<int> q(d);
    cout<<q.front()<<" "<<q.back()<<" ";
    q.pop();
    cout<<q.front()<<" "<<q.back()<<endl;
    return 0;
}
  
  
  
  
  
 
PartialQuestion 23
0.5 / 1 pts
Which container class can be used as underlying container for the stack? Choose all that apply.
  
  
  
  
  
 
Question 24
/ 1 pts

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;
}
  
  
  
  
  
 
Question 25
/ 1 pts

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(); ++it)
    {
        v2.push_back(v1[v1.end()-it-1]);    //LINE I
    }
    print(v2.rbegin(), v2.rend()) << endl;    //LINE II
    return 0;
}
  
  
  
  
  
 
Question 26
/ 1 pts

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 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};
    vector<int> v1(tab, tab+10);
    
    vector<int>::const_iterator it = v1.begin()+3;
    v1.erase(it, it + 1);
    print(v1.begin(), v1.end());
    v1.empty();
    cout<<v1.size()<<endl;
    return 0;
}
  
  
  
  
  
 
IncorrectQuestion 27
/ 1 pts
Which method from the std::vector class can delete an element of the vector using only its position within the vector (not iterator) as parameter?
  
  
  
  
  
 
Question 28
/ 1 pts

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> v1(tab, tab+10);
    vector<int> v2;
    v2.reserve(10);

    while(!v1.empty())
    {
        v2.insert(v2.begin(), v1.pop_back());
    }
    print(v2.rbegin(), v2.rend())<<": "<<v2.size()<<endl;
    return 0;
}
  
  
  
  
  
 
Question 29
/ 1 pts

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;
}
  
  
  
  
  
 
Question 30
/ 1 pts

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;
}

void 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;
}
  
  
  
  
  
Quiz Score: 24 out of 30