Free CPA-21-02 sample questions
Real questions from the Cpa – C++ Certified Associate 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 building a high-performance logging framework. To avoid ambiguity and ensure type safety, they want to prevent implicit conversions from integers to their custom `LogLevel` enum class. Which code snippet correctly achieves this?
A junior developer wrote the following function to process a configuration file. During a code review, a senior developer points out a potential issue with exception safety. What is the primary problem? ```cpp #include #include #include void processConfig(const char* filename) { std::ifstream* file = new std::ifstream(filename); if (!file->is_open()) { delete file; throw std::runtime_error("Cannot open file"); } // Assume this function can throw an exception parseData(*file); delete file; } ```
An architect is designing a system with different types of user accounts (e.g., `AdminUser`, `GuestUser`, `PowerUser`), all derived from a common `User` base class. The system needs a factory function that can create any type of user account based on a string identifier and return a pointer to the base `User` class. However, the architect wants to prevent clients from accidentally slicing the objects if they are copied. Which of the following are necessary steps to achieve this design? (Select TWO)
Consider the following macro definition. What is the output when the `main` function is executed? ```cpp #include #define SQUARE(x) x * x int main() { int a = 4; std::cout << SQUARE(a + 1); return 0; } ```
True or False: In C++, if a class has a user-defined destructor, the compiler will no longer implicitly generate a default constructor.
**Case Study: A Game Development Studio** A game development studio is creating a new 3D rendering engine. The engine needs to manage various types of game objects, such as `Player`, `Enemy`, and `Scenery`. Each object has a position in 3D space, but also contains type-specific data and behavior. For performance reasons, all game objects are stored in a single `std::vector ` and are updated and rendered in a tight loop. **Current Situation** The initial design has a `GameObject` base class with virtual functions for `update()` and `render()`. When a `GameObject` is destroyed (e.g., an enemy is defeated), its memory must be reclaimed. The team has observed occasional crashes and memory corruption issues during long gameplay sessions, particularly when large numbers of objects are created and destroyed rapidly. **Requirements** 1. The system must correctly manage the lifecycle of all derived `GameObject` types. 2. When an object is deleted through a `GameObject*` pointer, the complete object, including derived parts, must be destroyed. 3. The design should be extensible to allow for new `GameObject` types in the future without modifying the main game loop. 4. The cause of the memory corruption must be eliminated. **Proposed Code Snippet** ```cpp class GameObject { public: // Constructor and other members... virtual void update() = 0; virtual void render() = 0; // Missing destructor }; class Enemy : public GameObject { ... }; // In game loop: delete allGameObjects[i]; // i is the index of the object to be removed ``` Which change is most critical to fix the memory corruption and meet the design requirements?
What is the decimal value of the variable `result` after the following code executes? ```cpp int flags = 0b10110; // 22 in decimal int mask = 0x1A; // 26 in decimal, 0b11010 in binary int result = flags & mask; ```
A programmer needs to write a function that accepts an array of integers and its size, and it must modify the original array elements by doubling their value. Which function signature is the most appropriate and conventional C++ approach for this task?
What is the final value of `x` after the following code snippet is executed? ```cpp int x = 10; int y = 5; x = (y > 3) ? ( (y 0) ? ++y : --y ); ```
10 more free samples are waiting
Create a free account to unlock the whole CPA-21-02 sample bank, or get full access to all 275 practice questions in the simulator.