preloader
C++ Interview Questions

Top C++ Interview Questions and Answers for Freshers/Exp

author image

As C++ is an important programming language, we have provided C++ Interview Questions For freshers and experienced professionals who are preparing for a job interview. Here you can go through the Top C++ Interview questions and answers and prepare well for the interview. Here we have covered basic, intermediate, and advanced interview questions with coding examples to make you properly understand the question and concept so learn from these questions and prepare yourself.

About C++: It is a cross-platform programming language that is used to create high-performance applications. Bjarne Stroustrup developed C++, as a superset of the C language. It provides a high level of control to developers over system resources and memory.

C++ Interview Questions

1. Define C++?

2. Name different data types present in C++?

3. Differentiate between C and C++?

4. Define class and object in C++?

5. Differentiate between Structure and class?

6. Explain operator overloading?

7. Explain polymorphism in C++?

8. What is a constructor in C++?

9. Explain the virtual function

10. Explain friend class and friend function?

11. Explain C++ access specifiers?

12. Explain the inline function

13. Explain a reference in C++?

14. Explain abstraction in C++?

15. Tell whether the deconstructor overloading is possible or not?

16. What do you understand by call by reference and call by value?

17. Explain abstract class and its use?

18. Explain destructors in C++?

19. Explain static members and their functions?

20. What is an inheritance?

21. Explain a copy constructor?

22. Differentiate shallow copy and deep copy?

23. Explain virtual functions and pure virtual functions?

24. Tell the function of the keyword "Auto"?

25. Can a virtual function be called from a constructor?

26. Explain void pointers?

27. Explain pointer in C++?

28. Show the code of allocating and deallocating memory in C++?

29. Explain a token in C++ with examples

30. Can we call C++ OOPS?

31. Explain Namespace?

32. Explain a class template?

33. Explain the function of the keyword “Volatile”?

34. Explain and name some storage classes in C++?

35. Are we able to have a recursive inline function in C++?

36. What is ‘this’ pointer?

37. How different is function overloading from operator overloading?

38. Can a C++ program be compiled without the main() function?

39. What is a destructor?

40. Can we overload a destructor?

41. Explain the default constructor?

42. Can we give one default constructor for our class?

43. Differentiate between C++ and Java

44. Explain Static member in C++?

45. Explain the Reference variable?

46. Explain mutable storage class specifier?

47. Do we have String primitive data type in C++?

48. Can access specifiers be used to achieving data hiding in C++?

49. Explain Block scope variable?

50. Tell some functions of the scope resolution operator?


Learn More Interview Questions Here:


C++ Interview Questions and Answers

1. Define C++?

It is a computer programming language that is basically a superset of C which has additional features added in the C language.

2. Name different data types present in C++?

  • Primitive Datatype (basic datatype). Examples: char, float, long, short, double, int, bool, etc.
  • Derived datatype. Example: array, pointer, etc.
  • Enumeration. Example: enum
  • User-defined data types. Example: structure, class, etc.

3. Differentiate between C and C++?

  • C programming language is procedure-oriented and C++ programming language is object-oriented.
  • C language doesn’t support data hiding whereas C++ language hides the data by encapsulation to ensure that data structures and operators
  • C is a subset of C++ and C++ is a superset of C
  • C doesn’t support Function and operator overloading whereas C++ does
  • C doesn’t have Namespace features whereas C++ uses Namespace to avoid name collisions.
  • In C language functions cannot be defined inside structures whereas C++ allows the function to be defined inside structures.

4. Define class and object in C++?

It is a user-defined data type that has data and member functions. Data members manage the data variables and member functions perform operations on these variables.

An object is an instance of a class. As we know that class is a user-defined data type so an object is called a variable of that data type.

A class is defined as-

class A{
Private:
int data;
Public:
void fun(){
}
};

5. Differentiate between Structure and class?

  • Members of the structure are public by default whereas Class members are private.
  • When you derive a structure, default access specifiers are public whereas in Class the default access specifiers are private.

6. Explain operator overloading?

It is a very important element that runs the operations on user-defined data types. By using operator overloading you can change the default meaning to the operators like +, -, *, /, <=, etc.

For example:

By using operator overloading, Adding two complex numbers:

class complex{
Private:
float r, i;
Public:
complex(float r, float i){
this->r=r;
this->i=i;
}
complex(){}
void displaydata(){
cout«”real part = “«r«endl;
cout«”imaginary part = “«i«endl;
}
complex operator+(complex c){
return complex(r+c.r, i+c.i);
}
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
}

7. Explain polymorphism in C++?

Polymorphism defines having many forms and different situations bring different behavior. And this happens when we have many classes that are co-related by inheritance.

Polymorphism types in C++ are:

  • Compile Time Polymorphism
  • Runtime Polymorphism

8. What is a constructor in C++?

It is a member function that automatically executes when an object is made. Constructors and the class member have the same name so the compiler knows that the specific member function is a constructor. Constructors use no return type.

Example:

class A{
Private:
int val;
Public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);

return 0;
}

9. Explain the virtual function

It is a base class member function that redefines in a derived class. When a virtual function is made, C++ controls which function is to be run at the runtime depending on the type of object pointed by the base class pointer.

10. Explain friend class and friend function?

A friend class can acquire protected, private, and public members of different classes in which it is claimed as friends. Same as friend class, friend function can also acquire protected, private, and public members but, friend functions are not member functions. For example -

class A{

private:

int data_a;

public:

A(int x){

data_a=x;

}

friend int fun(A, B);

}

class B{

private:

int data_b;

public:

A(int x){

data_b=x;

}

friend int fun(A, B);

}

int fun(A a, B b){

return a.data_a+b.data_b;

}

int main(){

A a(10);

B b(20);

cout«fun(a,b)«endl;

return 0;

}

11. Explain C++ access specifiers?

C++ access specifiers are:

  • Public: All member functions and data members can be accessed outside the class.
  • Protected: All member functions and data members can be accessed inside the class and to the derived class.
  • Private: All member functions and data members cannot be accessed outside the class.

12. Explain the inline function

In the inline function, the compiler makes a copy of the function code at every point where the function is called for compilation. One of the major advantages is it removes the function calling overhead of a traditional function.

13. Explain a reference in C++?

A reference is another name of an already present variable or like a pointer. Once a reference name is set with a variable, then a variable is accessible by the reference or variable name.

For example-

int x=10;

int &ref=x; //reference variable

If we modify the value of ref it will be replicated in x. Once a reference variable is set it cannot denote to any other variable.

14. Explain abstraction in C++?

It is the procedure of showing the vital details and hiding the details which are need not be shown to the user or hides the irrelevant details to a particular user.

15. Tell whether the deconstructor overloading is possible or not?

No, destructor overloading is not possible. Destructors don’t take arguments, so it left only one way to destroy an object and this is why destructor overloading is not possible.

16. What do you understand by call by reference and call by value?

call by reference: Here, we pass the address of the variable through which we are able to access the actual argument used by a function call. So modification in the parameter changes the passing argument.

call by value: Here, we permit a copy of the parameter that is passed to the functions. For the copied values a new memory is allocated and modifications made to these values do not show the variable in the main function.

17. Explain abstract class and its use?

Whose objects can never be created is called an abstract class. This class becomes a parent for the derived classes. By placing a pure virtual function in the class, we can make it abstract.

18. Explain destructors in C++?

A constructor is itself called when an object is created. Likewise, when an object demolishes a function, the destructor itself gets called. A constructor and destructor have the same name but are preceded by a tilde.

Example:

class A{

private:

int val;

public:

A(int x){

val=x;

}

A(){

}

~A(){ //destructor

}

}

int main(){

A a(3);

return 0;

}

19. Explain static members and their functions?

When a class variable is stated static, then a specific space for it is assigned for the lifetime of the program. There is only one copy of the static member irrelevant of how many objects of that class have been made. So, all the objects of that class can access the static member.

If there is no object in the class then also a static member function can be called and accessed by only using the class name and the scope resolution operator::

20. What is an inheritance?

It is the procedure of generating new classes, known as derived classes, from existing classes. They are also called base classes. These new classes not only inherit all the abilities of the base class but also add new features and alterations of their own.

21. Explain a copy constructor?

If a member function initializes an object by using different objects of the same class then it is called a copy constructor

Example-

class A{

int x,y;

A(int x, int y){

this->x=x;

this->y=y;

}

};

int main(){

A a1(2,3);

A a2=a1; //default copy constructor is called

return 0;

}

22. Differentiate shallow copy and deep copy?

  • Shallow copy is faster than deep copy
  • Shallow copy saves the references of objects to the original memory address whereas deep copy creates a new and separate copy of the object and allocates a unique memory address to it.
  • Shallow copy shows changes done to the new/copied object in the original object whereas deep copy doesn’t show the changes there.

23. Explain virtual functions and pure virtual functions?

Virtual function: It is a base class member function that redefines in a derived class and is declared by using the virtual keyword.

Example-

class base{

public:

virtual void fun(){

}

};

Pure virtual function: A function that has no implementation and is declared by assigning 0 makes a pure virtual function, also it has nobody.

Example-

class base{

public:

virtual void fun()=0;

};

24. Tell the function of the keyword "Auto"?

By default The keyword “Auto” is used for local variables which helps to make the function work automatically.

25. Can a virtual function be called from a constructor?

Yes, we are able to call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor.

For example-

class base{

private:

int value;

public:

base(int x){

value=x;

}

virtual void fun(){

}

}

class derived{

private:

int a;

public:

derived(int x, int y):base(x){

base *b;

b=this;

b->fun(); //calls derived::fun()

}

void fun(){

cout«”fun inside derived class”«endl;

}

}

26. Explain void pointers?

It is a pointer that has no data type. It can hold addresses of any type.

For example-

void *ptr;

char *str;

p=str; // no error

str=p; // error because of type mismatch

As we know that we can allocate a pointer of any type to a void pointer but the inverse is not true unless you write it as

str=(char*) ptr;

27. Explain pointer in C++?

Every object member functions have a pointer that indicates to the object itself. The value is set to the address of the object for which it is called. It can access data in the object it points to.

Example

class A{

private:

int value;

public:

void setvalue(int x){

this->value=x;

}

};

int main(){

A a;

a.setvalue(5);

return 0;

}

28. Show the code of allocating and deallocating memory in C++?

A new operator allocates the memory and deletes the operator deallocates the memory in C++.

For example-

int value=new int; //allocates memory for storing 1 integer

delete value; // deallocates memory taken by value

int *arr=new int[10]; //allocates memory for storing 10 int

delete []arr; // deallocates memory occupied by arr

29. Explain a token in C++ with examples

A token is a name provided to the different functions in C++ programs. Examples of tokens can be a keyword, string, symbol, identifier, literal, constant, etc. The token code in C++ other than C, is displayed as.

asm bool catch class

const_cast delete dynamic_cast explicit

export false friend inline

mutable namespace new operator

private protected public reinterpret_cast

static_cast template this throw

true try typeid typename

using virtual wchar_t

30. Can we call C++ OOPS?

Yes, C++ can be called OOPS. OOPS stands for Object-Oriented Programming System, which means a paradigm that gives an application of different concepts like, inheritance, data binding, polymorphism, and several others.

31. Explain Namespace?

It is used for solving the name clash of the identifier, which is achieved by placing them under different namespaces. This method helps in the logical partition of the diverse codes.

32. Explain a class template?

It is a name specified to the generic class. So, the keyword template is used to define a class template.

33. Explain the function of the keyword “Volatile”?

“Volatile” is a function that declares that a specific variable is volatile and thereby guides the compiler to modify the variable externally. By this method, we can avoid the compiler optimization on the variable reference.

34. Explain and name some storage classes in C++?

Storage class resembles life or the scope of symbols that also includes functions, variables, etc. Some of the names of storage classes in C++ are mutable, static, auto, register, extern, etc.

35. Are we able to have a recursive inline function in C++?

Even we can call an inline function from within itself the compiler will not able to create the inline code. As compiler won’t regulate the depth of the recursion at the time of compilation. However, a good optimizer compiler can inline recursive calls up to some depth is fixed at compilation, and if actual depth exceeds it insert non-recursive calls at compile time.

36. What is ‘this’ pointer?

The ‘this’ pointer is constant, and it contains the current object memory address. To all the non-static member function calls it authorizes as a hidden argument and it is also presented as a local variable in the body.

37. How different is function overloading from operator overloading?

Function overloading permits more than one function with dissimilar types and many parameters to take the same name. Conversely, operator overloading redefines the way an operator works for user-defined types.

38. Can a C++ program be compiled without the main() function?

Yes, it is possible to compile a C++ program without the main() function. Though the main() function is vital for the program execution, the program will halt after compiling and will not perform.

39. What is a destructor?

It is the member function of the class. It contains a similar name as the class name and is also pre-added with a tilde symbol. It can be performed automatically whenever an object fails its scope.

40. Can we overload a destructor?

No, we are not able to overload a destructor, and it is the only one without the parameters.

41. Explain the default constructor?

The compiler arranges a constructor for every class if a provider is not able to offer the same. This is when the developer gives no particular parameters to the constructor then it is called a default constructor. It is displayed as.

// Cpp program to illustrate the

// concept of Constructors

#include

using namespace std;

class construct {

public:

int a, b;

// Default Constructor

construct()

{

a = 10;

b = 20;

}

};

int main()

{

// Default constructor called automatically

// when the object is created

construct c;

cout « “a: " « c.a « endl

« “b: " « c.b;

return 1;

}

42. Can we give one default constructor for our class?

No, we are not able to give one default constructor for our class. When a variable is set to null in the class type, it shows it was never prepared and the result will be zero.

43. Differentiate between C++ and Java

  • C++ has destructors, and Java uses automatic garbage collection.
  • C++ supports multiple inheritance, pointers, operator overloading, structures, unions, and templates. Java doesn’t have these.
  • Java has a Thread class and also creates a new thread. C++ doesn’t have inbuilt support for threads
  • C++ has a goto statement to jump from a position to some labeled statement within a function. Java doesn’t have any goto statement.

44. Explain Static member in C++?

A static member is assigned storage in a static storage area at one time during program life. Some essential facts related to the static members are:

  • The static member function can’t be virtual
  • They don’t have ‘this’ pointer
  • The static member function doesn’t have const, const volatile, and volatile declaration.

45. Explain the Reference variable?

It is the name provided to the present variables. The variable name and reference variable point share a similar memory through which it can update the original variable. The code can give you an example.

#include

using namespace std;

int main()

{

int x = 10;

// ref is a reference to x.

int& ref = x;

// Value of x is now changed to 20

ref = 20;

cout « “x = " « x « endl ;

// Value of x is now changed to 30

x = 30;

cout « “ref = " « ref « endl ;

return 0;

}

46. Explain mutable storage class specifier?

It is functional only on the class’s non-static and non-constant member variables. It is used for changing the constant class object’s member by declaring it. This can be done by using a storage class specifier.

47. Do we have String primitive data type in C++?

No, we cannot instead of it, we can use a class from Standard Template Library (STL).

48. Can access specifiers be used to achieving data hiding in C++?

Yes, access specifiers are used to achieve data hiding in C++ which includes Private and Protected specifiers.

49. Explain Block scope variable?

This variable is mentioned as a block and can be declared anywhere within the block using C++.

50. Tell some functions of the scope resolution operator?

  • It resolves the scope of various global variables.
  • It associates the function with the class when it is defined outside the class.

The code of the scope resolution operator are as follows.

#include

using namespace std;

int my_var = 0;

int main(void) {

int my_var = 0;

::my_var = 1; // set global my_var to 1

my_var = 2; // set local my_var to 2

cout « ::my_var « “, " « my_var;

return 0;

}


It is one of the popular languages that have high demand and vacancies. To get the job you really need to be well-prepared for the interview and the above given C++ interview questions will surely help you.

Want to prepare for these languages:

Recent Articles