CPP : C++ Certified Professional Programmer : Part 05

  1. 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):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 t[]={3,2,4,1,5,10,9,7,8,6};
    vector<B> v1(t,t+10);
    cout<<*max_element(v1.begin(), v1.end(), greater<B>());
    cout<<endl;
    return 0;
    }

    Program outputs:

    • 3
    • 1
    • 6
    • 10
    • compilation error
  2. What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: one two three<enter>?

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

    int main ()
    {
    string a;
    getline(cin, a);
    cout<<a<<endl;
    return 0;
    }

    Program will output:

    • one
    • one two three
    • runtime exception
    • compilation error
    • the result is unspecified
  3. 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 start++; }
    };
    struct Odd { bool operator()(int v) { return v%2==0; } };
    int main() {
    vector<int> v1(10);
    vector<int> v2(10);
    generate(v1.begin(), v1.end(), Sequence(1));
    stable_partition(v1.begin(),v1.end(), Odd());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
    return 0;
    }

    Program outputs:

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

    #include <iostream>
    using namespace std;
    int main()
    {
    cout.setf(ios::oct, ios::basefield);
    cout<<100<<” “;
    cout.setf(ios::showbase);
    cout<<100<<” “;
    return 0;
    }

    Program outputs:

    • 144 0144
    • 144 0x64
    • 0x144 0144
    • 0144 100
    • compilation error
  5. 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 start++; } };
    int main() {
    vector<int> v1(10);
    vector<int> v2(10);
    generate(v1.begin(), v1.end(), Sequence(1));
    random(v1.begin(),v1.end());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
    return 0;
    }

    Program outputs:

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

    #include <deque>
    #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<<” “; } };

    struct Add : public binary_function<int, int, int> {
    int operator() (const int & a, const int & b) const {
    return a+b;
    }
    };
    int main() {
    int t[]={1,2,3,4,5,6,7,8,9,10};
    deque<int> d1(t, t+10);
    deque<int> d2(10);
    transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add(), 1));
    for_each(d2.rbegin(), d2.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
  7. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <iostream>
    #include <algorithm>
    #include <set>
    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 t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    B t1[]={B(1),B(2),B(3),B(4)};
    deque<B> d1(t, t+10);
    set<B> s1(t, t+10);
    sort(d1.begin(), d1.end());
    cout<<includes(d1.begin(),d1.end(), t1,t1+4)<<” “<<includes(s1.begin(),s1.end(), t1,t1+4)
    <<endl;
    return 0;
    }

    Program outputs:

    • 1 1
    • 1 0
    • 0 1
    • 0 0
    • compilation error
  8. 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<<” “; } };

    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(ptr_fun (Add()), 1));
    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
  9. 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;} };
    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 t[]={3,2,4,1,5,6,10,8,7,9};
    vector<B> v1(t, t+10);
    transform(v1.begin(), v1.end(), v1.begin(), bind2nd(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
  10. 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=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};
    deque<B> d1(t, t+10);
    deque<B> d2(10);
    transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add<B>(), 1));
    for_each(d2.rbegin(), d2.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
  11. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    int main ()
    {
    std::vector<int>v1;
    for(int i = 0; i<10; i++) {v1.push_back(i); }
    std::vector<int> v2(v1.begin()+2, v1.end()?2);
    std::vector<int>::iterator it = v2.begin();
    for( ; it != v2.end(); it++) {std::cout<<*it++<<” “; }std::cout<<std::endl;
    return 0;
    }

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

    #include <iostream>
    #include <map>
    using namespace std;
    int main() {
    int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
    string s[] = { “one”, “one”, “two”, “two”, “three”,”three”, “four”, “four”, “five”, “five”};
    map<int, string> m;
    for (int i = 0; i < 10; i++) {
    m.insert(pair<int, string>(t[i], s[i]));
    }
    if (m.count(3) == 2) {
    m.erase(3);
    }
    for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i?>first << ” “;
    }
    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
    • program outputs: one two three four five
  13. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    class A {
    public:
    virtual int f() { return 10; }
    virtual ~A(){}
    };
    class B: public A {
    int f() {return 11; }
    virtual ~B(){}
    };
    int main (){
    std::vector<A*>v1;
    for(int i = 10; i>0; i??)
    {
    i%2>0?v1.push_back(new A()):v1.push_back(new B());
    }
    std::vector<A*>::iterator it = v1.begin();
    while(it != v1.end())
    {
    std::cout<<v1.back()?>f()<<” “;
    v1.pop_back();++it;
    }
    return 0;
    }

    • destructor of class A will be called
    • destructor of class B will be called
    • code will not compile
    • program outputs 10 11 10 11 10
    • program outputs 10 11 10 11 10 11 10 11 10 11
  14. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    template<class A>
    void f(A &a)
    {
    cout<<1<<endl;
    }

    void f(int &a)
    {
    cout<<2<<endl;
    }

    int main()
    {
    int a = 1;
    f(a);
    return 0;
    }

    • program displays: 1
    • program displays: 2
    • compilation error
    • runtime exception
  15. What will be 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 second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
    string first[] = {“three”, “four”, “two”, “one”, “six”,”five”, “seven”, “nine”,”eight”,”zero”};
    multimap<int,string> m;
    for(int i=0; i<10; i++) {
    m.insert(pair<int,string>(second[i],first[i]));
    }
    m[0]=”ten”;
    m.insert(pair<int,string>(1,”eleven”));
    for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++) {
    cout<<i?>second<<” “;
    }
    return 0;
    }

    • zero one two three four five six seven eight nine
    • ten one two three four five six seven eight nine
    • zero eleven two three four five six seven eight nine
    • ten eleven two three four five six seven eight nine
    • compilation error
  16. 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=0):val(v){}
    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() {
    B t1[]={3,2,4,1,5};
    B t2[]={6,10,8,7,9};
    vector<B> v1(5);
    transform(t1,t1+5,t2,v1.rbegin(), plus<B>());
    for_each(v1.rbegin(), v1.rend(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

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

    int main () {
    int t[] = {1,2,3,4,5,1,2,3,4,5};
    vector<int> v (t,t+10);
    vector<int>::iterator it;
    int m1[] = {1, 3, 2};
    it = find_first_of (v.begin(), v.end(), m1, m1+3);
    cout << “First found at position: ” << it?v.begin() << endl;
    return 0;
    }

    • program outputs: First found at position: 5
    • program outputs: First found at position: 0
    • program outputs: First found at position: 6
    • program outputs: First found at position: 1
    • program outputs: First found at position: 10
  18. What happens when you attempt to compile and run the following code?

    #include <list>
    #include <iostream>
    using namespace std;
    template<class T>
    void print(T start, T end) {
    while (start != end) {
    std::cout << *start << ” “; start++;
    }
    }
    int main()
    {
    int t1[] ={ 1, 2, 3, 4, 5};
    list<int> l1(t1, t1 + 5);
    l1.remove(2);
    print(l1.begin(), l1.end()); cout<<endl;
    return 0;
    }

    • program outputs: 1 2 4 5
    • program outputs: 3 4 5
    • program outputs: 1 3 4 5
    • program outputs: 4 5
  19. What happens when you attempt to compile and run the following code? Choose all that apply.

    #include <deque>

    #include <vector>

    #include <iostream>

    using namespace std;

    class A
    {
    int a;
    public:
    A(int a) {this?>a = a; c++;}
    A(const A & a) {this?>a = a.a; c++;}
    ~A() { c??;}
    static int c;
    };
    int A::c(0);
    int main ()
    {
    A* t[] = {new A(1), new A(2), new A(3),new A(4), new A(5)};
    vector<A*>v1(t, t+10);
    deque<A*>d1(v1.begin(), v1.end());
    d1.clear();
    v1.clear();
    cout<<A::c<< endl;
    return 0;
    }

    • there are 15 A objects created,
    • there are 5 A objects created,
    • for all object A the destructor is called
    • program will display 5
  20. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <map>
    using namespace std;
    int main() {
    int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
    string s[] = { “one”, “one”, “two”, “two”, “three”,”three”, “four”, “four”, “five”, “five”};
    multimap<int, string> m;
    for (int i = 0; i < 10; i++) {
    m.push_back(pair<int, string>(t[i], s[i]));
    }

    for (multimap<int, string>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i?>first << ” “;
    }
    return 0;
    }

    • program outputs: 1 2 3 4 5
    • compilation error
    • program outputs: 1 1 2 2 3 3 4 4 5 5
    • program outputs: one two three four five
    • program outputs: one one two two three three four four five five