C++ STL Concepts MCQ Quiz
Test your knowledge of C++ Standard Template Library (STL) with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Medium Level Questions Medium
Which of the following is NOT a container in the C++ STL?
While tuple is part of the C++ standard library, it is not considered an STL container. The main STL containers are sequence containers (vector, list, deque, array) and associative containers (set, map, etc.).
Which container provides constant time complexity for insertion and deletion at both ends?
Deque (double-ended queue) provides O(1) time complexity for insertion and deletion at both the beginning and the end. Vector only provides O(1) at the end, list provides O(1) anywhere but doesn't support random access, and set is an associative container with different performance characteristics.
Which algorithm is used to rearrange elements such that all elements for which a predicate returns true come before those for which it returns false?
The partition algorithm rearranges elements in a range such that all elements for which the predicate returns true come before those for which it returns false. The relative order of elements is not necessarily preserved.
What is the primary difference between a set and a multiset in C++ STL?
The primary difference between set and multiset is that set contains unique elements only, while multiset can contain duplicate elements. Both are typically implemented as balanced binary search trees and maintain elements in sorted order.
Which iterator category allows both reading and writing, and movement in both directions?
Bidirectional iterators allow both reading and writing, and movement in both directions (forward and backward). Examples include iterators for list, set, and map. Random access iterators provide even more functionality.
What does the following code do?
vector<int> v = {1, 2, 3, 4, 5};
v.erase(remove(v.begin(), v.end(), 3), v.end());
This is the erase-remove idiom, a common pattern in C++ STL. The remove algorithm shifts non-removed elements to the front and returns an iterator to the new end, and erase actually removes the elements from the container.
Which of the following statements about STL algorithms is true?
STL algorithms are designed to work with iterators rather than containers directly. This provides flexibility as the same algorithm can work with different container types as long as they provide the appropriate iterators.
What is the time complexity of inserting an element in a std::map?
std::map is typically implemented as a balanced binary search tree (often a red-black tree), which provides O(log n) time complexity for insertion, deletion, and search operations.
Which container would be most appropriate for implementing a LRU (Least Recently Used) cache?
A list is often used for LRU cache implementation because it allows efficient removal from any position and moving elements to the front (to mark as recently used) in constant time. Typically, a combination of list and unordered_map is used for optimal LRU cache implementation.
What is the key difference between std::array and traditional C-style arrays?
The main advantage of std::array over C-style arrays is that it knows its own size (through the size() method) and doesn't decay to a pointer when passed to functions. Both have fixed size and are stack-allocated.
Advanced Level Questions Advanced
Which C++ feature allows STL algorithms to work with different types of containers seamlessly?
The STL's flexibility comes from the combination of templates (for generic programming), iterators (for abstracting container access), and function objects (for customizing behavior). This combination allows algorithms to work with various container types.
What is the primary advantage of using std::make_unique and std::make_shared over directly using new?
The primary advantage is exception safety. When using make functions, the allocation and construction happen in a single step, preventing memory leaks if an exception occurs during construction. While they also provide convenience, exception safety is the key design motivation.
Which C++17 feature allows more efficient handling of optional values without using pointers or special values?
std::optional, introduced in C++17, represents a value that may or may not be present. It provides a type-safe way to handle optional values without resorting to pointers, special values, or boolean flags.
What is the key difference between std::function and a function pointer?
The key advantage of std::function is that it can store any callable object - function pointers, lambda expressions, bind expressions, or function objects. This provides much more flexibility than plain function pointers.
Which of the following is NOT a valid way to customize the behavior of an STL algorithm?
Macros are not a recommended way to customize STL algorithm behavior. The standard ways are function pointers, lambda expressions, and function objects (functors). Macros lack type safety and can lead to hard-to-debug issues.