Skip to content

Systems · updated 26 November 2026

C++ is used where performance and control matter: games, engines, finance and embedded software. These scenarios test lifetime, ownership and correctness.

Top 10 C++ scenario questions

Try each one yourself first, then open the model answer.

1. A program crashes after you return a reference to a local variable. Why?

Show answer

The local is destroyed when the function returns, so the reference dangles and using it is undefined behaviour. Return by value and let copy elision or move semantics keep it efficient.

2. A codebase uses new and delete everywhere and leaks memory. How would you improve it?

Show answer

Apply RAII: use containers and smart pointers so destructors release resources. Prefer unique_ptr for single ownership and shared_ptr only when ownership is truly shared.

3. Two objects hold shared_ptr to each other and are never freed. What do you do?

Show answer

The reference cycle keeps both counts above zero. Make one side a weak_ptr.

4. You delete a derived object through a base pointer and resources leak. Why?

Show answer

Without a virtual destructor in the base class the behaviour is undefined and the derived destructor may not run. Make the base destructor virtual.

5. Iterating over a vector crashes after push_back inside the loop. What happened?

Show answer

Reallocation invalidated iterators and references. Reserve capacity in advance, use indexes carefully, or collect new items separately and append afterwards.

6. A class managing a raw buffer is copied and later freed twice. How do you fix it?

Show answer

The default copy duplicates the pointer. Follow the rule of zero by using std::vector, or define or delete the copy and move operations consistently.

7. Two threads write to the same variable without synchronisation. Is that fine if it is just a flag?

Show answer

No. A data race is undefined behaviour. Use std::atomic or protect it with a mutex.

8. A function takes a large vector by value and is slow. What would you change?

Show answer

Pass by const reference if it only reads. Take by value and move when the function needs to keep its own copy.

9. A bug appears only in release builds. How do you approach it?

Show answer

Suspect undefined behaviour or uninitialised data that optimisation exposes. Build with warnings and sanitisers such as AddressSanitizer and UndefinedBehaviorSanitizer, and debug with symbols.

10. Passing a derived object by value to a function loses its overridden behaviour. Why?

Show answer

Object slicing: only the base part is copied. Pass by reference or pointer to keep polymorphism.

Ready to build something great?

Book your free 30-minute consultation today. No obligation, just honest advice.