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
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
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
Explanation: 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?
#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;
}
a) 12 21 32 31
b) 12 21 31 32
c) 12 21 32
d) 12 21 31
View Answer
Explanation: 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?
#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;
}
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
Explanation: 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?
#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;
}
a) 30
b) 40
c) 50
d) 60
View Answer
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?
#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;
}
a) 10 20 1 2
b) 10 20
c) 1 2
d) 1 10
View Answer
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?
#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;
}
a) 20
b) 100
c) 5
d) 15
View Answer
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
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
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.
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship