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 software architect is designing a system component that models a production line. The component must handle frequent additions and removals of items from both the front and the back of the line. Additionally, random access to any item in the line using an index is a frequent operation for quality control checks. Which STL container is the most suitable choice for this scenario, considering all requirements?
A junior developer wrote the following code to remove all even numbers from a vector. After execution, they are surprised to find that while some even numbers are gone, the vector's size has not changed: the program prints `Vector size: 9` followed by `1 3 5 7 9 6 7 8 9` (output from GCC; the last four values are unspecified). What is the fundamental misunderstanding in this code? ```cpp #include #include #include int main() { std::vector numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; std::remove_if(numbers.begin(), numbers.end(), [](int n){ return n % 2 == 0; }); std::cout << "Vector size: " << numbers.size() << std::endl; for(int n : numbers) { std::cout << n << " "; } return 0; } ```
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?
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.