Sample questions
Question 1Easy
What is the output?
#include <iostream>
using namespace std;
#include <queue>
int main() {
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.pop();
cout << q.front() << " " << q.back() << " " << q.size();
return 0;
}
- 2 3 2
- 3 2 2
- 1 2 2
- 2 3 3
Answer: A. 2 3 2
Explanation
A queue is first-in, first-out: pop removes 1, leaving 2 at the front and 3 at the back.
Step-by-step solution
- A queue is first in, first out.
- After pushing 1, 2, 3, pop removes 1, leaving 2 at the front and 3 at the back.
- The size is now 2.
- The output is 2 3 2: option A.
Why the other options are wrong
- B. Front is 2 and back is 3 after the pop, so the first value is 2.
- C. 1 was removed by pop, so the front is 2, not 1.
- D. The size is 2 after one pop, not 3.
Tip: A queue pops from the front and adds at the back.
Question 2Easy
What is the output?
#include <iostream>
using namespace std;
#include <list>
int main() {
list<int> l{1, 2, 1, 3, 1};
l.remove(1);
for (int x : l) cout << x;
cout << " " << l.size();
return 0;
}
- 2131 4
- 23 2
- 23 5
- 213 3
Answer: B. 23 2
Explanation
list::remove deletes every element equal to the value, not just the first one.
Step-by-step solution
- The list starts as 1 2 1 3 1.
- remove(1) deletes every element equal to 1, leaving 2 and 3.
- The loop prints 23, and the size is 2.
- The output is 23 2: option B.
Why the other options are wrong
- A. Remove deletes all the 1s, so 2131 is not left.
- C. The size is 2, not 5, because three elements were removed.
- D. 213 still has a 1, but remove deletes every 1.
Tip: List::remove deletes all elements equal to the value, not only the first.
Question 3Easy
What is the output?
#include <iostream>
using namespace std;
#include <stack>
int main() {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
while (!s.empty()) {
cout << s.top();
s.pop();
}
return 0;
}
- 321
- 3
- 123
- 132
Answer: A. 321
Explanation
A stack is last-in, first-out, so elements come out in reverse order of insertion.
Step-by-step solution
- A stack is last in, first out.
- Push 1, push 2, push 3 leaves 3 on top.
- The loop pops 3, then 2, then 1, printing each.
- The output is 321: option A.
Why the other options are wrong
- B. The loop prints every element, not only the top one.
- C. 123 is the insertion order, but a stack pops in reverse.
- D. 132 is not a stack order; the stack pops 3, 2, 1.
Tip: A stack pops the most recently pushed element first.
Question 4Easy
What is the output?
#include <iostream>
using namespace std;
#include <deque>
int main() {
deque<int> d;
d.push_back(1);
d.push_front(2);
d.push_back(3);
d.push_front(4);
for (int x : d) cout << x;
return 0;
}
- 4231
- 1234
- 4213
- 2413
Answer: C. 4213
Explanation
push_front adds at the beginning and push_back at the end: after the four calls the deque is 4, 2, 1, 3.
Step-by-step solution
- push_back(1) gives 1; push_front(2) gives 2 1.
- push_back(3) gives 2 1 3; push_front(4) gives 4 2 1 3.
- The loop prints the elements in order.
- The output is 4213: option C.
Why the other options are wrong
- A. The order is 4 2 1 3; 4231 mixes up the last two.
- B. The front and back pushes are not in plain order, so 1234 is wrong.
- D. 2413 does not match the front and back pushes.
Tip: Push_front adds to the front and push_back adds to the back of a deque.
Question 5Medium
What is the output?
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<string, int> m;
if (m["x"] == 0) cout << "zero ";
cout << m.size() << " " << m.count("y");
return 0;
}
- zero 0 0
- zero 1 0
- 1 0
- zero 1 1
Answer: B. zero 1 0
Explanation
operator[] inserts a value-initialised element (0) when the key is missing, so the map grows to size 1. count does not insert.
Step-by-step solution
- m[x] creates the key x with value 0 because it does not exist, so the if is true and zero is printed.
- The map now has one element, so m.size() is 1.
- count(y) does not insert and y is not present, so it returns 0.
- The output is zero 1 0: option B.
Why the other options are wrong
- A. m[x] inserts the key, so the size is 1, not 0.
- C. The if test is true, so zero is printed first.
- D. Count does not insert, so the second count is 0, not 1.
Tip: operator[] inserts a missing key; count and find do not.
Question 6Medium
What is the output?
#include <iostream>
using namespace std;
#include <set>
int main() {
set<int> s{10, 20, 30, 40};
cout << *s.lower_bound(20) << " " << *s.upper_bound(20) << " " << *s.lower_bound(25);
return 0;
}
- 10 30 20
- 20 30 30
- 20 20 30
- 20 30 20
Answer: B. 20 30 30
Explanation
lower_bound returns the first element not less than the key, and upper_bound the first element greater than it. For 25 the first element >= 25 is 30.
Step-by-step solution
lower_bound(20) = first >= 20 = 20.upper_bound(20) = first > 20 = 30.lower_bound(25) = first >= 25 = 30.- Output: 20 30 30.
Why the other options are wrong
- A. lower_bound(20) is 20, not 10.
- C. upper_bound(20) skips past 20.
- D. No element 20 is >= 25.
Tip: lower = not less; upper = greater.
Question 7Medium
What is the output?
#include <iostream>
using namespace std;
#include <map>
#include <string>
int main() {
map<int, string> m;
m[1] = "a";
m.insert({1, "b"});
cout << m[1];
m[1] = "c";
cout << m[1] << m.size();
return 0;
}
- bc2
- ab2
- bc1
- ac1
Answer: D. ac1
Explanation
insert does nothing when the key already exists, so the value stays "a". operator[] with assignment overwrites it.
Step-by-step solution
- m[1] = 'a' stores a for key 1.
- Insert with key 1 does nothing because the key already exists, so the value stays a.
- Cout m[1] prints a. Then m[1] = 'c' overwrites the value, so the next cout prints c.
- The map has one key, so m.size() is 1. The output is ac1: option D.
Why the other options are wrong
- A. Bc2 needs insert to overwrite the value, and the map size is 1, not 2.
- B. Ab2 has the wrong second value (it prints c, not b) and the wrong size; the map holds one key.
- C. Bc1 assumes insert replaced the value, but insert never overwrites an existing key.
Tip: Map insert never overwrites an existing key; assigning with operator[] does.
Question 8Medium
What is the output?
#include <iostream>
using namespace std;
#include <queue>
int main() {
priority_queue<int> pq;
for (int x : {3, 1, 4, 1, 5}) pq.push(x);
while (!pq.empty()) {
cout << pq.top();
pq.pop();
}
return 0;
}
- 5431
- 54311
- 11345
- 31415
Answer: B. 54311
Explanation
std::priority_queue is a max-heap by default, so top() always returns the largest remaining element. Duplicates are kept.
Step-by-step solution
priority_queue is a max-heap by default.top() gives the largest each time.- Pushed 3 1 4 1 5 -> pops 5 4 3 1 1.
- Output: 54311.
Why the other options are wrong
- A. Duplicate 1 is kept, so there are two 1s.
- C. That is ascending; the default is descending.
- D. This is the insertion order, not heap order.
Tip: Default priority_queue = max-heap.
Question 9Hard
Which statement about std::vector iterator invalidation is correct?
- push_back that causes reallocation invalidates all iterators, pointers and references to elements
- Iterators are invalidated only when the vector is destroyed
- push_back never invalidates iterators because elements are reference-counted
- erase invalidates only the iterator to the erased element, never later ones
Answer: A. push_back that causes reallocation invalidates all iterators, pointers and references to elements
Explanation
When capacity is exceeded the elements move to new storage, so every existing iterator and pointer dangles. erase invalidates iterators at and after the erase point.
Step-by-step solution
- A vector keeps its elements in one block of memory with a fixed capacity.
- When push_back needs more room than the capacity, the vector allocates new storage and moves the elements.
- Old iterators, pointers and references then point to freed memory, so they are invalidated.
- So option A is the correct statement.
Why the other options are wrong
- B. Iterators are invalidated by reallocation and erase, not only when the vector is destroyed.
- C. Vectors do not reference-count elements, so push_back that reallocates does invalidate iterators.
- D. Erase invalidates the erased position and every iterator after it, not only the erased one.
Tip: Any insert or reallocation can invalidate vector iterators, so check the rules before holding one.
Question 10Hard
What does the C++ standard guarantee about the iteration order of std::unordered_set<int>?
- Elements are visited in descending order of hash value
- Elements are visited in ascending order
- Nothing specific; the order depends on hashing and bucket layout and may change after rehashing
- Elements are visited in insertion order
Answer: C. Nothing specific; the order depends on hashing and bucket layout and may change after rehashing
Explanation
Unordered containers make no ordering promise. Code that needs sorted output should use std::set or sort the elements.
Step-by-step solution
- The standard only says that unordered containers have no defined iteration order.
- The order depends on the hash function, the bucket layout and past insertions, and it can change after a rehash.
- So nothing specific is guaranteed about the order: option C.
Why the other options are wrong
- A. No order by hash value is promised for unordered_set.
- B. Ascending order is not guaranteed; use std::set when sorted order is needed.
- D. Unordered_set does not keep insertion order.
Tip: Only ordered containers such as set and map guarantee a sorted order; unordered containers guarantee no order.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →