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

1

Which of the following is NOT a container in the C++ STL?

Correct Answer: D) tuple

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.).

2

Which container provides constant time complexity for insertion and deletion at both ends?

Correct Answer: B) deque

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.

3

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?

Correct Answer: B) partition

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.

4

What is the primary difference between a set and a multiset in C++ STL?

Correct Answer: B) multiset allows duplicates, set doesn't

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.

5

Which iterator category allows both reading and writing, and movement in both directions?

Correct Answer: D) Bidirectional Iterator

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.

6

What does the following code do?
vector<int> v = {1, 2, 3, 4, 5};
v.erase(remove(v.begin(), v.end(), 3), v.end());

Correct Answer: B) Removes all occurrences of 3

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.

7

Which of the following statements about STL algorithms is true?

Correct Answer: C) They operate through iterators

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.

8

What is the time complexity of inserting an element in a std::map?

Correct Answer: B) O(log n)

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.

9

Which container would be most appropriate for implementing a LRU (Least Recently Used) cache?

Correct Answer: B) list

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.

10

What is the key difference between std::array and traditional C-style arrays?

Correct Answer: B) std::array knows its own size

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

11

Which C++ feature allows STL algorithms to work with different types of containers seamlessly?

Correct Answer: D) All of the above

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.

12

What is the primary advantage of using std::make_unique and std::make_shared over directly using new?

Correct Answer: B) Exception safety

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.

13

Which C++17 feature allows more efficient handling of optional values without using pointers or special values?

Correct Answer: A) std::optional

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.

14

What is the key difference between std::function and a function pointer?

Correct Answer: A) std::function can store any callable object

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.

15

Which of the following is NOT a valid way to customize the behavior of an STL algorithm?

Correct Answer: D) Macro

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.

Your score: 0/15