AlgoMaster Logo

Iterators - Quiz

Last Updated: December 6, 2025

1 min read

Iterators Quiz

30 quizzes

1
Multiple Choice

Which operation must every C++ iterator type support at minimum?

2
Multiple Choice

What do begin() and end() of a standard container typically return?

3
Multiple Choice

Which iterator category allows both ++ and --, but not random jumps?

4
Multiple Choice

Which standard container provides random access iterators?

5
Multiple Choice

Which is a key advantage of using iterators with STL algorithms?

6
Multiple Choice

Which standard component is an example of an output iterator adapter?

7
Multiple Choice

What does std::make_reverse_iterator do?

8
Multiple Choice

When implementing a custom random access iterator, which typedef or using alias is critical for STL compatibility?

9
Multiple Choice

Which statement about input iterators is correct?

10
Multiple Choice

Which is the safest way to write through an iterator into a std::vector<int>?

11
Code Completion

Get a const iterator to the beginning of a std::vector<int> v

cpp
1
auto it = v.();

Click an option to fill the blank:

12
Code Completion

Use std::back_inserter to append elements to dst from src

cpp
1
std::copy(src.begin(), src.end(), std::(dst));

Click an option to fill the blank:

13
Code Completion

Create a transform iterator that doubles elements of vec using a lambda

cpp
1
auto it = std::make_transform_iterator(vec.begin(), (int x){ return x * 2; });

Click an option to fill the blank:

14
Output Prediction

What is the output of this code using a vector iterator?

1#include <iostream>
2#include <vector>
3
4int main() {
5    std::vector<int> v{1, 2, 3};
6    auto it = v.begin();
7    *it = 10;
8    ++it;
9    std::cout << v[0] << " " << *it << " " << v.back() << "\n";
10    return 0;
11}
15
Output Prediction

What does this reverse iteration print?

1#include <iostream>
2#include <vector>
3
4int main() {
5    std::vector<int> v{1, 2, 3, 4};
6    for (auto it = v.rbegin(); it != v.rend(); ++it) {
7        std::cout << *it;
8    }
9    std::cout << "\n";
10    return 0;
11}
16
Output Prediction

What is the output when using std::transform with back_inserter?

1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5int main() {
6    std::vector<int> src{1, 2, 3};
7    std::vector<int> dst;
8    std::transform(src.begin(), src.end(), std::back_inserter(dst),
9                   [](int x){ return x * x; });
10    for (int x : dst) std::cout << x << " ";
11    std::cout << "\n";
12    return 0;
13}
17
Output Prediction

What is the output of this code using a custom-like pointer iterator?

1#include <iostream>
2
3int main() {
4    int arr[] = {5, 10, 15};
5    int* begin = arr;
6    int* end = arr + 3;
7    for (int* it = begin; it != end; ++it) {
8        if (it == begin + 1) *it = 42;
9    }
10    std::cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
11    return 0;
12}
18
Output Prediction

What does this code print when mixing iterators and indices?

1#include <iostream>
2#include <vector>
3
4int main() {
5    std::vector<int> v{2, 4, 6, 8};
6    auto it = v.begin();
7    it += 2;
8    std::cout << *it << " ";
9    std::cout << v[it - v.begin()] << "\n";
10    return 0;
11}
19
Bug Spotting

Find the bug in this iterator-based erase function

Click on the line(s) that contain the bug.

cpp
1
#include <vector>
2
 
3
void remove_even(std::vector<int>& v) {
4
    for (auto it = v.begin(); it != v.end(); ++it) {
5
        if (*it % 2 == 0) {
6
            v.erase(it);
7
        }
8
    }
9
}
20
Bug Spotting

Find the bug in this custom iterator increment implementation

Click on the line(s) that contain the bug.

cpp
1
#include <cstddef>
2
 
3
template<typename T>
4
class RawPtrIterator {
5
    T* ptr_;
6
public:
7
    explicit RawPtrIterator(T* p) : ptr_(p) {}
8
 
9
    T& operator*() const { return *ptr_; }
10
 
11
    RawPtrIterator& operator++() {
12
        // move to next element
13
        ptr_--;
14
        return *this;
15
    }
16
};
21
Hotspot Selection

Click the line that dereferences an invalidated iterator

Click on the line to select.

cpp
1
#include <vector>
2
#include <algorithm>
3
 
4
int main() {
5
    std::vector<int> v{1,2,3,4,5};
6
    auto it = std::find(v.begin(), v.end(), 3);
7
    v.push_back(6);
8
    if (it != v.end()) {
9
        std::cout << *it << "\n";
10
    }
11
    return 0;
12
}
22
Hotspot Selection

Click the line where a const_iterator is incorrectly modified

Click on the line to select.

cpp
1
#include <vector>
2
 
3
int main() {
4
    std::vector<int> v{1,2,3};
5
    std::vector<int>::const_iterator it = v.begin();
6
    *it = 10;
7
    return 0;
8
}
23
Fill in the Blanks

Complete the range-based for loop using iterators under the hood

cpp
1
#include <vector>
2
#include <iostream>
3
4
int main() {
5
std::vector<int> v{1,2,3};
6
for ( it = v.(); it != v.(); ++it) {
7
std::cout << *it << " ";
8
}
9
std::cout << "\n";
10
}

Click an option to fill blank 1:

24
Fill in the Blanks

Fill in the blanks to use std::make_reverse_iterator correctly

cpp
1
#include <vector>
2
#include <iostream>
3
#include <iterator>
4
5
int main() {
6
std::vector<int> v{1,2,3};
7
auto rbegin = std::make_reverse_iterator(v.());
8
auto rend = std::make_reverse_iterator(v.());
9
for (auto it = rbegin; it != rend; ++it) {
10
std::cout << *it << " ";
11
}
12
std::cout << "\n";
13
}

Click an option to fill blank 1:

25
Fill in the Blanks

Fill in the blanks to declare iterator traits for a custom iterator

cpp
1
struct MyIter {
2
using = std::random_access_iterator_tag;
3
using = int;
4
using difference_type = std::ptrdiff_t;
5
using pointer = int*;
6
using reference = int&;
7
};

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the code using std::istream_iterator and std::back_inserter

cpp
1
#include <vector>
2
#include <iterator>
3
#include <algorithm>
4
5
void read_all(std::istream& is, std::vector<int>& v) {
6
std::<int> first(is), last;
7
std::copy(first, last, std::(v));
8
}

Click an option to fill blank 1:

27
Matching

Match each iterator category with a valid operation guarantee

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match iterator-related adapters with their description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the typical steps to implement a simple custom forward iterator for a dynamic array

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the operations when using a transform iterator with std::vector<int>

Drag and drop to reorder, or use the arrows.

Premium Content

Subscribe to unlock full access to this content and more premium articles.