Free CPP sample questions
Real questions from the C++ Certified Professional Programmer practice bank, with the correct answer and an explanation for each one. No junk, no filler.
Try them in the simulator Same questions, with study, timed and flashcard modes.
Showing 10 of 20 free sample questions.
A developer is implementing a cache using `std::map` where the key is a custom struct `UserSession`. The code fails to compile with an error deep inside the STL headers related to comparison. What is the most likely missing component in the `UserSession` struct definition that is required for it to be a key in a `std::map`? ```cpp #include #include struct UserSession { int userId; std::string sessionId; // Missing component }; int main() { std::map userCache; // ... code to populate map return 0; } ```
**Case Study:** A financial technology company is developing a high-frequency trading platform. The system receives two separate, large streams of real-time trade data from two different exchanges, `exchangeA_trades` and `exchangeB_trades`. Both streams are delivered as sorted `std::vector ` objects, ordered by trade ID. The `Trade` struct has a unique `tradeId` and other financial data. The system needs to perform three critical tasks with maximum efficiency: 1. Create a single, consolidated list of all trades from both exchanges, sorted by `tradeId`. 2. Generate a report of trades that appeared on *both* exchanges (i.e., common trades). 3. Generate a report of trades that appeared on `exchangeA` but *not* on `exchangeB`. Given that the input vectors are already sorted and performance is paramount, which sequence of STL algorithms represents the most efficient approach to accomplish all three tasks?
A library designer has provided a generic template function `process()` and a specific overload for `const char*`. A developer then calls this function with a string literal. What is the output of the following program? ```cpp #include template void process(T value) { std::cout << "Template version\n"; } void process(const char* value) { std::cout << "Non-template overload\n"; } int main() { process("hello"); return 0; } ```
Given a `std::vector` named `source` and a smaller `std::vector` named `pattern`, what is the correct way to use an STL algorithm to find the beginning of the *last* occurrence of the `pattern` sequence within the `source` sequence?
True or False: Once the `std::fixed` stream manipulator is used on `std::cout`, it remains in effect for all subsequent floating-point output to `std::cout` until it is explicitly cleared by another manipulator like `std::defaultfloat`.
Analyze the following C++ code. What will be the final contents of the `data` vector after the `std::transform` algorithm is executed? ```cpp #include #include #include #include int main() { std::vector data(5); std::iota(data.begin(), data.end(), 1); // Fills data with 1, 2, 3, 4, 5 int multiplier = 3; std::transform(data.begin(), data.end(), data.begin(), [multiplier](int val) { if (val % 2 != 0) { return val * multiplier; } return val; }); for (int val : data) { std::cout << val << " "; } return 0; } ```
A developer is searching for a value in a sorted `std::vector `. If the exact value is not found, they need to find the position where the value *could be inserted* while maintaining the sort order. Consider the following code: ```cpp #include #include #include int main() { std::vector data = {10, 20, 30, 50, 60}; auto it = std::lower_bound(data.begin(), data.end(), 40); std::cout << *it; return 0; } ``` What is the output of this program?
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?
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: ?)
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.