The 50+ Most Common C++ Interview Questions
The C++ programming language is used for a wide range of technical needs including client-server systems, object-oriented databases, real-time system designing, and even disciplines like neural networking and parallel programming. Used in many large software companies, C++ may be a useful skill to add to your resume.
Prepare for meeting with a prospective hiring manager by studying these common C++ interview questions.
Here are over 50 of the most asked questions in a C++ job interview.
Last Updated October 2022
Classroom and Hands-on sessions- Features of C++ 11 , Exception Handling and STL – for Both Academics and Industry | By Abdul Bari
Explore Course1. What is the difference between C and C++?
C is a Procedural Oriented Programming (POP) language, whereas C++ language supports Object Orientation. Additionally, C++ supports features like templates, inheritance, function overloading, virtual functions, friend functions, and references. These features are not supported in C.
2. What does OOPS stand for?
OOPS stands for Object-Oriented Programming System, which is how C++ is categorized.
3. What is function overloading?
Function overloading is the concept of having many functions with the same name. These functions should have different parameters for different types of functioning.
4. What is operator overloading?
Manipulating the operations of a pre-existing operator to achieve different operations.
5. What is a class in C++?
A class is a collection of different variables and different functions. The variables are called data members and the functions are called member functions.
6.What is a Friend Class?
If a class is mentioned as a friend class to another class, then it can access private and protected members of the other class.
Example:
class ClassA
{
private:
int a;
public:
ClassA()
{
a=10;
}
friend class FriendClass;
};
class FriendClass
{
private:
int b;
public:
void printClassA(ClassA& p)
{
cout<<“a=”<<p.a<<endl;
}
};
int main()
{
ClassA x;
FriendClass y;
y.printClassA();
return 0;
}
7. How can we access data members and member functions of a class?
Dot-Operator( . ) helps to access data members and member functions of a class.
8. What is inheritance?
Inheritance is the feature in which an object acquires the properties of another class.
9. What are the different types of inheritances?
There are different types of Inheritances-
- Single inheritance
- Multiple inheritance
- Multi-level inheritance
- Hierarchical inheritance
- Hybrid inheritance
10. What is single inheritance?
In Single Inheritance, there is one derived class and one base class.
11. What is multilevel inheritance?
In multi-level inheritance, a class is derived from another derived class.
12. What is multiple inheritance?
This is when a class can be derived with more than one parent class.
13. What is hierarchical inheritance?
In hierarchical inheritance, many classes are derived from a single parent class.
14. What is Hybrid Inheritance?
This is a combination of more than one inheritance, called hybrid inheritance. It is also called virtual inheritance.
15. What is Template?
A template is a method for creating generic classes and generic functions.
16. What are access specifiers? What are the different types?
Access Specifiers define the accessibility of the members of the class. There are three types of access specifiers:
- Private – they are only accessible from inside of the class
- Protected – like private specifiers but with an extra feature — they are also accessible in derived classes
- Public – they are accessible from both inside and outside the class
17. What is a friend function?
A friend function to a class has access to all the members of the class, even the private ones. We declare it outside the class.
18. What is a virtual function?
A virtual function is a function declared as a member function of a class, but its definition is inside its derived class.
19. What is data hiding?
Data hiding is hiding data in objects. This is generally done by having private data members of the class.
20. What is a constructor?
A constructor is a special member function of a class. When we declare an object, it initializes the data member.
21. What is a Copy Constructor?
A Copy Constructor is a member function of a class. When called while creating a new object of the class, it initializes the object using another object of the same class. In order to use a Copy Constructor, there should be already an existing object of that class.
Example:
class Copy
{
private:
int a;
public:
Copy(int z)
{
a=z;
}
Copy( Copy &x)
{
a=x.a;
}
void print()
{
cout<<“Value of ‘a’ is ”<<a<<endl;
}
};
int main()
{
Copy p(10);
p.print();
Copy q = p;
q.print();
}
22.What is the use of Static Member Functions?
If you make a member function of a class static, you make it independent of any object of the class. Function Calls of a static member function can be made even if there doesn’t exist any object of that class.
23. In how many ways is scope resolution used?
There are many ways to use scope resolution. Some use examples include:
- To define a member function outside the class
- To access data members of various classes while in inheritance
24. What are inline functions?
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
25. State the advantage of inline functions.
The inline functions increase the execution time of the program.
26. What is the difference between structure and class?
The variables of the structures are public, whereas the data member of the class can be made as private.
27.What is the difference between a Local and a Global Variable?
A variable, when declared inside a code block, is called a local variable. We can use this Local Variable only inside the code block. A global variable is declared on the top of the program, before all the function definitions. It can be accessed from anywhere in the code.
28.What is Compilation Time and Run Time?
The time Compiler takes for compiling a piece of code is called Compilation Time of the code. The time taken by the program to run is called Run Time.
29.What is an Assignment Operator?
We use Assignment Operator for assigning value to a variable. It is denoted by ‘=’.
30. What is function overriding?
Re-defining the member function of base class in the derived class is function overriding.
31. Explain polymorphism.
If an object follows polymorphism, then it acts differently in different conditions. There are two types of polymorphism:
- Compile-time polymorphism (static binding)
- Run-time polymorphism (dynamic binding)
32. What is inner class?
A nested class is a class which is declared inside another class.
33. Explain exception handling.
Exception handling is used when some exception is encountered in the program. Three keywords are used for exception handling: try, catch, throw.
34. What is a destructor?
A destructor is a special member function. The Destructor destructs the object and frees up space.
35. What is the use of a virtual destructor?
When we try to delete an object of the derived class using a base class pointer, some undefined is encountered. To prevent this, a virtual destructor should be defined in the base class.
36. What is the full form of STL?
STL stands for Standard Template Library.
37. Can we store duplicate values in a set?
No, we cannot store duplicate values in a set.
38. What type of data structure does map use?
Map uses BST as its data structure.
39. Can we initialize a vector with an array?
Yes, we can initialize a vector with an array.
40. What are some components of STL?
Some of the components of STL are iterators, containers, and algorithms.
41. How is ‘final’ used?
We add ‘final’ after the function name at the time of declaring to prevent overriding in derived classes.
42. Write a C++ program for finding the length of a string using a string iterator.
# include<iostream>
using namespace std;
int main()
{
string str="welcome";
string::iterator it;
int count=0;
for(it=str.begin();it!=str.end();it++)
{
cout<<"length is"<<count<<endl;
}
return 0;
}
43. Write a C++ program to change the given string to all upper cases.
#include<iostream>
using namespace std;
int main()
{
string str="wELcoMe7";
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>=97 && str[i]<=122)
{
str[i]=str[i]-32;
}
}
cout<<str<<endl;
return 0;
}
44. Write a C++ program to check if a string is a palindrome.
# include<iostream>
using namespace std;
int main()
{
string str="MADAM";
string rev="";
int len=(int)str.length();
rev.resize(len);
for(int i=0;j=len-1;i<len;i++;j--)
{
rev[i]=str[j];
}
rev[len]='\0';
if(str.compare(rev)==0)
cout<<"palindrome"<<endl;
else
cout<<"not a palindrome"<<endl;
return 0;
}
45. Write a C++ program to show function templates.
# include<iostream>
using namespace std;
template<class t>
t maxim(t a,t b)
{
return a>b?a:b;
}
int main()
{
cout<<maxim(12,14)<<endl;
cout<<maxim(2.3,1.4)<<endl;
cout<<maxim(2.3f,5.6f)<<endl;
return 0;
}
46. Write a C++ program to show pointers to an object.
# include<iostream>
using namespace std;
class rectangle
{
public:
int length;
int breadth;
int area()
{
return length*breadth;
}
int perimeter()
{
return perimeter 2*(length+breadth);
}
};
int main()
{
rectangle r1;
rectangle *ptr;
ptr=&r1;
ptr->length=10;
ptr->breadth=5;
cout<<ptr->area()<<endl;
cout<<ptr->perimeter()<<endl;
}
47. Write a C++ program showing operator overloading using friend functions.
# include<iostream>
using namespace std;
class complex
{
private:
int real;
int img;
public:
complex(int r=0,i=0)
{
real=r;
img=i;
}
void display()
{
cout<<real<<"+i"<<img;
}
friend complex opreator+(complex c1,complex c2);
};
complex operator+(complex c1,complex c2)
{
complex temp;
temp.real=c1.real+c2.real;
temp.img=c1.img+v2.img;
return temp;
}
int main()
{
complex c1(5,3),c2(10,5),c3;
c3=c1+c2;
c3.display();
}
48. Write a C++ program using access specifiers.
# include<iostream>
using namespace std;
class base
{
public:
int a;
void display()
{
cout<<"display of base"<<a<<endl;
}
};
class derived:public base
{
public:
void show()
{
cout<<"show of derived"<<endl;
}
};
int main()
{
derived d;
d.a=100;
d.display();
d.show();
}
49. Write a C++ program showing inheritance.
# include<iostream>
using namespace std;
class rectangle
{
private:
int length;
int breadth;
public:
void setlength(int l)
{
if(l>0)
length=l;
else
length=1;
}
void setbreadth( int b)
{
if(b>0)
breadth=b;
else
breadth=1;
}
int getlength()
{
return length;
}
int getbreadth()
{
return breadth;
}
int area()
{
return length*breadth;
}
int perimeter()
{
return 2*(length+breadth);
}
};
int main()
{
rectangle r1;
r1.setlength(10);
r1.setbreadth(5);
cout<<"r1.area()<<endl;
cout<<"r1.perimeter()<<endl;
cout<<"length"<<r1.getlength()<<endl;
cout<<"breadth"<<r1.getbreadth()<<endl;
}
50. Write a C++ program showing polymorphism.
# include<iostream>
using namespace std;
class car
{
public:
virtual void start()
{
cout<<"car started"<<endl;
}
};
class innova:public car
{
public:
void start()
{
cout<<"innova started"<<endl;
}
};
class swift:public car
{
public:
void start()
{
cout<<"swift started"<<endl;
}
};
int main()
{
car *p=new innova();
ptr->start();
p=new swift();
ptr->start();
}
51. Write a C++ example program for nested classes.
# include<iostream>
using namespace std;
class outer
{
class inner;
public:
void fun()
{
i.display();
}
class inner
{
public:
void display()
{
cout<<"display of inner"<<endl;
}
};
inner i;
};
int main()
{
outer::inner i;
}
52. Write a C++ program to show throw and catch between functions.
# include<iostream>
using namespace std;
int division(int a,int b)
{
if(b==0)
throw 1;
return a/b;
}
int main()
{
int x=10,y=2,z;
try
{
if(y==0)
throw 1;
z=x/y;
cout<<z<<endl;
}
catch(int e)
{
cout<<"division by zero"<<e<<endl;
}
cout<<"bye"<<endl;
}
53. Write a C++ program using vectors from STL.
# include<vector>
using namespace std;
int main()
{
vector<int> v={2,4,6,8,10};
v.push_back(20);
v.push_back(30);
vector<int>::iterator itr;
cout<<"using iterator"<<endl;
for(itr=v.begin();itr!=v.end();itr++)
cout<<++*itr<<endl;
cout<<"using for each loop"<<endl;
for(int x:v)
cout<<x<<endl;
}
54. Write a C++ program using map from STL.
# include<iostream>
# include<map>
using namespace std;
int main()
{
map<int,string> m;
m.insert(pair<int,string>(1,"john"));
m.insert(pair<int,string>(2,"ravi"));
m.insert(pair<int,string>(3,"khan"));
map<int,string>::iterator itr;
for(itr=m.begin();itr!=m.end();itr++)
{
cout<<itr->first<<" "<<itr->second<<endl;
}
map<int,string>::iterator itr1;
itr1=m.find(2);
cout<<"value found is"<<endl;
cout<<itr1->first<<" "<<itr1->second<<endl;
}
55. Write a C++ program showing the use of auto.
# include<iostream>
using namespace std;
float fun()
{
return 2.34f;
}
int main()
{
double d=12.3f;
int i=9;
auto x=2*d+i;
cout<<x;
}
56. Write a C++ program to show Lambda functions.
# include<iostream>
using namespace std;
int main()
{
[](int x,int y)
{
cout<<"sum is "<<x+y<<endl;
}
(10,30);
}
57. Write a C++ program for ellipses.
#include<iostream>
#include<cstdarg>
using namespace std;
int sum(int n,...)
{
va_list list;
va_start(list,n);
int x;
int s=0;
for(int i=0;i<n,i++)
{
x=va_arg(list,int);
s+=x;
}
return s;
}
int main()
{
cout<<sum(3,10,20,30)<<endl;
cout<<sum(5,1,2,3,4,5)<<endl;
}
58. Write a C++ program to write in a file.
# include<iostream>
# include<fstream>
using namespace std;
int main()
{
ofstream ofs("my.text",ios::trunc);
ofs<<"john"<<endl;
ofs<<25<<endl;
ofs<<"cs"<<endl;
ofs.close();
}
59. Write a C++ program for reading a file.
# include<iostream>
# include<fstream>
using namespace std;
int main()
{
ifstream ifs;
ifs.open("my.txt");
if(ifs_open())
cout<<"file is opened"<<endl;
string name;
int roll;
string branch;
ifs>>name>>roll>>branch;
ifs.close();
cout<<"name"<<name<<endl;
cout<<"roll"<<rollendl;
cout<<"branch"<<branch<<endl;
}
Recommended Articles
Top courses in C++ (programming language)
C++ (programming language) students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.