250+ TOP MCQs on Iterators and Sequences and Answers

This section on C++ language interview questions and answers on “Iterators and Sequences”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ language interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions on “Iterators and Sequences” along with answers, explanations and/or solutions:

1. How many categories of iterators are there in c++?
a) 2
b) 4
c) 5
d) 3
Answer: c
Clarification: There are five types of iterators. They are Output, Input, Forward, Random access and Bi-directional.

2. Which of the following can serve as random-access iterator?
a) Memory pointer
b) Object pointer
c) Class pointer
d) Memory & Class pointer
Answer: b
Clarification: Because of this, It can serve as any category of iterator.

3. What kind of pattern is iterator pattern?
a) Design pattern
b) Sequence pattern
c) Adapter pattern
d) Star pattern
Answer: a
Clarification: Iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements.

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

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main()
  5.     {
  6.         set<int> tst;
  7.         tst.insert(12);
  8.         tst.insert(21);
  9.         tst.insert(32);
  10.         tst.insert(31);
  11.         set<int> :: const_iterator pos;
  12.         for(pos = tst.begin(); pos != tst.end(); ++pos)
  13.         cout << *pos << ' ';
  14.         return 0;
  15.     }

a) 12 21 32 31
b) 12 21 31 32
c) 12 21 32
d) 12 21 31
Answer: b
Clarification: In this program, We are using const_iterator to sort the data
in the set.
Output:

$ g++ itr.cpp
$ a.out
12 21 31 32

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

  1.     #include 
  2.     #include 
  3.     #include
  4.     using namespace std;
  5.     int main ()
  6.     {
  7.         vector<int> myvector;
  8.         for (int i = 1; i <= 10; i++)
  9.         myvector.push_back(i);
  10.         myvector.erase (myvector.begin() + 6);
  11.         myvector.erase (myvector.begin(), myvector.begin() + 4);
  12.         for (unsigned i = 0; i < myvector.size(); ++i)
  13.         cout << ' ' << myvector[i];
  14.         return 0;
  15.     }

a) 5 6 7 8 9
b) 5 6 8 9 10
c) 6 7 8 9 10
d) 4 7 5 9 10
Answer: b
Clarification: In this program, We are erasing the values in the vector based on the given condition.
Output:

$ g++ itr1.cpp
$ a.out
5 6 8 9 10

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         list<int> mylist;
  8.         for (int i = 0; i < 10; i++) 
  9.         mylist.push_back (i * 10);
  10.         list<int> :: iterator it = mylist.begin();
  11.         advance (it, 5);
  12.         cout  << *it << endl;
  13.         return 0;
  14.     }

a) 30
b) 40
c) 50
d) 60
Answer: c
Clarification: In this program, We are printing the sixth element in the list.
Output:

$ g++ itr2.cpp
$ a.out
50

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         list<int> firstlist, secondlist;
  8.         for (int i = 1; i <= 2; i++)
  9.         {  
  10.             firstlist.push_back(i); 
  11.             secondlist.push_back(i * 10); 
  12. 	}
  13.         list<int> :: iterator it;
  14.         it = firstlist.begin(); 
  15.         advance (it, 3);
  16.         copy (secondlist.begin(), secondlist.end(), inserter(firstlist, it));
  17.         for ( it = firstlist.begin(); it != firstlist.end(); ++it )
  18.             cout << *it << " ";
  19.         return 0;
  20.     }

a) 10 20 1 2
b) 10 20
c) 1 2
d) 1 10
Answer: a
Clarification: In this iterator, We are copying the first list into second and printing it.
Output:

$ g++ itr3.cpp
$ a.out
10 20 1 2

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

  1.     #include 
  2.     #include 
  3.     #include 
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         list<int> mylist;
  8.         for (int i = 0; i < 5; i++) 
  9.             mylist.push_back (i * 20);
  10.         list<int> :: iterator first = mylist.begin();
  11.         list<int> :: iterator last = mylist.end();
  12.         cout << distance(first, last) << endl;
  13.         return 0;
  14.     }

a) 20
b) 100
c) 5
d) 15
Answer: c
Clarification: In this program, We are printing the number of elements in the list by using distance method.
Output:

9. In which type of semantics does c++ implements iterator?
a) Memory
b) Size
c) Pointer
d) Value
Answer: c
Clarification: C++ uses pointer arithmetic/semantic to implement iterators.

10. By using which operator does point to next element is represent in
iterator?
a) ++
b) —
c) +-
d) -+-
Answer: a
Clarification: ‘++’ operator is used to represent the next element in the iterator.

  250+ TOP MCQs on Exception Handling – 2 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top