C++ practice questions with answers
Practise C++ questions at your own pace: pointers, references, the STL, virtual functions, smart pointers and move semantics. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.
What does this print?
easy
int a = 7, b = 2; std::cout << a / b << " " << a % b;- 3.5 1
- 3 1
- 3 0
- 4 1
Answer: B. 3 1
Explanation: Dividing two ints truncates towards zero: 7 / 2 = 3, and the remainder is 1.
Which container keeps its keys sorted?
easy
- std::unordered_map
- std::map
- std::vector
- std::queue
Answer: B. std::map
Explanation: std::map is a balanced binary search tree (usually red-black): sorted keys, O(log n) operations. unordered_map is a hash table.
What does the & in void f(int& x) mean?
easy
- x is a pointer
- x is a reference: changes inside f change the caller's variable
- x is passed by value
- x is the address of an int
Answer: B. x is a reference: changes inside f change the caller's variable
Explanation: Pass-by-reference avoids copying and lets the function modify the original. const int& gives no-copy read-only access.
What is the size of the vector after these lines?
easy
std::vector<int> v(3, 5); v.push_back(1); v.pop_back(); v.push_back(2);- 3
- 4
- 5
- 6
Answer: B. 4
Explanation: v(3, 5) holds three 5s. push_back then pop_back cancel out, and the last push_back makes it 4.
What does this print?
medium
int x = 5; int* p = &x; *p += 3; std::cout << x;- 5
- 8
- The address of x
- Undefined
Answer: B. 8
Explanation: p points to x, so *p += 3 changes x itself.
Which function is a virtual call resolved at run time?
medium
- A non-virtual member function
- A virtual function called through a base-class pointer or reference
- A static member function
- A function template
Answer: B. A virtual function called through a base-class pointer or reference
Explanation: Dynamic dispatch uses the object's vtable, so the derived override runs even through a Base*. Non-virtual calls are bound at compile time.
Why should a base class with virtual functions have a virtual destructor?
medium
- To make the class abstract
- So that deleting a derived object through a base pointer runs the derived destructor too
- It makes construction faster
- It is required to compile
Answer: B. So that deleting a derived object through a base pointer runs the derived destructor too
Explanation: Without it, delete basePtr only runs ~Base, which is undefined behaviour and usually leaks the derived part's resources.
What is the time complexity of std::sort?
medium
- O(n)
- O(n log n) on average and in the worst case (introsort)
- O(n²) worst case
- O(log n)
Answer: B. O(n log n) on average and in the worst case (introsort)
Explanation: Since C++11 the standard requires O(n log n) comparisons. Implementations use introsort: quicksort that falls back to heapsort.
What problem does std::unique_ptr solve?
hard
- Sharing ownership between many owners
- Exclusive ownership: the object is deleted automatically when the unique_ptr goes out of scope, and it can be moved but not copied
- Making objects thread-safe
- Garbage collection
Answer: B. Exclusive ownership: the object is deleted automatically when the unique_ptr goes out of scope, and it can be moved but not copied
Explanation: This is RAII. shared_ptr is for shared ownership with reference counting; weak_ptr breaks cycles.
What does std::move do?
hard
- Moves memory to a new address
- Casts its argument to an rvalue reference so a move constructor or move assignment can be chosen
- Deletes the source object
- Copies the object faster
Answer: B. Casts its argument to an rvalue reference so a move constructor or move assignment can be chosen
Explanation: std::move itself moves nothing; it only allows overloads that take T&& to "steal" resources. The moved-from object is left valid but unspecified.
After these lines, what is true about r?
hard
std::vector<int> v = {1, 2, 3}; int& r = v[0]; v.reserve(1000);- r is still a valid reference to v[0]
- r may now dangle: reallocation invalidates references, pointers and iterators
- r becomes 0
- It does not compile
Answer: B. r may now dangle: reallocation invalidates references, pointers and iterators
Explanation: Growing capacity moves the elements to new memory. Any reference or iterator taken before the reallocation is invalid.
Which statements about const in C++ are true? (Select all that apply)
hard
- A const member function promises not to modify the object
- const int* p means the int cannot be changed through p
- int* const p means p cannot point elsewhere
- const variables are always stored in read-only memory
Answer: A. A const member function promises not to modify the object; B. const int* p means the int cannot be changed through p; C. int* const p means p cannot point elsewhere
Explanation: Read const from right to left: const int* is a pointer to const int; int* const is a const pointer. Where a const object is stored is up to the compiler.