CPP : C++ Certified Professional Programmer : Part 02

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

    #include <vector>
    #include <set>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    class B {
    int val;
    public:
    B(int v):val(v){}
    operator int() { return val;}
    };
    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++; } };
    bool predicate(int v) { return v%2==0; }
    int main() {
    vector<int> v1(10);
    generate_n(v1.begin(), 10, Sequence(1));
    for_each(v1.begin(), remove_if(v1.begin(), v1.end(), predicate), Out<int>(cout));cout<<endl;
    return 0;}

    Program outputs:

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

    #include <iostream>
    using namespace std;

    int main ()
    {
    float f1 = 10.0;
    float f2 = 10.123;
    cout<<noshowpoint<<f1<<” “<<f2;
    return 0;
    }

    Program outputs:

    • 10 10
    • 10.0 10.123
    • compilation error
    • 10 10.123
  3. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main ()
    {
    float f = 10.126;
    cout.unsetf(ios::floatfield);
    cout<<showpoint<<f<<fixed<<” “<<setprecision(2)<<f<<endl;
    return 0;
    }

    Program outputs:

    • 10.126 10
    • 10.126 10.12
    • 10.1260 10.13
    • 10.126 10.13
  4. What happens when you attempt to compile and run the following code?

    #include <deque>
    #include <vector>
    #include <iostream>
    using namespace std;
    int main ()
    {
    int t[] = {1, 2 ,3 ,4 ,5};
    vector<int>v1(t, t+5);
    deque<int>d1;
    d1.assign(v1.end(), v1.begin());
    for(int i=0; i<d1.size(); i++)
    {
    cout<<d1.at(i)<<” “;
    }
    cout<<endl;
    return 0;
    }

    • program outputs 5 4 3 2 1
    • program outputs 1 2 3 4 5
    • compilation error in line 8
    • compilation error in line 10
    • segmentation fault runtime exception
  5. Which changes introduced independently will allow code to compile and display 0 1 8 9 (choose all that apply)

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

    };
    /* Insert Code Here 2 */
    int main(){
    A t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
    vector<A>v(t, t+10);
    set<A> s1(v.begin(),v.end());
    s1.insert(v.begin(),v.end());
    s1.erase(s1.lower_bound(2),s1.upper_bound(7));
    for(set<A>::iterator i=s1.begin();i!= s1.end(); i++) {
    cout<<i?>getA()<<” “;
    }
    cout<<endl;
    return 0;
    }

    • operator int() const { return a;} inserted at Place 1
    • bool operator < (const A & b) const { return a<b.a;} inserted at Place 1
    • bool operator < (const A & b) const { return b.a<a;} inserted at Place 1
    • bool operator < (const A & a, const A & b) { return a.getA()<b.getA();} inserted at Place 2
  6. 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;} };

    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_difference(t1,t1+5,t2,t2+5,v1.begin());
    for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 1 2 3 4 5 6 8 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
    • 1 2 5 0 0 0 0 0 0 0
  7. What happens when you attempt to compile and run the following code?

    #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<<” “; } };
    int main() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    deque<int> d1(t, t+10);
    sort(d1.begin(), d1.end());
    deque<int>::iterator it = upper_bound(d1.begin(), d1.end(), 4);
    for_each(it, d1.end(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

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

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

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

    Program outputs:

    • 10 5 9 6 2 4 7 8 3 1
    • 10 5 9 6 2 4 7 8 3 1 10 5 9 6 2 4 7 8 3 1
    • compilation error
    • runtime exception/segmentation fault
  9. What will happen when you attempt to compile and run the following code?

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

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

    How many objects of type A will be created:

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

    #include <vector>
    #include <iostream>
    using namespace std;
    class A
    {
    int a,b;
    public:
    A & operator =(const A & c) { a = c.a; return *this;}
    A():a(0),b(0){}
    void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}
    int getA() {return a;} int getB() {return b;}
    };

    int main ()
    {
    vector<A> v;
    A a;
    a.setA(10); a.setB(11);
    v.push_back(a);
    A b = v.front(); v.pop_back();
    cout<<b.getB()<<” “<<b.getA()<<endl;
    return 0;
    }

    • program outputs 11 10
    • compilation error
    • program outputs 0 10
    • program outputs 10 0
    • program outputs 11 0
  11. 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<<” “; } };
    B Add(B a, B b) { 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(), bind2nd(ptr_fun(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
  12. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    bool compare(int a, int b) { return a == b; }
    int main () {
    int t[] = {1,2,3,4,5,1,2,3,4,5};
    vector<int> v (t,t+10);
    vector<int>::iterator it = v.begin();
    int m1[] = {1, 2, 3};

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

    • program outputs: 0 1 2 5 6 7
    • program outputs: 0 5
    • program outputs: 0 0
    • compilation error
    • program will run forever
  13. What happens when you attempt to compile and run the following code?

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int main ()
    {
    float f = 10.126;
    cout<<f<<” “<<setprecision(2)<<f<<endl;
    return 0;
    }

    Program outputs:

    • 10.126 10
    • 10.126 10.12
    • compilation error
    • 10.126 10.13
  14. What happens when you attempt to compile and run the following code?

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

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

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

    Program outputs:

    • 10 5 9 6 2 4 7 8 3 1
    • 1 3 8 7 4 2 6 9 5 10 10 5 9 6 2 4 7 8 3 1
    • 1 3 8 7 4 2 6 9 5 10
    • runtime exception/segmentation fault
    • compilation error
  15. What will happen 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) { _v+=a; }

    };

    int main()

    {

    A<string> a(“Hello”);

    string s(” world!”);

    a.add(s);

    cout << a.getV() <<endl;

    return 0;

    }

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

    #include <iostream>

    using namespace std;

    template<int>
    void g(int a)
    {
    cout<<a?1<<endl;
    }

    template<class A>
    void g(A a)
    {
    cout<<a+1<<endl;
    }

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

    • program displays: 1
    • program displays: 2
    • compilation error
    • runtime exception
  17. 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 start++;
    }
    };
    int main() {
    vector<int> v1(10);
    generate_n(v1.begin(), 10, Sequence(1));
    for_each(v1.begin(), v1.end(), print);
    cout<<endl;
    return 0;
    }

    Program outputs:

    • 1 2 3 4 5 6 7 8 9 10
    • 0 0 0 0 0 0 0 0 0 0
    • compilation error
    • no output
  18. Which lines of the code below contain proper instantiation of queue objects?

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

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

    • line I
    • line II
    • line III
    • line IV
    • line V
  19. 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==(const A & b) const { return a == b.a; }
    };
    bool compare(const A & a, const A & b) { return a == b; }
    int main () {
    int t[] = {1,2,3,3,5,1,2,4,4,5};
    vector<A> v (t,t+10);
    vector<A>::iterator it = v.begin();

    while ( (it = adjacent_find (it, v.end(), compare)) != 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
  20. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    class C {

    public:
    int _c;
    C():_c(0){}
    C(int c) { _c = c;}
    C operator+=(C & b) {
    C tmp;
    tmp._c = _c+b._c;
    return tmp;
    }
    };

    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(2);
    A<C> a (5);
    C c;
    a.add(c);
    cout << a.getV() <<endl;
    return 0;
    }

    • program will display:2
    • 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