CPP : C++ Certified Professional Programmer : Part 09

  1. Which stack initialization (line numbers) are correct? Choose all that apply.

    #include <iostream>
    #include <deque>
    #include <list>
    #include <stack>
    #include <vector>
    using namespace std;

    int main()
    {
    deque<int> mydeck;
    list<int> mylist;
    vector<int> myvector;
    stack<int> first;// Line I
    stack<int> second(mydeck);// Line II
    stack<int> third(second);// Line III
    stack<int, list<int> > fourth(mylist);// Line IV
    stack<int, vector<int> > fifth(myvector);// Line V
    return 0;
    }

    • line I
    • line II
    • line III
    • line IV
    • line V
  2. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int main() {
    int t[]={3,2,4,1,5,6,10,8,7,9};
    vector<int> v1(t, t+10);
    for_each(v1.begin(), v1.end(), bind2nd(plus<int>(), 1));
    for_each(v1.rbegin(), v1.rend(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 3 2 4 1 5 6 10 8 7 9
    • 4 3 5 2 6 7 11 9 8 10
    • 9 7 8 10 6 5 1 4 2 3
    • 10 8 9 11 7 6 2 5 3 4
    • compilation error
  3. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    class B { int val;
    public:
    B(int v):val(v){}
    int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
    ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int main() {
    B t1[]={3,2,4,1,5};
    B t2[]={5,6,8,2,1};
    vector<B> v1(10,0);
    sort(t1, t1+5);
    sort(t2, t2+5);
    set_symmetric_difference(t2,t2+5,t1,t1+5,v1.begin());
    for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 6 8 3 4 0 0 0 0 0 0
    • 3 4 0 0 0 0 0 0 0 0
    • 6 8 0 0 0 0 0 0 0 0
    • compilation error
    • 3 4 6 8 0 0 0 0 0 0
  4. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    class B { int val;
    public:
    B(int v):val(v){}
    int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
    ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };
    int main() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    deque<B> d1(t, t+10);
    sort(d1.begin(), d1.end());
    deque<B>::iterator it = upper_bound(d1.begin(), d1.end(), B(4));
    for_each(it, d1.end(), Out<B>(cout)); cout<<endl;
    return 0;
    }

    Program outputs:

    • 5 6 7 8 9 10
    • 4 5 6 7 8 9 10
    • 6 7 8 9 10
    • 1 2 3 4 5
    • 1 2 3 4
  5. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int Add(int a, int b) {
    return a+b;
    }

    int main() {
    int t[]={1,2,3,4,5,6,7,8,9,10};
    vector<int> v1(t, t+10);
    vector<int> v2(10);
    transform(v1.begin(), v1.end(), v2.begin(), bind2nd(ptr_fun (Add),1));
    vector<int>::iterator it = find_if(v2.begin(), v2.end(),bind2nd(equal_to<int>(),10));
    cout<<*it<<endl;
    return 0;
    }

    Program outputs:

    • false
    • true
    • 10
    • 0
    • compilation error
  6. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <list>
    using namespace std;
    int main(){
    int t[] ={ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
    list<int>v(t, t+10);
    set<int> s1(v.begin(),v.end());
    if (s1.count(3) == 2) {
    s1.erase(3);
    }
    for(set<int>::iterator i=s1.begin();i!= s1.end(); i++) {
    cout<<*i<<” “;
    }
    return 0;
    }

    • program outputs: 1 2 3 4 5
    • program outputs: 1 2 4 5
    • program outputs: 1 1 2 2 3 4 4 5 5
    • program outputs: 1 1 2 3 3 4 4 5 5
    • compilation error
  7. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <set>
    using namespace std;

    void myfunction(int i) {
    cout << ” ” << i;
    }

    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    set<int> s1(t, t+10);
    vector<int> v1(s1.rbegin(), s1.rend());
    swap_ranges(s1.begin(), s1.end(), v1.begin());
    for_each(v1.begin(), v1.end(), myfunction);
    for_each(s1.begin(), s1.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
    • 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
    • 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1
  8. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <deque>
    using namespace std;

    void myfunction(int i) {
    cout << ” ” << i;
    }

    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    deque<int> d1(t, t+10);
    vector<int> v1(d1.rbegin(), d1.rend());
    sort(d1.begin(), d1.end());
    swap_ranges(v1.begin(), v1.end(), d1.begin());
    for_each(v1.begin(), v1.end(), myfunction);
    for_each(d1.begin(), d1.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
    • 1 2 3 4 5 6 7 8 9 10 1 3 8 7 4 2 6 9 5 10
    • 1 3 8 7 4 2 6 9 5 10 1 2 3 4 5 6 7 8 9 10
  9. What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 end<enter>?

    #include <iostream>
    #include <string>
    #include <list>
    #include <algorithm>

    using namespace std;

    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) {out<<val<<” “; } };

    int main ()
    {
    list<int> l;
    for( ; !cin.bad() ; )
    {
    int i;
    cin>>i;
    l.push_back(i);
    }
    for_each(l.begin(), l.end(), Out<int>(cout));
    return 0;
    }

    Program will output:

    • 1 2 3
    • 1 2 3 end
    • 1
    • compilation error
    • program runs forever without output
  10. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int main() {
    int t[]={3,2,4,1,5,10,9,7,8,6};
    vector<int> v1(t,t+10);
    sort(v1.begin(), v1.end(), greater<int>());
    cout<<min_element(v1.begin(), v1.end());
    return 0;
    }

    Program outputs:

    • 3
    • 1
    • 6
    • 10
    • compilation error
  11. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int main() {
    int t1[]={3,2,4,1,5};
    int t2[]={5,6,8,2,1};
    vector<int> v1(10);
    sort(t1, t1+5);
    sort(t2, t2+5);
    set_union(t1,t1+5,t2,t2+5,v1.begin());
    for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 3 2 4 1 5 6 8 2 1 0
    • 1 2 3 4 5 6 8 2 1 0
    • 1 1 2 2 3 4 5 5 6 8
    • 1 2 3 4 5 6 8 0 0 0
    • compilation error
  12. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    class B { int val;
    public:
    B(int v):val(v){}
    int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
    ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };
    int main() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    vector<B> v1(t, t+10);
    sort(v1.begin(), v1.end(), greater<B>());
    for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 8 10 5 1 4 6 2 7 9 3
    • 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 10 9 8 7 6 5 4 3 2 1
  13. What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: true false<enter>?

    #include <iostream>
    #include <string>
    using namespace std;

    int main ()
    {
    bool a,b;
    cin>>boolalpha>>a>>b;
    cout<<a<<b<<endl;
    return 0;
    }

    Program will output:

    • truefalse
    • true0;
    • 1false
    • 10
    • none of these
  14. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <deque>
    #include <vector>
    using namespace std;
    bool identical(int a, int b) {
    return b == 2*a?true:false;
    }
    int main() {
    int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
    int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};
    vector<int> v1(t, t + 15);
    deque<int> d1(u, u + 15);

    pair<deque<int>::iterator, vector<int>::iterator > result;
    result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I
    if (result.first == d1.end() && result.second == v1.end()) {//Line II
    cout<<“Identical\n”;
    } else {
    cout<<“Not identical\n”;
    }
    return 0;
    }

    Program outputs:

    • Identical
    • Not identical
    • compilation error at line marked I
    • compilation error at line marked II
  15. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    template <class T>
    class A {
    T_v;
    public:
    A() {}
    A(T v): _v(v){}
    friend ostream & operator<<(ostream & c, const A<T> & v);
    };

    template <class T>
    ostream & operator<<(ostream & c, const A<T> & v) {
    c<<v._v;return c; }

    int main()
    {
    A<int>a(10);
    cout<<a<<endl;
    return 0;
    }

    • program will display:10
    • program will not compile
    • program will compile
    • program will run without output
  16. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    class B { int val;
    public:
    B(int v):val(v){} B(){}
    int getV() const {return val;} };
    ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;}
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };
    int main() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    deque<B> d1(t, t+10);
    deque<B>::iterator it = lower_bound(d1.begin(), d1.end(), 4);
    for_each(it, d1.end(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 8 10 5 1 4 6 2 7 9 3
    • 4 5 6 7 8 9 10
    • 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 1 2 3 4
  17. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    class A {
    int a;
    public:
    A(int a) : a(a) {}
    int getA() const { return a; } void setA(int a) { this?>a = a; }
    bool operator==(A & b) { return a == b.a; }
    };
    struct Compare{
    bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
    };
    int main () {
    int t[] = {1,2,3,4,5,1,2,3,4,5};
    vector<A> v (t,t+10);
    vector<A>::iterator it;
    A m1[] = {A(1), A(2), A(3)};
    it = search (v.begin(), v.end(), m1, m1+3, Compare());
    cout << “First found at position: ” << it?v.begin() << endl;
    return 0;
    }

    Program outputs:

    • First found at position: 5
    • First found at position: 0
    • First found at position: 7
    • compilation error
    • First found at position: 10
  18. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
    class B { int val;
    public:
    B(int v=0):val(v){}
    int getV() const {return val;}
    operator int () const { return val;} };

    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; } };

    int main() {
    B t[]={3,2,4,1,5,6,10,8,7,9};
    vector<B> v1(t, t+10);
    for_each(v1.begin(), v1.end(), bind1st(plus<B>(), 1));
    for_each(v1.rbegin(), v1.rend(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 3 2 4 1 5 6 10 8 7 9
    • 4 3 5 2 6 7 11 9 8 10
    • 9 7 8 10 6 5 1 4 2 3
    • 10 8 9 11 7 6 2 5 3 4
    • compilation error
  19. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <map>
    using namespace std;

    void myfunction(pair<int, int> i) {
    cout << ” ” << i.first;
    }

    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    map<int, int> m;
    for(int i=0; i < 10; i++) {
    m[i]=t[i];
    }

    for_each(m.begin(), m.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 10 5 9 6 2 4 7 8 3 1
    • 0 1 2 3 4 5 6 7 8 9
    • 9 8 7 6 5 4 3 2 1 0
    • 1 3 8 7 4 2 6 9 5 10
    • compilation error
  20. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <set>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    template<class 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 start++; } };

    int main() {
    vector<int> v1(10);
    generate_n(v1.begin(), 10, Sequence(1));
    random_shuffle(v1.rbegin(), v1.rend());
    sort(v1.begin(), v1.end(), great<int>());
    for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 8 10 5 1 4 6 2 7 9 3
    • 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 10 9 8 7 6 5 4 3 2 1
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments