CPP : C++ Certified Professional Programmer : Part 01

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

    #include <vector>
    #include <set>
    #include <deque>
    #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(5);
    generate(v1.begin(), v1.end(), Sequence(1));
    set<int> s1(v1.rbegin(), v1.rend());
    deque<int> d1(s1.rbegin(), s1.rend());
    reverse(v1.begin(),v1.end());
    reverse(s1.begin(), s1.end());
    reverse(d1.begin(), d1.end());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );
    for_each(s1.begin(), s1.end(), Out<int>(cout) );
    for_each(d1.begin(), d1.end(), Out<int>(cout) );cout<<endl;
    return 0;
    }

    Program outputs:

    • 5 4 3 2 1 1 2 3 4 5 1 2 3 4 5
    • 1 2 3 4 5 1 2 3 4 5 5 4 3 2 1
    • no output
    • 1 2 3 4 5 5 4 3 2 1 1 2 3 4 5
    • compilation error
  2. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <set>
    using namespace std;
    struct Even {
    bool operator ()(int a) {
    return (a % 2)==0?true:false;
    }
    };
    int main () {
    int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
    set<int> s(t,t+15);

    int number = count_if(s.begin(), s.end(), Even());
    cout<< number<<endl;
    return 0;
    }

    Program outputs:

    • 4
    • 3
    • 7
    • 8
    • compilation error
  3. 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;
    }
    struct sequence {
    int val,inc;
    sequence(int s, int i):val(s),inc(i){}
    int operator()(){
    int r = val; val += inc;
    return r;
    }
    };
    int main() {
    vector<int> v1(10);
    fill(v1.begin(), v1.end(), sequence(1,1));
    for_each(v1.begin(), v1.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 1 2 3 4 5 6 7 8 9 10
    • 10
    • 0 0 0 0 0 0 0 0 0 0
    • compilation error
  4. 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<<” “; } };
    bool Greater(int v1, int v2) { return v1<v2; }
    int main() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    vector<int> v1(t, t+10);
    sort(v1.begin(), v1.end(), Greater);
    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
  5. 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,3,5,1,2,4,4,5};
    vector<int> v (t,t+10);
    vector<int>::iterator it = v.begin();

    while ( (it = adjacent_find (it, v.end())) != v.end()) {
    cout<<it?v.begin()<<” “;it++;
    }
    cout<< endl;
    return 0;
    }

    • program outputs: 2 3
    • program outputs: 2 7
    • program outputs: 3 8
    • compilation error
    • program will run forever
  6. 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);
    multiset<int> s1(v.begin(),v.end());
    multiset<int, greater<int> > s2(v.begin(), v.end());
    for(multiset<int, greater<int> >::iterator i=s2.begin();i!= s2.end(); i++) {
    cout<<*i<<” “;
    }
    for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
    cout<<*i<<” “;
    }
    cout<<endl;
    return 0;
    }

    The output will be:

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

    #include <iostream>
    #include <map>
    #include <string>
    using namespace std;
    int main(){
    int second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
    string first[] = {“three”, “four”, “two”, “one”, “six”,”five”, “seven”, “nine”,”eight”,” ten”};
    map<int,string> m;
    for(int i=0; i<10; i++) {
    m.insert(pair<int,string>(second[i],first[i]));
    }
    if (m[11] == “eleven”) {
    cout<<“eleven “;
    }
    for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) {
    cout<<i?>second<<” “;
    }
    cout<<m.size();
    return 0;
    }

    • program outputs: one two three four five six seven eight nine ten 11
    • program outputs: one two three four five six seven eight nine ten 10
    • program outputs: one two three four five six seven eight nine ten 10
    • program outputs: eleven one two three four five six seven eight nine ten 10
    • runtime exception
  8. What happens when you attempt to compile and run the following code?

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

    Program outputs:

    • 64 64
    • 100 0x64
    • 0x64 0x64
    • 64 100
    • compilation error
  9. What happens when you attempt to compile and run the following code?

    #include <list>
    #include <deque>
    #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, 7, 8, 4, 5 };
    list<int> l1(t1, t1 + 5);
    int t2[] ={ 3, 2, 6, 9, 0 };
    deque<int> d1(t2, t2 + 5);
    l1.sort();
    d1.sort();
    l1.merge(d1);
    print(l1.begin(), l1.end());
    print(d1.begin(), d2.end()); cout<<endl;
    return 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
    • program outputs: 9 8 7 6 5 4 3 2 1 0
    • compilation error
  10. What happens when you attempt to compile and run the following code?

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

    Program outputs:

    • 64.21 64.21
    • 64.21 0x64.21
    • 0x64.21 0x64.21
    • 100.33 100.33
    • compilation error
  11. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <set>
    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 < ( const A & b) const { return a<b.a;}
    };
    struct display { void operator() (const A & a) {cout << ” ” << a.getA();} };
    struct add10
    {
    void operator() (A & a) { a.setA(a.getA()+10) ;}
    };

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

    • program outputs: 10 5 9 6 2 4 7 8 3 1 1 2 3 4 5 6 7 8 9 10
    • program outputs: 20 15 19 16 12 14 17 18 13 11 1 2 3 4 5 6 7 8 9 10
    • program outputs: 20 15 19 16 12 14 17 18 13 11 11 12 13 14 15 16 17 18 19 20
    • compilation error
  12. 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<<” “; } };
    int main() {
    B t1[]={1,2,3,4,5,6,7,8,9,10};
    B t2[]={1,2,3,4,5,6,7,8,9,10};
    vector<B> v1(t1, t1+10);
    vector<B> v2(t2, t2+10);
    vector<B> v3(10);
    transform(v1.begin(), v1.end(), v2.rbegin(), v3.begin(), minus<B>());
    for_each(v3.rbegin(), v3.rend(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 9 7 5 3 1 ?1 ?3 ?5 ?7 ?9
    • ?1 ?3 ?5 ?7 ?9 9 7 5 3 1
    • 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
    • 1 3 5 7 9 ?1 ?3 ?5 ?7 ?9
    • ?9 ?7 ?5 ?3 ?1 1 3 5 7 9
  13. 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);
    generate(v1.begin(), v1.end(), Sequence(1));
    partition(v1.begin(),v1.end(), Odd());
    for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
    return 0;
    }

    Choose all possible 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
  14. 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<<” “; } };
    struct Add : public binary_function<B, B, B> {
    B operator() (const B & a, const B & b) const {
    return a+b; } };
    int main() {
    B 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(Add(), 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
  15. What will happen when you attempt to compile and run the following code? Choose all that apply.

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <set>
    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 < (const A & b) const { return a<b.a;}
    };
    class F {
    A val;
    public:
    F(A & v):val(v){}
    bool operator() (A & v) {
    if (v.getA() == val.getA()) return true;
    return false;
    }
    };
    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    vector<A> v1(t, t + 10);
    set<A> s1(t, t + 10);
    A a(6); F f(a);
    find_if(s1.begin(), s1.end(), f);
    if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {
    cout<<“Found!\n”;
    } else {
    cout<<“Not found!\n”;
    }
    return 0;
    }

    • it will compile successfully
    • it will display Found!
    • it will display Not found!
    • it will not compile successfully
  16. What happens when you attempt to compile and run the following code? Choose all possible answers.

    #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) {
    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
  17. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    template<typename T>class B { T val;
    public:
    B(T v):val(v){}
    T getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} };
    template<class T>ostream & operator <<(ostream & out, const B<T> & 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<<” “; } };
    bool Less(const B<float> &a, const B<float> &b) { return int(a.getV())<int(b.getV());}
    int main() {
    float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
    vector<B<float> > v1; v1.assign(t, t+10);
    stable_sort(v1.begin(), v1.end(), Less);
    for_each(v1.begin(), v1.end(), Out<B<float> >(cout));cout<<endl;

    return 0;
    }

    Program outputs:

    • 1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64
    • 1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94
    • compilation error
    • 3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13
    • the exact output is impossible to determine
  18. 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<<” “; } };
    template <typename T> struct Sequence {
    T start; T step;
    Sequence(T start, T step):start(start), step(step){}
    T operator()() { T v = start; start+=step; return v; } };
    bool Less(float a, float b) { return int(a)<int(b);}
    int main() {
    float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
    vector<float> v1; v1.assign(t, t+10);
    stable_sort(v1.begin(), v1.end(), Less);
    for_each(v1.begin(), v1.end(), Out<float>(cout));cout<<endl;

    return 0;
    }

    Program outputs:

    • 1.66 1.32 1.96 1.13 2.28 2.3 2.98 2.62 3.94 3.64
    • 1.13 1.32 1.66 1.96 2.28 2.3 2.62 2.98 3.64 3.94
    • compilation error
    • 3.94 3.64 2.98 2.62 2.3 2.28 1.96 1.66 1.32 1.13
    • the exact output is impossible to determine
  19. What happens when you attempt to compile and run the following code?

    #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};
    set<B> s1(t, t+10);
    sort(s1.begin(), s1.end());
    for_each(s1.begin(), s1.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
  20. What will happen when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

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

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

    • program will display:1
    • program will not compile
    • program will compile
    • program will cause runtime exception
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments