Here are the Top 30 C++ Interview Questions and Answers categorized from basic to advanced, perfect for job interviews and technical rounds.
Top 30 C++ Interview Questions and Answers
🟢 Basic Level (1–10)
1. What is C++?
C++ is a general-purpose, object-oriented programming language developed by Bjarne Stroustrup. It supports both procedural and object-oriented programming, making it a multi-paradigm language.
2. What are the key features of C++?
- OOP support (classes, inheritance, polymorphism)
- Function and operator overloading
- Templates
- Exception handling
- STL (Standard Template Library)
3. What is the difference between C and C++?
Feature | C | C++ |
---|---|---|
Paradigm | Procedural | Multi-paradigm |
OOP | No | Yes |
Function Overloading | No | Yes |
Encapsulation | No | Yes |
4. What is the use of iostream
?
#include <iostream>
is used for input/output operations in C++ using cin
and cout
.
5. What is the difference between cin
and cout
?
cin
is used for input.cout
is used for output.
Both are part of theiostream
library.
6. What are the data types in C++?
- Primitive:
int
,float
,double
,char
,bool
- Derived:
array
,pointer
- User-defined:
struct
,class
,union
,enum
7. What is the difference between ==
and =
?
=
is an assignment operator.==
is a comparison operator.
8. What is a pointer in C++?
A pointer is a variable that stores the memory address of another variable.
9. What is a reference variable?
A reference variable is an alias for another variable, created using &
.
int a = 10;
int &ref = a;
10. What is a constructor?
A constructor is a special function that is automatically called when an object is created.
Top 30 C++ Interview Questions and Answers
🟡 Intermediate Level (11–20)
11. What is function overloading?
Function overloading allows multiple functions with the same name but different parameters.
12. What is operator overloading?
Operator overloading allows you to define custom behavior for operators.
class A {
int x;
public:
A(int i): x(i) {}
A operator+(A a) { return A(x + a.x); }
};
13. What is inheritance in C++?
Inheritance is the process where one class (child) inherits properties of another class (parent).
14. Types of inheritance?
- Single
- Multiple
- Multilevel
- Hierarchical
- Hybrid
15. What is polymorphism?
Polymorphism means having many forms. It allows the same function name to behave differently for different inputs.
16. Difference between compile-time and runtime polymorphism?
- Compile-time: Function overloading, Operator overloading
- Run-time: Virtual functions
17. What is a virtual function?
A virtual function allows run-time polymorphism and is declared using the virtual
keyword.
18. What is a pure virtual function?
virtual void show() = 0;
It makes a class abstract, and derived classes must override it.
19. What is encapsulation?
Encapsulation is data hiding by wrapping data and functions into a single unit (class).
20. What is abstraction?
Abstraction means hiding internal details and showing only relevant information.
Top 30 C++ Interview Questions and Answers
🔴 Advanced Level (21–30)
21. What is the Standard Template Library (STL)?
STL is a library of generic classes and functions like vector
, map
, set
, queue
, etc.
22. Difference between class and structure?
Feature | Class | Structure |
---|---|---|
Default access | private | public |
OOP Support | Yes | Partial |
23. What are access specifiers?
public
: Accessible anywhereprivate
: Only within classprotected
: Within class and derived class
24. What are the types of constructors?
- Default constructor
- Parameterized constructor
- Copy constructor
25. What is a destructor?
A destructor is a special function with ~ClassName()
used to release memory.
26. Difference between new
and malloc()
?
new
calls constructor;malloc
doesn’tnew
is C++;malloc
is C
27. What is the this
pointer?
It is a pointer that refers to the current object.
28. What are smart pointers in C++?
Smart pointers automatically manage memory (shared_ptr
, unique_ptr
, weak_ptr
).
29. What is exception handling?
Mechanism to handle runtime errors using try
, catch
, throw
.
30. What is a template in C++?
Templates allow writing generic code.
template <typename T>
T add(T a, T b) {
return a + b;
}
Top 30 C++ Interview Questions and Answers
Table of Contents
Top 30 C++ Interview Questions and Answers