C++ Interview Questions and Answers for Freshers

C++ is the language most students use for DSA and competitive coding, so interviewers expect you to know it well. In service company interviews they ask OOP basics in C++, and in product company rounds they check if you understand pointers, virtual functions and the STL you use every day. Below are the 40 most asked C++ interview questions for freshers with short answers you can speak in an interview.
Read each answer, then write a small program for it. Virtual functions and copy constructors only become clear when you see the output yourself.
Quick summary
| Section | Questions | Focus |
|---|---|---|
| Basics | Q1 to Q7 | C vs C++, references, namespaces |
| Classes and objects | Q8 to Q15 | Access specifiers, constructors, this |
| OOP pillars | Q16 to Q23 | Inheritance, overloading, overriding |
| Virtual functions | Q24 to Q28 | vtable, pure virtual, abstract class |
| Memory and pointers | Q29 to Q33 | new vs malloc, smart pointers |
| STL | Q34 to Q37 | vector, map, set, iterators |
| Output questions | Q38 to Q40 | Predict the result |
Basics
1. What is C++?
A general-purpose language created by Bjarne Stroustrup as an extension of C. It supports procedural, object-oriented and generic programming.
2. Difference between C and C++?
C is procedural; C++ supports OOP (classes, inheritance, polymorphism). C++ adds references, function overloading, namespaces, exceptions, templates and the STL.
3. Difference between struct and class in C++?
Both can have data and functions. Members of a struct are public by default; members of a class are private by default.
4. What is a reference? How is it different from a pointer?
A reference is another name for an existing variable. It must be initialised, cannot be null and cannot be changed to refer to something else. A pointer can be null, reassigned and needs * to access the value.
5. What is a namespace?
A named scope that prevents name clashes. std is the namespace of the standard library, so we write std::cout or using namespace std;.
6. What is the difference between endl and \n?
Both add a new line. endl also flushes the output buffer, so it is slower inside big loops.
7. What is an inline function?
A request to the compiler to replace the function call with the function body. Useful for small functions; it saves call overhead.
Classes and objects
8. What is a class and an object?
A class is a blueprint that groups data and functions. An object is an instance of a class that takes memory.
9. What are access specifiers?
public (accessible everywhere), private (only inside the class), protected (inside the class and derived classes).
10. What is a constructor? What are its types?
A special function with the same name as the class, called automatically when an object is created. Types: default, parameterised and copy constructor.
11. What is a copy constructor? When is it called?
It creates an object as a copy of another object of the same class. Called when an object is passed or returned by value, or initialised from another object.
12. Shallow copy vs deep copy?
Shallow copy copies pointer addresses, so both objects share the same memory. Deep copy allocates new memory and copies the data. You need a deep copy when the class holds dynamic memory.
13. What is a destructor?
A function with ~ClassName() that runs when an object goes out of scope. It frees resources. There is only one destructor and it takes no arguments.
14. What is the this pointer?
A hidden pointer inside every non-static member function that points to the current object.
15. What is a static member?
A static data member is shared by all objects of the class. A static member function can be called with the class name and can only use static members.
OOP pillars
16. What are the four pillars of OOP?
Encapsulation, abstraction, inheritance and polymorphism.
17. What is inheritance? Name its types.
One class gets the properties of another. Types: single, multiple, multilevel, hierarchical and hybrid.
18. What is the diamond problem? How is it solved?
In multiple inheritance, a class gets two copies of a common base class. It is solved with virtual inheritance: class B : virtual public A.
19. What is polymorphism?
One name, many forms. Compile-time polymorphism: function and operator overloading. Run-time polymorphism: function overriding with virtual functions.
20. Function overloading vs function overriding?
Overloading: same name, different parameters, in the same class, decided at compile time. Overriding: same name and parameters in a derived class, decided at run time.
21. What is operator overloading?
Giving a new meaning to an operator for a user-defined type, for example adding two Complex objects with +.
22. What is a friend function?
A non-member function that can access private and protected members of a class because the class declares it as friend.
23. What is encapsulation vs abstraction?
Encapsulation wraps data and functions together and hides data with private members. Abstraction shows only what is needed and hides how it works.
Virtual functions
24. What is a virtual function?
A base class function declared with virtual so that a call through a base pointer runs the derived class version at run time.
25. How do virtual functions work internally?
Each class with virtual functions has a vtable (table of function addresses). Each object has a hidden vptr pointing to its class vtable. The call is resolved through it at run time.
26. What is a pure virtual function and an abstract class?
A pure virtual function has no body: virtual void draw() = 0;. A class with at least one is abstract and cannot be instantiated.
27. Why should a base class destructor be virtual?
So that deleting a derived object through a base pointer calls the derived destructor too. Otherwise memory can leak.
28. Can a constructor be virtual?
No. The object is not fully built when the constructor runs, so the vptr is not ready yet.
Memory and pointers
29. new vs malloc?new calls the constructor, returns the correct type and throws an exception on failure. malloc only gives raw memory, returns void* and returns NULL on failure. Pair new with delete and malloc with free.
30. delete vs delete[]?delete frees a single object; delete[] frees an array created with new[].
31. What is a memory leak?
Memory allocated with new that is never freed with delete. The program keeps using more memory.
32. What are smart pointers?
Classes that free memory automatically. unique_ptr (single owner), shared_ptr (shared owner with a count), weak_ptr (does not increase the count, breaks cycles).
33. What is a dangling pointer?
A pointer to memory that is already freed. Set it to nullptr after delete.
STL
34. What is the STL?
The Standard Template Library: containers (vector, list, map, set), algorithms (sort, find) and iterators.
35. vector vs array?
An array has a fixed size. A vector grows automatically, knows its size and works with STL algorithms.
36. map vs unordered_map?
map keeps keys sorted (red-black tree, O(log n)). unordered_map uses hashing (average O(1)) and keeps no order.
37. What are templates?
Code that works for any data type, for example template <typename T> T maxOf(T a, T b). The STL is built with templates.
Output questions
38. What is the output?int a = 5; int &r = a; r++; cout << a;
Output: 6. r is another name for a.
39. What is the output?
A base class show() is not virtual, a derived class overrides it, and you call it through a base pointer to a derived object.
Output: the base class version runs, because without virtual the call is decided at compile time.
40. What is the output?cout << sizeof(char) << sizeof(bool);
Output: 11 on most systems (both are 1 byte).
How to prepare in 3 days
| Day | What to do |
|---|---|
| Day 1 | Q1 to Q15, write classes with all three constructors and a destructor |
| Day 2 | Q16 to Q28, write one program for overriding with and without virtual |
| Day 3 | Q29 to Q40, practise STL with 5 easy DSA problems |
Keep preparing
- C programming interview questions
- OOPs interview questions
- DSA interview questions
- Java interview questions
- Free notes and eBooks
Save this page and revise it the night before your interview. All the best.




Doubts? Ask here