This comprehensive master repository contains 100+ verified C++ technical interview and viva questions frequently asked by premier recruiters, tech giants (MAANG), gaming studios, and university examination boards.
new/delete and malloc/free?
Answer: new is a C++ type-safe operator that calculates object size automatically, allocates memory, and calls the constructor. malloc() is a C library function that allocates uninitialized raw bytes without calling constructors. Similarly, delete calls the object's destructor before releasing memory, whereas free() releases memory blindly.
Answer: The VTable is a static table of function pointers created by the compiler for any class having at least one virtual function. Every object instance contains an invisible 8-byte pointer (vptr) pointing to this table. When a virtual function is called via a base pointer, C++ dereferences vptr to dispatch to the correct derived implementation dynamically at runtime.
unique_ptr, shared_ptr, and weak_ptr?
Answer:
• std::unique_ptr: Models exclusive ownership (cannot be copied, only moved via std::move). Zero runtime overhead.
• std::shared_ptr: Models shared ownership via an atomic reference-counted control block.
• std::weak_ptr: Non-owning observer of a shared_ptr. Prevents cyclic dependency memory leaks.
virtual?
Answer: If a base class destructor is non-virtual, deleting a derived object via a base pointer (delete basePtr;) calls ONLY the base class destructor. The derived class destructor is bypassed, permanently leaking all derived member resources. Making the destructor virtual ensures the complete destructor chain runs from child to parent.
Answer: RAII (Resource Acquisition Is Initialization) ties resource lifetime to the scope of a local stack object. In languages with finally blocks (like Java), developers must remember to write cleanup boilerplate in every function. In C++, stack unwinding guarantees that destructors run automatically upon scope exit under ALL conditions, including early returns, breaks, and exceptions.
Answer: C++ uses virtual base classes (class B : virtual public A). When classes B and C inherit virtually from A, derived class D contains only a single shared instance of A, completely eliminating duplicate data and ambiguous compiler calls.
Answer: Object slicing occurs when a derived class object is assigned or passed by value to a base class variable (Base b = derivedObj;). The derived portions of the object are sliced away, leaving only the base portion and losing all polymorphic virtual behavior. Always pass by reference or pointer (const Base&) to prevent slicing.
constexpr and consteval?
Answer: A constexpr function can execute at compile time if arguments are constant, but falls back to normal runtime execution if called with runtime values. A C++20 consteval function is an immediate function that MUST evaluate at compile time; calling it with runtime variables generates a compile-time error.
Answer: Concepts specify compile-time predicate constraints on template arguments (e.g. template <std::integral T>). Prior to C++20, developers used arcane SFINAE and std::enable_if hacks. Concepts make template constraints readable, integrate with IDEs, and generate clear, actionable error messages.
std::vector preferred over raw pointers and other STL containers?
Answer: std::vector stores its elements in contiguous heap RAM, allowing hardware CPU cache prefetching to operate at maximum efficiency. It guarantees $O(1)$ random access, amortized $O(1)$ push_back, manages its own memory via RAII, and works seamlessly with all STL algorithms.
Boost your C++ proficiency by studying these closely connected core modules:
Solve practical lab code examples.
Debug common pitfalls and compiler traps.
Quick revision cheat sheet before interviews.