C++ Programming Questions and Answers – Iterators

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: a
Explanation: 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()
View Answer

Answer: b
Explanation: advance() function is used to increment an iterator by a given value.

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

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

Answer: b
Explanation: 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
Note: Join free Sanfoundry classes at Telegram or Youtube

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

#include<iostream> 
#include<iterator> 
#include<vector> 
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
View Answer

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

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

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

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

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

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

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

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

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

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

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

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

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

Answer: a
Explanation: 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.

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.

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.