Top 100+ C++ Interview Questions & Answers: From Campus Placements to Senior Architect

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.

Section 1: Language Basics & Memory (Q1 - Q25)

Q1: What are the differences between 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.

Q2: What is the Virtual Method Table (VTable) and how does it work?

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.

Q3: What is the difference between 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.

Q4: Why are base class destructors required to be 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.

Q5: What is RAII and why is it superior to manual cleanup or finally blocks?

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.

Section 2: Object-Oriented Architecture (Q26 - Q50)

Q6: How does C++ resolve the Diamond Problem in Multiple Inheritance?

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.

Q7: What is Object Slicing in C++?

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.

Section 3: Modern C++ Standards (Q51 - Q100+)

Q8: What is the difference between 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.

Q9: What are C++20 Concepts and how do they replace SFINAE?

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.

Q10: Why is 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.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is the most frequently asked C++ interview question for freshers?

The most frequent question is: 'What is the difference between a Virtual Function, a Pure Virtual Function, and why must a base class destructor be declared virtual?'

Q2. How should I explain Move Semantics in an interview?

Explain that Move Semantics (introduced in C++11) allows an object to take ownership of resources from an rvalue (temporary object) via pointer swapping in O(1) time, rather than allocating memory and performing a slow O(N) deep copy.

Explore Related C++ Tutorials & Next Steps

Boost your C++ proficiency by studying these closely connected core modules:

50+ Practice Programs

Solve practical lab code examples.

Code Challenges

Debug common pitfalls and compiler traps.

Syntax Reference

Quick revision cheat sheet before interviews.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!