CPP : C++ Certified Professional Programmer : Part 12

  1. 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);
    deque<int>::iterator it = lower_bound(d1.begin(), d1.end(), 4);
    for_each(it, d1.end(), Out<int>(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
  2. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

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

    int main()
    {
    A<int> a(1);
    a.add(10);
    cout.setf( ios::showpoint);
    cout << a.getV() << ” ” << a.get(1.0)<<endl;
    return 0;
    }

    • program will display: 11 11
    • program will not compile
    • program will display: 11.0000 11
    • program will display: 11 11.000
  3. 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;

    int main() {
    int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
    vector<int> v1(t, t + 10);
    deque<int> d1(t, t + 10);
    set<int> s1(t, t + 10);
    cout<<find(v1.begin(), v1.end(), 6)<<” “<<find(d1.begin(), d1.end(), 6)<<” “<<find(s1.begin(), s1.end(), 6);
    return 0;
    }

    • program outputs: 6 6 6
    • program outputs: 3 3 5
    • program outputs: 3 6 5
    • compilation error
    • none of these
  4. 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 = 10; i>0; i??)
    {
    v1.push_back(i);
    }
    std::vector<int>::iterator it = v1.begin();
    int sum = 0;
    while(it != v1.end())
    {
    sum+=it++;
    }
    std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<” “<<sum <<std::endl;

    return 0;
    }

    • program outputs 3 55
    • compilation error
    • program outputs 3 45
    • program outputs 7 55
  5. What happens when you attempt to compile and run the following code?

    #include <iostream>

    using namespace std;

    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: 0
    • program displays: 2
    • compilation error
    • runtime exception
  6. 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[]={6,10,8,7,9};
    vector<int> v1(10);
    sort(t1, t1+5);
    sort(t2, t2+5);
    merge(t1,t1+5,t2,t2+5,v1.begin());
    for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

    #include <list>
    #include <iostream>
    #include <deque>
    using namespace std;
    template<class T> void print(T start, T end) {
    while (start != end) {
    std::cout << *start << ” “; start++;
    }
    }
    class A {
    int a;
    public:
    A(int a):a(a){}
    operator int () const { return a;}int getA() const { return a;}
    };
    struct R {
    int val;
    R(int v):val(v){}
    bool operator ()(const A & a) { return a>val;} };
    int main() {
    int t1[] ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    list<A> l1(t1, t1 + 10);
    R r(4);l1.remove_if(r);
    print(l1.begin(), l1.end()); cout<<endl;
    return 0;
    }

    • program outputs: 1 2 3 4
    • program outputs: 5 6 7 8 9 10
    • program outputs: 1 2 3 4 5
    • program outputs: 6 7 8 9 10
  8. 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_difference(t1,t1+5,t2,t2+5,v1.begin());
    for_each(v1.begin(), v1.end(), Out<int>(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
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments