My Report

C++ Iterators Test – 1


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 <list>
    using namespace std;
    int main () 
    {
        list<int> mylist;
        for (int i = 0; i < 5; i++) 
            mylist.push_back (i * 20);
        list<int> :: iterator first = mylist.begin();
        list<int> :: iterator last = mylist.end();
        cout << distance(first, last) << endl;
        return 0;
    }

2. In which type of semantics does c++ implements iterator?

3. How many categories of iterators are there in c++?

4. Which of the following can serve as random-access iterator?

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

  
    #include <iostream>
    #include <vector>
    #include<iterator>
    using namespace std;
    int main ()
    {
        vector<int> myvector;
        for (int i = 1; i <= 10; i++)
        myvector.push_back(i);
        myvector.erase (myvector.begin() + 6);
        myvector.erase (myvector.begin(), myvector.begin() + 4);
        for (unsigned i = 0; i < myvector.size(); ++i)
        cout << ' ' << myvector[i];
        return 0;
    }

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

  
    #include <iostream>
    #include <iterator>
    #include <list>
    using namespace std;
    int main () 
    {
        list<int> firstlist, secondlist;
        for (int i = 1; i <= 2; i++)
        {  
            firstlist.push_back(i); 
            secondlist.push_back(i * 10); 
	}
        list<int> :: iterator it;
        it = firstlist.begin(); 
        advance (it, 3);
        copy (secondlist.begin(), secondlist.end(), inserter(firstlist, it));
        for ( it = firstlist.begin(); it != firstlist.end(); ++it )
            cout << *it << " ";
        return 0;
    }

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

  
    #include <iostream>
    #include <set>
    using namespace std;
    int main()
    {
        set<int> tst;
        tst.insert(12);
        tst.insert(21);
        tst.insert(32);
        tst.insert(31);
        set<int> :: const_iterator pos;
        for(pos = tst.begin(); pos != tst.end(); ++pos)
        cout << *pos << ' ';
        return 0;
    }

8. What kind of pattern is iterator pattern?

9. By using which operator does point to next element is represent in
iterator?

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

  
    #include <iostream>
    #include <iterator>
    #include <list>
    using namespace std;
    int main () 
    {
        list<int> mylist;
        for (int i = 0; i < 10; i++) 
        mylist.push_back (i * 10);
        list<int> :: iterator it = mylist.begin();
        advance (it, 5);
        cout  << *it << endl;
        return 0;
    }

 

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.