STL Quiz - Languages C++
Free practice quiz: STL (Languages C++). 53 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages C++ practice quiz has 53 multiple-choice questions on STL, and 53 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 53 questions, freeSample questions
Which STL container provides constant time complexity for insertion and deletion at both ends?
Show answer
Answer: C. std::deque
std::deque (double-ended queue) offers constant time complexity O(1) for insertions and deletions at both the front and back, unlike std::vector which has amortized constant time for insertions/deletions at the back but linear time at the front. std::list has constant time for insertions/deletions anywhere, but accessing elements is linear. std::set has logarithmic time complexity for these operations.
What is an iterator in the context of STL?
Show answer
Answer: B. A pointer-like object that allows traversal and access to elements of a container
Iterators are objects that act like pointers to elements within STL containers. They provide a standardized way to access elements, move through containers, and are fundamental to STL algorithms.
Which algorithm can be used to remove all occurrences of a specific value from a container like `std::vector`?
Show answer
Answer: B. std::remove
`std::remove` shifts all elements *not* equal to the specified value to the beginning of the range and returns an iterator to the new logical end. It does not actually change the size of the container. To remove the elements, you typically use the container's `erase` method with the iterator returned by `std::remove`. `std::erase` is a member function for some containers, or a free function since C++20 for sequence containers, that directly removes elements. `std::pop_back` removes the last element.
Which algorithm can be used to count the number of elements in a range that satisfy a specific condition?
Show answer
Answer: C. std::count_if
`std::count_if` counts the number of elements in the range `[first, last)` for which the given predicate returns `true`. `std::count` counts occurrences of a specific value. `std::find_if` finds the first element satisfying a condition. `std::accumulate` sums elements.
What is the time complexity of accessing the last element of a `std::vector` using `v.back()`?
Show answer
Answer: A. O(1)
Accessing the last element of a `std::vector` using `v.back()` is a constant time operation, O(1), because the vector stores its elements contiguously and knows the location of the last element.
More questions in this quiz
Answers and explanations are in the full quiz.
- What is the primary advantage of using std::unordered_map over std::map?
- Which algorithm can be used to sort a container in descending order?
- Which STL container is best suited for implementing a priority queue where elements are retrieved in order of priority (largest first by default)?
- What is the purpose of `std::accumulate`?
- Which STL container provides efficient insertion and deletion at any position, but does not offer random access?
- What does `std::find` return if the element is not found in the range?
- Which of the following is NOT a standard STL container?
- What is the purpose of `std::copy`?
- Which STL container offers O(log n) time complexity for insertion, deletion, and search operations?
- Which of the following algorithms is used to find the first element in a range that satisfies a specific condition?
- What is the primary difference between `std::set` and `std::multiset`?
- Which STL container is suitable for implementing a queue (FIFO - First-In, First-Out)?
- What is the time complexity of inserting an element into `std::vector` at the beginning?
- Which algorithm sorts a range and guarantees that the relative order of equal elements is preserved?
- What is the purpose of `std::inserter`?
- Which STL container is implemented as a hash table?
- What is the purpose of `std::partition`?
- Which standard library header is required to use `std::vector`?
- What is the time complexity of searching for an element in `std::set`?
- What is the purpose of `std::swap`?
- Which STL container is a sequence container that stores elements contiguously in memory?
- What is the result of calling `v.push_back(x)` on a `std::vector` if its capacity is full?
- Which algorithm finds the first element in a sorted range that is not less than a given value?
- What is the purpose of `std::unique`?
- Which STL container is a doubly-linked list?
- What is the time complexity of inserting an element into `std::list` at a position indicated by an iterator?
- Which STL container provides key-value pairs and keeps them sorted by key?
- What does `std::sort` require for its elements?
- What is the underlying container typically used by `std::stack`?
- Which algorithm rearranges elements in a range such that all elements for which a predicate returns `true` precede elements for which it returns `false`, while preserving the relative order within each group?
- What does `std::set::insert` return?
- Which algorithm efficiently finds the Nth smallest element in an unsorted range?
- What is the purpose of `std::transform`?
- Which STL container is designed for fast insertion and deletion at both the beginning and the end?
- What is the relationship between `std::remove` and `std::vector::erase` for removing all occurrences of a value?
- Which STL container provides guaranteed logarithmic time complexity for search, insertion, and deletion, and stores unique elements in sorted order?
- What is the purpose of `std::for_each`?
- Which STL container adapter provides a Last-In, First-Out (LIFO) behavior?
- Which algorithm can be used to merge two sorted ranges into a single sorted range?
- What is the purpose of `std::bind` (or lambdas) in conjunction with STL algorithms?
- Which STL container guarantees that elements are stored in sorted order and allows duplicate keys?
- What is the purpose of the `reserve()` member function in `std::vector`?
- Which algorithm finds the first element in a range that is greater than a given value?
- What is the time complexity of `std::list::push_front()`?
- Which algorithm can be used to copy elements from one range to another, applying a transformation function during the copy?
- What is the purpose of `std::back_inserter`?
- Which STL container provides efficient searching, insertion, and deletion, but does not store elements in a sorted order?
- What is the time complexity of `std::vector::insert` when inserting at the end?