CPP : C++ Certified Professional Programmer : Part 06

  1. 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;
    }

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

    Program outputs:

    • 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
    • compilation error
    • 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
    • 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1
  2. 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;
    }

    int main() {
    vector<int> v1(10,1);
    fill(v1.begin()+2, v1.end()?2,2);
    fill_n(v1.begin()+4,2,3);
    for_each(v1.begin(), v1.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 1 1 2 2 3 3 2 2 1 1
    • 1 1 2 2 2 2 2 2 1 1
    • compilation error
    • none of these
  3. 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.insert(pair<int, string>(t[i], s[i]));
    }
    if (m.count(3) == 2) {
    m.erase(3);
    }
    for (multimap<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 2 4 4 5 5
    • program outputs: one two three four five
  4. 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() {
    int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
    int t1[]={1,2,3,4};
    deque<int> d1(t, t+10);
    set<int> s1(t, t+10);
    sort(d1.begin(), d1.end());
    cout<<includes(s1.begin(),s1.end(), t1,t1+4)<<” “<<includes(d1.begin(),d1.end(), t1,t1+4)
    <<endl;
    return 0;
    }

    Program outputs:

    • 1 1
    • 1 0
    • 0 1
    • 0 0
  5. 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;
    }
    int main() {
    int t[] = { 1, 5, 2, 5, 2, 4, 4, 3, 3, 1 };
    vector<int> v1(t, t+10);
    set<int> s1(t, t+10);
    replace(v1.begin(), v1.end(), 1, 10);
    replace(s1.begin(), s1.end(), 1, 10);
    for_each(v1.begin(), v1.end(), myfunction);
    return 0;
    }

    Program outputs:

    • 10 5 2 5 2 4 4 3 3 1
    • 1 10 2 5 2 4 4 3 3 10
    • compilation error
    • 10 5 2 5 2 4 4 3 3 10
  6. What will happen when you attempt to compile and run the following code?

    #include <iostream>
    #include <set>
    #include <vector>
    using namespace std;
    template<class T> void print(T start, T end) {
    while (start != end) {
    std::cout << *start << ” “; start++;
    }
    }
    int main(){
    vector<int>v;
    set<int> s;
    for(int i=10; i>0; i??) {
    v.push_back(i);
    s.push_back(i);
    }
    print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl;
    return 0;
    }

    The output will be:

    • 10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10
    • 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 1
    • 10 9 8 7 6 5 4 3 2 1 and unpredictable sequence of number range 1 to 10
    • compilation error
  7. 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, 6 , 7, 8 , 9, 10};
    vector<int>v1(t, t+10);
    deque<int>d1(t, t+10);
    vector<int>::iterator it1 = v1.begin();
    deque<int>::iterator it2 = d1.begin();
    d1.erase(it2+5);
    it2 = d1.begin();
    cout<<*(it2+5)<<” “;
    v1.erase(it1+5);
    it1 = v1.begin();
    cout<<*(it1+5)<<endl;
    }

    • program outputs: 7 7
    • program outputs: 6 6
    • compilation error
    • result is unpredictable
  8. What happens when you attempt to compile and run the following code?

    #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; queue<int> second(mydeck);
    queue<int> third(second); queue<int, list<int> > fourth(mylist);
    fourth.push(10);fourth.push(11);fourth.push(12);
    queue<int, vector<int> > fifth(myvector);
    fifth.push(10);fifth.push(11);fifth.push(12); // Line I
    while(!fifth.empty())
    {
    cout<<fifth.front()<<” “; // Line II
    fifth.pop(); // Line III
    }
    while (!fourth.empty())
    {
    cout << fourth.front() << ” “;
    fourth.pop(); // Line IV
    }
    return 0;
    }

    • program outputs: 10 11 12 10 11 12
    • compilation error in line I
    • compilation error in line II
    • compilation error in line III
    • compilation error in line IV
  9. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <iostream>
    using namespace std;
    class A
    {
    int a;
    public:
    A():a(0){} A(int a){ this?>a = a;}
    void setA(int a) {this?>a = a;}
    int getA() {return a;}
    };
    ostream &operator<<(ostream & cout, A & a)
    {
    cout<< a.getA();
    return cout;
    }
    int main ()
    {
    vector<A*>v(5, new A());
    v.push_back(new A(1));
    vector<A*>::iterator it;
    for(it = v.begin(); it != v.end(); it++)
    {
    cout<<*it<<” “;
    }
    cout<<endl;
    return 0;
    }

    • program outputs 0 0 0 0 0 1
    • program outputs 0 0 0 0 0 0
    • compilation error
    • program outputs 1 1 1 1 1 1
    • none of these
  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):val(v){} B(){}
    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(), greater<B>());
    deque<B>::iterator it = lower_bound(d1.begin(), d1.end(), 4,greater<B>());
    for_each(it, d1.end(), Out<B>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • 4 3 2 1
    • 3 2 1
    • 5 4 3 2 1
    • compilation error
    • 1 2 3 4
  11. 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, 6 , 7, 8 , 9, 10};
    vector<int>v1(t, t+10);
    deque<int>d1(t, t+10);
    d1.empty();
    v1.empty();

    if (v1.isempty())
    {
    cout<<“I am empty “;
    }
    else
    {
    cout<<“I am not empty “;
    }
    cout<<v1.size()<<” “<<d1.size()<<endl;
    return 0;
    }

    • program outputs: I am empty 0 0
    • program outputs: I am not empty 0 0
    • compilation error
    • program outputs: I am not empty 10 10
  12. What happens when you attempt to compile and run the following code?

    #include <vector>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <ctype.h>
    using namespace std;
    template<typename T>class B { T val;
    public:
    B(T v):val(v){}
    T getV() const {return 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<<” “; } };
    string tolower(const string & s) {
    string tmp(s);
    for(unsigned i = 0; i< tmp.size(); ++i){
    tmp[i] = tolower(tmp[i]); }
    return tmp; }
    bool Less(const B<string> &a, const B<string> &b) {
    return tolower(a.getV())<tolower(b.getV()); }
    int main() {
    string t[]={“aaa”,”bbb”,”Aaa”, “Bbb”,”aAa”,”bBb”,”aaA”,”bbB”};
    vector<B<string> > v1; v1.assign(t, t+8);
    stable_sort(v1.begin(), v1.end(), Less);
    for_each(v1.begin(), v1.end(), Out<B<string> >(cout));cout<<endl;
    return 0;
    }

    Program outputs:

    • Aaa aaa aAa aaA bbb Bbb bBb bbB
    • Aaa aaa aAa aaA bbb Bbb bbB bBb
    • aaa Aaa aAa aaA bbb Bbb bBb bbB
    • the exact output is impossible to determine
  13. 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”};
    multimap<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(multimap<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
    • compilation error
  14. 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(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
  15. 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,2,3,5,1,2,7,3,2,1,10, 4,4,5};
    vector<int> v (t,t+15);

    vector<int>::iterator it = search_n(v.begin(), v.end(), 4, 2);
    cout<< it?v.begin()<<endl;
    return 0;
    }

    Program outputs:

    • 10
    • 3
    • 1
    • 15
    • compilation error
  16. 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.flags(ios::showbase);
    cout<<100<<” “;
    return 0;
    }

    Program outputs:

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

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

    Program outputs:

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

    #include <deque>
    #include <vector>
    #include <iostream>
    #include <string>
    using namespace std;
    template<typename T>
    void print(T start, T end)
    {
    while (start != end)
    cout<<*start++;
    }
    int main ()
    {
    string t[] = {“one”, “two” ,”three” ,”four”, “five”};
    vector<string>v1(t, t+5);
    deque<string>d1(v1.rbegin(), v1.rend());
    d1.push_back(“zero”);
    print(d1[0].rbegin(),d1[0].rend());

    return 0;
    }

    • program outputs: orez
    • program outputs: evif
    • compilation error
    • program outputs: five
  19. 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<<” “; } };
    struct Sequence { int start;
    Sequence(int start):start(start){}
    int operator()() {return 10*(1+(start++ %3));}
    };
    int main() {
    deque<int> d1(10);
    generate(d1.begin(), d1.end(), Sequence(1));
    sort(d1.begin(), d1.end());
    pair<deque<int>::iterator, deque<int>::iterator > result = equal_range(d1.begin(), d1.end(), 20);
    for_each(result.first, result.second, Out<int>(cout));cout<<endl;
    return 0;
    }

    Program outputs:

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

    Program outputs:

    • found at position: 5
    • found at position: 0
    • found at position: 6
    • found at position: 1
    • found at position: 10