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.
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: ?)
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?
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?
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; } ```
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.
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; } ```
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?
**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?
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.