ExamDumpster

Free CPP sample questions

Here are free CPP sample questions on STL containers and algorithms, each with the answer and an explanation. They also touch on sorting, binary search and templates. Use them to spot which parts of the standard library you should revisit.

Try them in the simulator Same questions, with study, timed and flashcard modes.

Showing 10 of 20 free sample questions.

Question 1Choose one

A developer is attempting to remove all elements with the value `3` from a `std::vector` inside a loop. Their initial code causes a crash. ```cpp // Problematic Code for (auto it = v.begin(); it != v.end(); ++it) { if (*it == 3) { v.erase(it); // This line is problematic } } ``` Which of the following loops correctly and safely erases all elements with the value `3` from the vector?

Question 2Choose one

A program needs to find the common elements between two sorted lists of user IDs. Given the two input vectors `admins` and `power_users`, what will be the content of the `result` vector after the following code is executed? ```cpp #include #include #include #include int main() { std::vector admins = {101, 205, 301, 450}; std::vector power_users = {205, 301, 512, 620}; std::vector result; std::set_intersection(admins.begin(), admins.end(), power_users.begin(), power_users.end(), std::back_inserter(result)); for (int id : result) { std::cout << id << " "; } return 0; } ``` graph TD subgraph Input A[admins: 101, 205, 301, 450] B[power_users: 205, 301, 512, 620] end Input --> |std::set_intersection| Output(result: ?)

Question 3Choose one

A developer uses `std::partition` to reorder a vector of integers so that all odd numbers appear before all even numbers. Given the initial vector `{8, 3, 7, 2, 5, 4, 6, 1}`, which of the following is a possible state for the vector after the operation `std::partition(v.begin(), v.end(), [](int n){ return n % 2 != 0; });` is executed?

Question 4Choose one

When populating a `std::map `, a developer has a large dataset where it is common for keys to already exist in the map. They want to avoid overwriting existing values. Which of the following methods is generally more performant for this specific use case and why?

Question 5Choose one

What is the output of the following program that uses `std::count_if` to count elements satisfying a specific condition? ```cpp #include #include #include int main() { std::vector scores = {88, 95, 67, 72, 99, 58, 91}; int passing_grade = 90; auto high_scores = std::count_if(scores.begin(), scores.end(), [=](int score) { return score >= passing_grade; }); std::cout << high_scores; return 0; } ```

Question 6Choose one

To efficiently find the median element in a large, unsorted `std::vector` without the overhead of fully sorting the entire collection, a developer should use the `std::____` algorithm.

Question 7Choose one

What is the precise output of the following C++ program that uses various stream manipulators to format a string? ```cpp #include #include #include int main() { std::string product_id = "A42"; std::cout << "|" << std::setw(8) << std::setfill('*') << std::left << product_id << "|"; return 0; } ```

Question 8Choose one

The function adapters `std::bind1st` and `std::bind2nd` are deprecated in C++11 and removed in C++17. A developer maintaining legacy code needs to replace a call like `std::find_if(v.begin(), v.end(), std::bind2nd(std::greater (), 10));` which finds the first element greater than 10. What is the simplest and most idiomatic modern C++ equivalent using a lambda expression?

Question 9Choose one

**Case Study:** A development team is building a collaborative text editor. A core feature is the multi-level undo/redo functionality. The system must store the entire state of the document after each significant user action (e.g., typing a word, deleting a selection). **Requirements:** 1. When a user performs an action, the new document state is added to a history manager. 2. When the user clicks 'Undo', the most recent state must be retrieved and removed from the history, and the previous state becomes the current one. 3. The system must support at least 100 levels of undo. 4. Performance of adding a new state and performing an undo operation must be very fast (amortized constant time is required). 5. Memory usage should be reasonably efficient, but performance is the primary concern. Which STL container is the most suitable choice to implement the history manager for the 'undo' stack?

Question 10Choose 2

To use a custom class `MyData` as a key in a `std::map ` that uses the default comparator, what are the minimal requirements that the class must fulfill? (Select TWO)

10 more free samples are waiting

Create a free account to unlock the whole CPP sample bank, or get full access to all 151 practice questions in the simulator.

Create account