My Report

C++ Iterators Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following C++ code?

  
    #include <iostream>
    #include <iterator>
    #include <vector>
    using namespace std;
    int main () 
    {
        vector<int> myvector;
        for (int i = 0; i < 10; i++) 
            myvector.push_back(i);
        typedef vector<int> :: iterator iter_int;
        reverse_iterator<iter_int> rev_iterator;
        rev_iterator = myvector.rend() - 4;
        cout << *rev_iterator << endl;
        return 0;
    }

2. What is the use of checked iterators?

3. What will be the output of the following C++ code?

  
    #include <vector>
    #include <iostream>
    #include <typeinfo>
    #include <stdexcept>
    using namespace std;
    int main()
    {
        vector<int> vec;
        vec.push_back(10);
        int i = vec[100];
        try {
            i = vec[0];
            cout << i << endl;
        }
        catch (exception &e)
        {
            cout << "Caught: " << e.what( ) << endl;
            cout << "Type: " << typeid( e ).name( ) << endl;
        }
        catch (...) 
        {
            cout << "Unknown exception: " << endl;
        }
        return 0;
    }

4. What will be the output of the following C++ code?

  
    #include <iostream>
    #include <map>
    using namespace std;
    int main ()
    {
        try {
            map<char, int> mymap;
            map<char, int> :: iterator it;
            mymap['a'] = 50;
            mymap['b'] = 100;
            mymap['c'] = 150;
            mymap['d'] = 200;
            it = mymap.find('b');
            mymap.erase (it);
            mymap.erase (mymap.find('d'));
            cout << mymap.find('a') -> second << '\n';
        }
        catch (...) 
        {
            cout << "Unknown exception: " << endl;
        }
        return 0;
    }

5. What does the checked iterator allow you to find?

6. What will be the output of the following C++ code?

  
    #include <iostream>
    #include <iterator>
    using namespace std;
    int main ()
    {
        try {
            double value1, value2;
            istream_iterator<double> eos;       
            istream_iterator<double> iit (cin);   
            if (iit != eos) 
                value1 = *iit;
            iit++;
            if (iit != eos) 
                value2 = *iit;
            cout << (value1 * value2) << endl;
        }
        catch (...) {
            cout << "Unknown exception: " << endl;
        }
        return 0;
   }

7. What kind of errors do checked iterators detect?

8. What will be the output of the following C++ code?

  
    #include <iostream>
    #include <iterator>
    #include <vector>
    using namespace std;
    int main () 
    {
        vector<int> myvector;
        for (int i = 1; i < 4; ++i) 
            myvector.push_back(i*10);
        ostream_iterator<int> out_it (cout,", ");
        copy ( myvector.begin(), myvector.end(), out_it );
        return 0;
    }

9. What will happen if the iterator is unchecked?

10. How many adaptors support the checked iterators?


 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.