C++ STL MCQs (Standard Template Library)

1 min read

The C++ Standard Template Library (STL) is a powerful set of template classes that provides general-purpose structures and functions for handling data efficiently. It is built around three core components: containers (which store data), algorithms (which process data), and iterators (which bridge the gap between containers and algorithms). Containers like std::vector, std::map, and std::set manage collections of objects using under-the-hood implementations like dynamic arrays and red-black trees. Iterators allow developers to traverse these collections safely without exposing their internal memory layouts. Mastering the STL is essential for writing optimized, production-ready C++ code and is a heavily tested topic in software engineering interviews.

1. What is the STL in C++?

a) A library of generic containers, algorithms, and iterators
b) A compiler
c) An operating system
d) A database engine
Correct Answer: a) A library of generic containers, algorithms, and iterators
Explanation:
The STL provides reusable generic data structures and algorithms.

2. Which header is used for std::vector?

a) <list>
b) <vector>
c) <map>
d) <set>
Correct Answer: b) <vector>
Explanation:
The vector container is declared in the vector header.

3. Which of the following is a sequence container?

a) map
b) set
c) vector
d) unordered_map
Correct Answer: c) vector
Explanation:
Sequence containers store elements in a linear order.

4. Which of the following is an associative container?

a) vector
b) deque
c) list
d) map
Correct Answer: d) map
Explanation:
Associative containers store elements by key.

5. Which container is an unordered associative container?

a) unordered_map
b) vector
c) deque
d) list
Correct Answer: a) unordered_map
Explanation:
Unordered containers use hashing instead of sorted order.

6. Which container adapter uses deque as its default underlying container?

a) stack
b) queue
c) vector
d) list
Correct Answer: b) queue
Explanation:
std::queue uses std::deque by default.

7. Which container adapter uses vector as its default underlying container?

a) stack
b) queue
c) priority_queue
d) deque
Correct Answer: c) priority_queue
Explanation:
std::priority_queue uses std::vector by default.

8. Which container adapter can use deque as its default underlying container?

a) queue
b) map
c) set
d) stack
Correct Answer: d) stack
Explanation:
std::stack uses std::deque by default.

9. What does begin() return?

a) An iterator to the first element
b) An iterator past the last element
c) The size
d) A pointer to memory only
Correct Answer: a) An iterator to the first element
Explanation:
begin points to the first element of a container.

10. What does end() return?

a) The first element
b) An iterator past the last element
c) The last element
d) The capacity
Correct Answer: b) An iterator past the last element
Explanation:
end is a sentinel position after the last element.

11. What does rbegin() return?

a) First element
b) Past-the-end iterator
c) A reverse iterator to the last element
d) Container size
Correct Answer: c) A reverse iterator to the last element
Explanation:
rbegin starts reverse traversal.

12. What does rend() return?

a) Last element
b) First element
c) Reverse begin
d) A reverse iterator before the first element
Correct Answer: d) A reverse iterator before the first element
Explanation:
rend marks the end of reverse traversal.

13. What is std::vector best described as?

a) A dynamic array
b) A linked list
c) A hash table
d) A binary tree
Correct Answer: a) A dynamic array
Explanation:
vector provides contiguous, resizable storage.

14. What is std::list?

a) A contiguous array
b) A doubly linked list
c) A hash set
d) A priority queue
Correct Answer: b) A doubly linked list
Explanation:
list supports fast insertion and deletion at known positions.

15. What is std::forward_list?

a) A doubly linked list
b) A vector
c) A singly linked list
d) A map
Correct Answer: c) A singly linked list
Explanation:
forward_list stores elements in a one-way linked sequence.

16. Which container allows efficient insertion and deletion at both ends?

a) vector
b) forward_list
c) set
d) deque
Correct Answer: d) deque
Explanation:
deque supports efficient operations at the front and back.

17. Which function adds an element to the end of a vector?

a) push_back()
b) push_front()
c) insert_end()
d) append_new()
Correct Answer: a) push_back()
Explanation:
push_back appends an element at the end.

18. Which function removes the last element from a vector?

a) push_back()
b) pop_back()
c) clear()
d) erase_end()
Correct Answer: b) pop_back()
Explanation:
pop_back removes the final element.

19. Which function constructs an element at the end of a vector?

a) push_front()
b) pop_back()
c) emplace_back()
d) make_back()
Correct Answer: c) emplace_back()
Explanation:
emplace_back forwards arguments to construct the element in place.

20. Which function requests extra storage for a vector without changing size?

a) clear()
b) resize()
c) shrink_to_fit()
d) reserve()
Correct Answer: d) reserve()
Explanation:
reserve increases capacity without adding elements.

21. What does size() return?

a) The number of elements
b) The capacity
c) The maximum size
d) The first index
Correct Answer: a) The number of elements
Explanation:
size gives the current element count.

22. What does empty() return when a container has no elements?

a) false
b) true
c) 0
d) nullptr
Correct Answer: b) true
Explanation:
empty is true when size is zero.

23. Which function removes all elements from a container?

a) clear()
b) erase()
c) delete()
d) remove()
Correct Answer: a) clear()
Explanation:
clear destroys or removes all elements.

24. Which member function removes elements from many STL containers?

a) push()
b) assign()
c) find()
d) erase()
Correct Answer: d) erase()
Explanation:
erase deletes elements at positions or ranges.

25. Which function inserts elements into a container?

a) insert()
b) remove()
c) clear()
d) swap()
Correct Answer: a) insert()
Explanation:
insert places elements at a specified position.

26. What does std::map store?

a) Only keys
b) Key-value pairs
c) Only values
d) Unordered duplicates
Correct Answer: b) Key-value pairs
Explanation:
map associates each key with a mapped value.

27. What does std::set store?

a) Duplicate keys
b) Key-value pairs
c) Unique keys
d) Indexes only
Correct Answer: c) Unique keys
Explanation:
set contains unique sorted elements.

28. Which container allows duplicate keys?

a) set
b) map
c) unordered_set
d) multiset
Correct Answer: d) multiset
Explanation:
multiset can store repeated values.

29. What is true about keys in std::map?

a) They are unique
b) They can be duplicated freely
c) They must be integers
d) They are not sorted
Correct Answer: a) They are unique
Explanation:
Each key appears once in a map.

30. What is a typical advantage of std::unordered_map?

a) Sorted order
b) Average constant-time lookup
c) No hashing
d) Only integer keys
Correct Answer: b) Average constant-time lookup
Explanation:
Hash tables provide fast average access.

31. What does std::pair hold?

a) One value
b) Three values
c) Two values named first and second
d) Only iterators
Correct Answer: c) Two values named first and second
Explanation:
pair groups two related values.

32. What does std::tuple hold?

a) Only two values
b) Only strings
c) Only integers
d) A fixed number of heterogeneous values
Correct Answer: d) A fixed number of heterogeneous values
Explanation:
tuple can store multiple values of different types.

33. Which function creates a pair?

a) std::make_pair
b) std::make_tuple
c) std::make_vector
d) std::make_list
Correct Answer: a) std::make_pair
Explanation:
make_pair constructs a pair from two values.

34. Which function accesses the first element of a tuple?

a) std::pair.first
b) std::get<0>(t)
c) std::first(t)
d) std::access(t)
Correct Answer: b) std::get<0>(t)
Explanation:
std::get uses a zero-based index.

35. What does std::sort do by default?

a) Sorts in descending order
b) Shuffles elements
c) Sorts using operator<
d) Removes duplicates
Correct Answer: c) Sorts using operator<
Explanation:
sort arranges elements in ascending order by default.

36. What does std::binary_search require?

a) An unsorted range
b) A linked list
c) A map only
d) A sorted range
Correct Answer: d) A sorted range
Explanation:
Binary search only works on sorted data.

37. What does std::find return?

a) An iterator to the found element or end
b) The index only
c) A Boolean only
d) A copy of the element
Correct Answer: a) An iterator to the found element or end
Explanation:
find searches a range and returns an iterator.

38. What does std::count do?

a) Sorts elements
b) Counts matching elements
c) Deletes elements
d) Reverses a range
Correct Answer: b) Counts matching elements
Explanation:
count returns how many elements match a value.

39. What does std::reverse do?

a) Sorts a range
b) Removes elements
c) Reverses the order of elements
d) Finds the maximum
Correct Answer: c) Reverses the order of elements
Explanation:
reverse swaps elements to invert their order.

40. Which algorithm returns an iterator to the smallest element?

a) sort()
b) find()
c) count()
d) min_element()
Correct Answer: d) min_element()
Explanation:
min_element locates the minimum element.

41. Which header provides std::accumulate?

a) <numeric>
b) <iostream>
c) <utility>
d) <cstring>
Correct Answer: a) <numeric>
Explanation:
accumulate is declared in the numeric header.

42. What does std::for_each do?

a) Deletes elements
b) Applies a function to each element
c) Sorts elements
d) Creates a copy
Correct Answer: b) Applies a function to each element
Explanation:
for_each invokes a callable over a range.

43. What does std::transform do?

a) Sorts a range
b) Counts elements
c) Applies a function and stores results
d) Removes duplicates
Correct Answer: c) Applies a function and stores results
Explanation:
transform can map a function over one or two ranges.

44. What does std::remove do?

a) It erases elements from the container automatically
b) It reduces container size automatically
c) It deletes memory
d) It shifts elements and returns a new logical end iterator
Correct Answer: d) It shifts elements and returns a new logical end iterator
Explanation:
Use the erase-remove idiom to actually remove elements.

45. What does std::unique do?

a) It removes consecutive equal elements logically
b) It sorts elements
c) It erases from the container automatically
d) It counts duplicates
Correct Answer: a) It removes consecutive equal elements logically
Explanation:
unique returns a new logical end iterator; erase may be needed.

46. What does std::lower_bound return?

a) The last element
b) The first element not less than the value
c) The first greater element
d) The smallest key
Correct Answer: b) The first element not less than the value
Explanation:
lower_bound finds the insertion point for a value.

47. What does std::upper_bound return?

a) The first element not less
b) The last element
c) The first element greater than the value
d) The largest key
Correct Answer: c) The first element greater than the value
Explanation:
upper_bound finds the first strictly greater element.

48. What does std::equal_range return?

a) A single iterator
b) A Boolean
c) A map
d) A pair of iterators representing lower and upper bounds
Correct Answer: d) A pair of iterators representing lower and upper bounds
Explanation:
equal_range combines lower_bound and upper_bound.

49. What iterator category do vector iterators usually support?

a) Random access
b) Forward only
c) Bidirectional only
d) Input only
Correct Answer: a) Random access
Explanation:
vector iterators support constant-time jumps.

50. What can happen to vector iterators and references after reallocation?

a) Nothing
b) They can become invalidated
c) Only size changes
d) Only capacity changes
Correct Answer: b) They can become invalidated
Explanation:
Reallocation may move elements to new storage.
← Previous: C++ Pointers and References - MCQs
Next →: C++ Strings MCQs
NewC++ Memory Management MCQs

C++ Memory Management MCQs

Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…

By MCQs Generator
New
No Image

C++ Templates - MCQs

1. What is the STL in C++? a) A library of generic containers, algorithms, and iterators b) A compiler c)…

By MCQs Generator
NewC++ Pointers and References - MCQs

C++ Pointers and References - MCQs

In C++, pointers and references are powerful features that provide direct access to memory and allow for highly efficient data…

By MCQs Generator