
The Standard Template Library is the toolbox every C++ programmer and competitive coder uses daily, and interviewers love asking about its complexities and gotchas. This third C++ set covers vector, map, unordered_map, set, deque, list and priority_queue, iterator invalidation, and algorithms like sort, remove and lower_bound. Assume the usual headers are included with using namespace std.
How to practise: read each question and tap the option you think is correct. You will see right away whether you got it, along with a short explanation, and your score is tracked at the bottom of the screen. This set has 15 questions.
Q1. Which STL container gives average O(1) lookup, insertion and deletion by key?
a) std::map
b) std::unordered_map
c) std::set
d) std::list
Q2. std::map is usually implemented as:
a) A hash table
b) A self-balancing binary search tree (typically a red-black tree)
c) A sorted array
d) A doubly linked list
Q3. What is printed?
vector<int> v = {3, 1, 2};
sort(v.begin(), v.end());
cout << v[0] << v[2];
a) 31
b) 13
c) 12
d) 32
Q4. What is printed?
set<int> s = {5, 1, 5, 3};
cout << s.size();
a) 4
b) 3
c) 2
d) Compilation error
Q5. What is the time complexity of vector::push_back?
a) Always O(1)
b) Amortized O(1)
c) O(log n)
d) O(n) every time
Q6. You hold an iterator into a vector and then call push_back. What can happen to the iterator?
a) Nothing, vector iterators are never invalidated
b) It may be invalidated if the vector had to reallocate
c) It always points to the new element
d) It becomes a reverse iterator
Q7. Which relationship always holds for a std::vector v?
a) v.size() > v.capacity()
b) v.capacity() >= v.size()
c) v.size() == v.capacity()
d) v.capacity() == 2 * v.size()
Q8. What is printed?
vector<int> v = {1, 2, 3, 2};
remove(v.begin(), v.end(), 2);
cout << v.size();
a) 2
b) 4
c) 3
d) 0
Q9. What is printed?
priority_queue<int> pq; pq.push(3); pq.push(7); pq.push(1); cout << pq.top();
a) 3
b) 7
c) 1
d) Compilation error
Q10. Which container supports fast insertion and removal at BOTH the front and the back, plus random access?
a) std::vector
b) std::deque
c) std::forward_list
d) std::stack
Q11. What is printed?
map<string, int> m; cout << m["x"] << m.size();
a) 00
b) 01
c) 10
d) Throws an exception
Q12. What is printed?
vector<int> v = {1, 3, 5, 7};
cout << *lower_bound(v.begin(), v.end(), 4);
a) 3
b) 5
c) 4
d) 7
Q13. Why can you not call std::sort on a std::list?
a) list elements are const
b) std::sort needs random-access iterators, and list only has bidirectional iterators
c) list is already sorted
d) sort only works on arrays
Q14. What is the default underlying container of std::stack?
a) std::vector
b) std::list
c) std::deque
d) std::array
Q15. What is the time complexity of find() on a std::set with n elements?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Recommended MCQs
C++ MCQs - Classes, Objects, Constructors and References (Set 1)
C++ MCQs - Inheritance, Virtual Functions and Templates (Set 2)
Comments
Post a Comment