Tuesday, March 9, 2021

Cisco - CPP - Advanced Programming in C++

UGRD-ITE6102-2013T / UGRD-ITE-6104-2023T CPP - Advanced Programming in C++



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

 

code compiles and executes successfully

 

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

program will not compile

 

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

both vectors v1 and v2 have the same capacity

 

size of vector v2 less than 20

 

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

program will run and print output: Exception!

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

code will not compile due to error in line LINE I

 

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 0

 

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

size()

empty()

 

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

program will run successfully and display: 10 10 10 10 10 10 10 10 10 10 : 10

 

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

code will not compile

 

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

code will not compile due to error in line LINE I

code will not compile due to error in line LINE II

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

code will not compile due to error in line LINE I

code will not compile due to error in line LINE I

 

Which methods from the std::list class can delete all the elements from the collection in one call? Choose all that apply.

clear()

erase()

 

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

none of these

 

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

code will not compile

 

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
}

 

when program reaches LINE I there will be 10 objects of type A

 

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

Deque

vector

 

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.

deque<int> d; priority_queue<int> q(d.begin(), d.end());

vector<int> v; priority_queue<int> q(v);

 

 

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

 

program will display: 5 5

 

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

List

deque

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.

deque<int> d; queue<int> q(d);

queue<int> q;

 

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

program will display: 1 2 5 2

 

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

 

List

vector

deque

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 display: 2 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(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;
}

code will not compile due to error in line LINE II

 

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

program will run successfully and display: 1 2 3 5 6 7 8 9 10 9

 

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?

 

clear()

 

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

code will not compile

 

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

code will not compile due to error in line marked LINE II

 

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

none of these

 

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

 

#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);
    set<int> set1(v.begin(),v.end());
    set<int, greater<int> > set2(v.begin(), v.end()); //LINE I
    for(set<int, int>::iterator i=set2.begin();i!= set2.end(); i++)
        cout<<*i<<" ";
    for(set<int>::iterator i=set1.begin();i!= set1.end(); i++)
        cout<<*i<<", ";
    cout<<endl;
    return 0;
}

 

program outputs: 9 5 4 3 2 1 0 0, 1, 2, 3, 4, 5, 9,

code compiles and executes successfully

 

 

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

 

#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,

the size of the s1 set is 8

 

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

 

#include <iostream>
#include <set>
#include <vector>
using namespace std;
class A {
    int a;
public:
    A(int a):a(a){}
    int getA() const { return a;}
};

int main(){
    A mynumbers[] =    { 3, 9, 0, 2, 1, 4, 5 };
    vector<A>    v(mynumbers, mynumbers+7);
    set<A> s1(v.begin(),v.end()); //LINE I
    s1.insert(v.begin(),v.end());
    s1.erase(s1.lower_bound(3),s1.upper_bound(6));
    for(set<A>::iterator i=s1.begin();i!= s1.end(); i++) {
        cout<<i->getA()<<" ";
    }
    cout<<endl;
    return 0;
}

 

compilation fails because there is no bool operator < defined in class A

 

What happens 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, 4, 5, 9,

 

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,

 

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

 

#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 };
    vector<int> v(mynumbers, mynumbers+7);
    set<int> s(v.begin(),v.end());
    for(int i=10; i>0; i  ) {
        int x = s.top(); //LINE I
        s.pop();         //LINE II
        v.push_back(i+x);//LINE III
    }
    print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl;
    return 0;
}

 

compilation error in LINE II

compilation error in LINE I

 

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> s(v.begin(),v.end());
    s.insert(v.begin(),v.end());
    v.push_back(5);
    s.insert(8);
    pair<set<int>::iterator,set<int>::iterator> range;
    range = s.equal_range(5);//LINE I
    cout<<*range.first<<", "<<*range.second<<endl;
    return 0;
}

 

 

The output will be: 5, 8

 

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

 

The output will be: 0, 1, 2, 3, 4, 5, 9, 9, 9, 5, 4, 3, 2, 1, 0,

 

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,

 

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

 

#include <iostream>
#include <set>
#include <vector>
using namespace std;
class A {
    int a;
public:
    A(int a):a(a){}
    int getA() const { return a;}
    operator int() const { return a;}
    bool operator < (const A & b) const { return b.a<a;} //LINE I
};

int main(){
    int mynumbers[] =    { 3, 9, 0, 2, 1, 4, 5 };
    set<A>    s(mynumbers, mynumbers+7);            //LINE II
    multiset<A> s1(s.begin(),s.end());    //LINE III
    s1.insert(s.begin(),s.end());
    s1.erase(s1.lower_bound(5),s1.upper_bound(2));//LINE IV
    multiset<A>::iterator i=s1.begin();    
    for( ;i!= s1.end(); i++)
    {
        cout<<i->getA()<<", ";
    }
    cout<<endl;
    return 0;
}

 

 

The output will be: 9, 9, 1, 1, 0, 0,

 

What happens 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, 9,

 

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[] =    { 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;
}

 

program outputs: 0, 1, 2, 3, 4, 5, 6, 9,

 

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

 

compilation error in LINE I

 

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[] =    { 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, 5,

 

What happens 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, 8};
    vector<int> v(mynumbers, mynumbers+7);
    map<int,string> m;
    for(map<int, string>::iterator i=m.begin();i!= m.end(); i++)
        cout<<*i<<", ";//LINE I
    return 0;
}

compilation error in LINE I

 

What happens 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<9; 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;
}

the exception will be thrown at line LINE I

 

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

program outputs: zero, one, two, three, four, five, nine, , 8

 

What changes, introduced independently, will allow the code to compile and display "zero, one, two, five, 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;}
    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;
}

code compiles and executes successfully with the expected result

change LINE II to m.erase(m.lower_bound(3), m.upper_bound(5));

(wrong)

 

What happens 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: 0, 1, 2, 3, 4, 5, 9,

 

            What happens 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", "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

 

What happens 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 1
    for (; i != range.second; i++) {
        cout << i->first << ", ";
    }
    return 0;
}

program outputs: 3,

 

What happens 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: 0, 1, 2, 3, 4, 5, 9,

 

What will be the output of the program 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 };
    string words[] = {"three", "nine", "zero", "two", "one"};
    multimap<int,string> m;
    for(int i=0; i<4; i++) {
        m.insert(pair<int,string>(mynumbers[i], words[i]));
        m.insert(pair<int,string>(mynumbers[i], words[i]));
    }
    m[0]="ten";        //LINE I
    for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++)
        cout<<i->second<<", ";
    return 0;
}

compilation error in LINE I

 

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

program outputs: found expected zero, one, two, three, four, five, nine, 7

 

What changes introduced independently will allow the code to compile and display “zero, one, 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;}
    //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<<endl;
    return 0;
}

bool operator < (const A & b) const { return b.a<a;} inserted at LINE I

(wrong)

What happens 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", "fiveandhalf"};
    multimap<int, string> m;
    for (int i = 0; i < 8; i++)
        m.insert(pair<int, string>(mynumbers[i], words[i]));
    if (m.count(5) == 1)
        m.erase(2);
    for (multimap<int, string>::iterator i = m.begin(); i != m.end(); i++)
        cout << i->first << ", ";
    return 0;
}

program outputs: 0, 1, 2, 3, 4, 5, 5, 9,

 

What happens 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", "five"};
    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;

compilation error in LINE I

 

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 II
    range = m.equal_range(4);
    for (multimap<int, string>::iterator i = range.first; i != range.second; i++) {
        cout << i->first << ", ";
    }
    return 0;
}

4,

 


No comments:

Post a Comment