C++ Programming Questions and Answers – Iterators and Sequences

This section on C++ language interview questions and answers focuses 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: Iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     #include <set>
  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
View Answer

Answer: b
Explanation: In this program, We are using const_iterator to sort the data
in the set.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ itr.cpp
$ a.out
12 21 31 32

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

advertisement
  1.     #include <iostream>
  2.     #include <vector>
  3.     #include<iterator>
  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
View Answer

Answer: b
Explanation: In this program, We are erasing the values in the vector based on the given condition.
Output:

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

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

  1.     #include <iostream>
  2.     #include <iterator>
  3.     #include <list>
  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
View Answer

Answer: c
Explanation: 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 <iostream>
  2.     #include <iterator>
  3.     #include <list>
  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
View Answer

Answer: a
Explanation: 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 <iostream>
  2.     #include <iterator>
  3.     #include <list>
  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
View Answer

Answer: c
Explanation: In this program, We are printing the number of elements in the list by using distance method.
Output:

$ g++ itr4.cpp
$ a.out
5

9. In which type of semantics does c++ implements iterator?
a) Memory
b) Size
c) Pointer
d) Value
View Answer

Answer: c
Explanation: 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) -+-
View Answer

Answer: a
Explanation: ‘++’ operator is used to represent the next element in the iterator.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
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.