250+ TOP MCQs on Iterators and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Iterators”.

1. What are Iterators?
a) STL component used to point a memory address of a container
b) STL component used for vectors
c) STL component used to call functions efficiently
d) STL component used to define template classes
Answer: a
Clarification: Iterators are STL components used to point a memory address of a container. They are used to iterate over container classes.

2. Which function is used increment the iterator by a particular value?
a) next()
b) advance()
c) prev()
d) move()
Answer: b
Clarification: advance() function is used to increment an iterator by a given value.

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}

a) 2
b) 3
c) 4
d) 5
Answer: b
Clarification: Initially the ptr is pointing to first element of vector and now as we are advancing the iterator by 2 which takes the iterator the value 3. Hence the output is 3.
Output:

$ ./a.out 
3

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    ptr = advance(ptr, 2);
    cout << *ptr << endl; 
    return 0; 
}

a) 3
b) 4
c) Error
d) Segmentation fault
Answer: c
Clarification: advance() function is used to increment/decrement the iterator by a given value so it does not returns any thing. So when we are doing ptr = advance(ptr, 2); we are expecting the advance() function to return some value but as it doesn’t returns anything therefore compiler throws an error.

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

#include 
#include 
#include 
using namespace std; 
int main() 
{ 
    vector<int> ar = { 1, 2, 3, 4, 5 }; 
    vector<int>::iterator ptr = ar.begin(); 
    ptr = next(ptr, 3);
    cout << *ptr << endl; 
    return 0; 
}

a) 1
b) 2
c) 3
d) 4
Answer: d
Clarification: next() function returns an iterator to the position which is ahead of current iterator by a distance of given value. Hence when we are calling the function next(ptr,3); then we are storing the result into ptr which is now an iterator pointing to 4.
Output:

$ ./a.out 
4

6. How many types of Iterators are there?
a) 5
b) 2
c) 3
d) 4
Answer: a
Clarification: There are 5 types of iterators discussed under STL namely:
i) Input Iterators
ii) Output Iterators
iii) Forward Iterators
iv) Bi-directional Iterators
v) Random-access Iterators.

7. Pick the correct statement.
a) Input iterator moves sequentially forward
b) Input iterator moves sequentially backward
c) Input iterator moves in both direction
d) Input iterator moves sequentially downwards
Answer: a
Clarification: By definition Input iterators moves sequentially forward.

8. Which of the following is correct about Input Iterators?
a) Input iterators can be used with all relational operators
b) Input iterators can work with arithmetic operators
c) No value can be assigned to the location pointed by Input Iterator
d) Input iterators can work with sequence operators
Answer: c
Clarification: Values cannot be assigned to the location pointed by input operators. Input operators cannot be used with all relational and arithmetic operators.

9. Which of the following is correct about Input Iterators?
a) They cannot be decremented
b) Cannot be used in multi-pass algorithms
c) Can only be incremented
d) All of the mentioned
Answer: d
Clarification: Input iterators can only be incremented and as it cannot be decremented it can be used in single-pass algorithms only.

10. Which of the following is correct?
a) Input Iterators are used for assigning
b) Output Iterators are used for assigning
c) Both Input and Output Iterators are used for accessing
d) Both Input and Output Iterators are used for assigning
Answer: b
Clarification: Input Iterators are used for accessing and Output Iterators are used for assigning.

11. What type of Iterator i1 is in the following C++ code snipet?

================ code ================
vector<int>::iterator i1; 
for (i1=v1.begin();i1!=v1.end();++i1) 
	*i1 = 1;
======================================

a) Input Iterator
b) Output Iterator
c) Both Input and Output Iterator
d) Neither Input nor Output Iterator
Answer: b
Clarification: As i1 iterator is used for assigning values to the vector elements therefore it it an output iterator.

12. If i1 is Input Iterator and i2 is Output Iterator, then which of the following things are correct?

i) cout<<*i1;
ii) i2 can be used with == operator
iii) *i1 = 1
iv) i2--

a) i and ii
b) i only
c) i, ii and iv
d) iii and iv
Answer: b
Clarification: Input iterators are used for accessing containers, therefore *i1 = 1 is wrong. Both Input and Output iterators cannot be decremented therefore i2– is also wrong. Output iterators cannot be used with == operator, therefore, that is also wrong. So only cout<<*i1 is correct.

13. Which of the following is an advantage of Forward iterator over input and output iterator?
a) Can be used as both accessing and assigning iterator
b) Forward iterator can be incremented or decremented
c) Can be used with relational operators also
d) Can be used with arithmetic operators also
Answer: a
Clarification: Forward iterator is a combination of both input and output iterator, therefore, can be used as both accessing and assigning iterator. Just like Input and output iterator this can also be not used with all relational and arithmetic operators and can be incremented only.

14. What are Bi-directional iterators?
a) Iterator same as Forward Iterator
b) Forward Iterator that can be used in both directions
c) Iterator that can only be used to access the sequence from both sides
d) Iterator that can only be used to assign the sequence from both sides
Answer: b
Clarification: Bi-directional iterator is a type of forward iterators that can be used for both directions access and assign.

15. What are Random-access Iterators?
a) Iterators that can be used to access elements at an arbitrary offset position
b) Same as Bi-directional iterator
c) Input iterator with the additional property of random access
d) Output iterator with the additional property of random access
Answer: a
Clarification: Random access iterators are those iterators that can be used to access elements at an arbitrary offset position relative to the memory that the iterator points.

  250+ TOP MCQs on Standard Template Library and Answers

Leave a Comment

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

Scroll to Top