CPP : C++ Certified Professional Programmer : Part 04

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

    #include <list>
    #include <iostream>
    using namespace std;
    bool mycomparison (int first, int second){return first>second;}
    template<class T>
    void print(T start, T end) {
    while (start != end) {
    std::cout << *start << ” “; start++;
    }
    }
    int main()
    {
    int t1[] ={ 1, 7, 8, 4, 5 };
    list<int> l1(t1, t1 + 5);
    int t2[] ={ 3, 2, 6, 9, 0 };
    list<int> l2(t2, t2 + 5);
    l1.sort(mycomparison);
    l2.sort(mycomparison);
    l1.merge(l2,mycomparison);
    print(l1.begin(), l1.end());
    print(l2.begin(), l2.end()); cout<<endl;
    return 0;
    }

    • program outputs: 9 8 7 6 5 4 3 2 1 0
    • program outputs: 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
    • program outputs: 9 8 7 6 5 4 3 2 1 0 9 6 3 2 0
    • program outputs: 0 1 2 3 4 5 6 7 8 9 0 2 3 6 9
    • program outputs: 0 1 2 3 4 5 6 7 8 9
  2. What will happen when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    class C {};

    template <class T>
    class A {
    T _v;
    public:
    A() {}
    A(T v): _v(v){}
    T getV() { return _v; }
    void add(T a) { _v+=a; }
    };

    int main()
    {
    A<int> b;
    A<C> a;
    a.add(C());
    cout << b.getV() <<endl;
    return 0;
    }

    • program will display:0
    • program will not compile
    • program will compile
    • program will cause runtime exception
  3. 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;}
    B operator +(const B &b )const { return B(val + b.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<<” “; } };

    template<typename A> struct Add : public binary_function<A, A, A> {
    A operator() (const A & a, const A & b) const { return a+b; }};
    int main() {
    int t[]={1,2,3,4,5,6,7,8,9,10};
    vector<B> v1(t, t+10);
    vector<B> v2(10);
    transform(v1.begin(), v1.end(), v2.begin(), bind1st(ptr_fun (Add<B>()), 1));
    for_each(v2.rbegin(), v2.rend(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

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

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

    Program will output:

    • truetrue
    • falsefalse
    • 11
    • 00
    • none of these
  5. What happens when you attempt to compile and run the following code?

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

    int main() {
    int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
    vector<int> v1(t, t + 15);
    set<int> s1(t, t + 15);

    pair<set<int>::iterator, vector<int>::iterator > resultSet = equal(s1.begin(), s1.end(), v1.begin());
    cout<<*resultSet.first<<” “<<*resultSet.second<<endl;

    return 0;
    }

    Program outputs:

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

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

    int main() {
    string t[]={“aaa”,”Aaa”, “aAa”,”aaA”,”bbb”,”Bbb”, “bBb”, “bbB”};
    vector<string> v1(t, t+8);
    sort(v1.begin(), v1.end());
    for_each(v1.begin(), v1.end(), Out<string>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • Aaa Bbb aAa aaA aaa bBb bbB bbb
    • Aaa aAa Bbb aaA aaa bBb bbB bbb
    • bBb bbB bbb Aaa aAa Bbb aaA aaa
    • Aaa aAa bBb bbB bbb Bbb aaA aaa
    • compilation error
  7. 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<<” “;
    }
    };
    struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() { return 10*(1+(start++ %3)); }
    };
    int main() {
    vector<int> v1(10);
    generate(v1.begin(), v1.end(), Sequence(1));
    unique(v1.begin(),v1.end());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
    return 0;
    }

    Program outputs:

    • 20 30 10 20 30 10 20 30 10 20
    • 20 30 10
    • 30 10 20
    • compilation error
  8. 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 = find_end (v.begin(), v.end(), m1, m1+3, Compare());
    cout << “Found at position: ” << it?v.begin() << endl;
    return 0;
    }

    • program outputs: Found at position: 5
    • program outputs: Found at position: 0
    • program outputs: Found at position: 7
    • compilation error
    • program outputs: Found at position: 10
      ***/
  9. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <deque>
    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; }
    };
    struct Even {
    bool operator ()(const A & a, const A &b) {
    return (a.getA() % 2)==b.getA() % 2;
    }
    };
    int main () {
    int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
    deque<int> d (t,t+15);
    deque<int>::iterator it = search_n(d.begin(), d.end(), 3, 2, Even());
    cout<< it?d.begin()<<endl;
    return 0;
    }

    Program outputs:

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

    #include <deque>
    #include <iostream>
    #include <algorithm>
    #include <set>
    using namespace std;
    template<class T>struct Out {
    ostream & out;
    Out(ostream & o): out(o){}
    void operator() (const T & val ) { out<<val<<” “; }
    };
    int main() {
    char s[]={“qwerty”};
    char t1[]={“ert”};
    char t2[]={“ERT”};
    sort(s, s+6);
    cout<<includes(s,s+6, t1,t1+3)<<” “<<includes(s,s+6, t2,t2+3)<<endl;
    return 0;
    }

    Program outputs:

    • 0 0
    • 0 1
    • 1 0
    • 1 1
  11. What happens when you attempt to compile and run the following code?

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

    void myfunction(int i) {
    cout << ” ” << i;
    }
    int add (int a, int b) { return a+b; }

    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    vector<int> v1(t, t+10);
    set<int> s1(t, t+10);
    deque<int> d1;
    d1.resize(s1.size());
    transform(s1.begin(), s1.end(), v1.begin(), d1.begin(), add);

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

    Program outputs:

    • 0 0 0 0 0 0 0 0 0 0
    • 11 7 12 10 7 10 14 16 12 11
    • compilation error
    • runtime exception
    • 20 10 18 12 4 8 14 16 6 2
  12. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <string>

    using namespace std;

    template <class T>
    class A {
    T _v;
    public:
    A() {}
    A(T v): _v(v){}
    T getV() { return _v; }

    void add(T & a);

    void add(string & a);

    };

    template<class T>
    void A<T>::add(T & a) { _v+=a; }

    void A<string>::add(string & a) {
    _v.insert(0, a);
    }

    int main()
    {
    A<string> a(“Hello”);
    string s(” world!”);
    a.add(s);
    cout << a.getV() <<endl;
    return 0;
    }

    • program will display: Hello world!
    • compilation error
    • program will display: world!Hello
    • program will run without any output
  13. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int main(){
    int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
    multiset<int> s1(t,t+10);
    s1.insert(s1.find(7), 3);
    for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
    cout<<*i<<” “;
    }
    return 0;
    }

    • program outputs: 0 1 2 3 3 4 5 6 7 8 9
    • program outputs: 0 1 2 3 4 5 6 7 8 9
    • program outputs: 0 1 2 3 4 5 6 7 3 8 9
    • program outputs: 0 1 2 3 4 5 6 3 7 8 9
    • runtime exception
  14. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <vector>
    #include <iostream>
    using namespace std;
    template<typename T>
    int calculate(T start, T end)
    {
    int s = 0;
    while (start != end)
    s+= *start; start++;return s;
    }
    int main ()
    {
    int t[] = {1, 2 ,3 ,4 ,5, 6 , 7, 8 , 9, 10};
    vector<int>v1(t, t+5);
    deque<int>d1(t+5, t+10);
    cout<<calculate(t,t+10)<<” “;
    cout<<calculate(v1.begin()+1,v1.end()?2)<<” “;
    cout<<calculate(d1.rbegin()+1,d1.rend()?2)<<” “;
    cout<<calculate(t[0],t[10])<<” “;
    cout<<endl;
    return 0;
    }

    • compilation error
    • runtime exception
    • program outputs 55 5 17 55
    • program outputs 55 5 17 0
  15. 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<<” “; } };

    struct Add {
    int operator()(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(), bind1st(1,Add()));
    for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    int main(){
    int myints[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
    vector<int>v(myints, myints+10);
    set<int> s1(v.begin(),v.end());
    set<int, greater<int> > s2(v.begin(), v.end());
    for(set<int>::iterator i=s1.begin();i!= s1.end(); i++) {
    cout<<*i<<” “;
    }
    for(set<int, greater<int> >::iterator i=s2.begin();i!= s2.end(); i++) {
    cout<<*i<<” “;
    }
    cout<<endl;
    return 0;
    }

    • program outputs: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
    • program outputs: 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
    • program outputs: 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
    • program outputs: 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9
  17. 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 t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
    vector<int>v(t, t+10);
    set<int> s1(v.begin(),v.end());
    s1.insert(v.begin(),v.end());
    pair<set<int>::iterator,set<int>::iterator> range;
    range = s1.equal_range(6);
    cout<<*range.first<<” “<<*range.second<<endl;
    return 0;
    }

    The output will be:

    • 6 6 
    • 5 7 
    • 6 7
    • 1 5 
    • 6 5
  18. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>

    using namespace std;

    void print(int v) { cout<<v<<” “; }
    struct Sequence {
    int start;
    Sequence(int start):start(start){}
    int operator()() {
    return 10*(1+(start++ %3));
    }
    };
    int main() {
    vector<int> v1(10);
    generate_n(v1.begin(), 10, Sequence(1));
    remove(v1.begin(), v1.end(), 10);
    for_each(v1.begin(), v1.end(), print);cout<<endl;
    return 0;
    }

    Program outputs:

    • 20 30 10 20 30 10 20 30 10 20
    • 20 30 20 30 20 30 20
    • 20 30 20 30 20 30 20 30 10 20
    • compilation error
  19. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    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[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};
    deque<B> d1(t, t+10);
    sort(d1.begin(), d1.end(), greater<B>());
    pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(), B(20), greater<B>());
    for_each(result.first, result.second, Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 30 30 30 20 20 20 20 10 10 10
    • 20 20 20 20
    • 30 20 20 20 10
    • 20 20 20 20 10
    • 30 20 20 20 20 10
  20. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <set>
    #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());
    set<B> s1(t,t+10);
    cout<<binary_search(s1.begin(),s1.end(), B(4))<<” “<<binary_search(d1.begin(),d1.end(), B(4))<<endl;
    return 0;
    }

    Program outputs:

    • 1 0
    • 1 1
    • 0 0
    • 0 1
    • compilation error