C++ Interview Questions with Answers

The C++ programming language contains vast concepts and useful topics. There are so many basic to advanced level questions from the C++ programming language. In this tutorial, we discussed some basic to advanced level questions which are frequently asked in placements and interviews. These questions will help you to improve your understanding of the C++ programming language.

Following are some of the C++ Interview questions with their answers. This will help you better in preparing for placements and interviews.

C++ Basic Interview Questions

Q.1 Can you tell the difference between reference and pointers?

Ans. References and pointers both are being used to change the local variables of a function which is inside another function.

Pointers
Used to store the address of a variable.
Syntax

Data_type *pointer;

References
When you declare a parameter as reference then it refers to an existing variable in another name.
Syntax

Data_Type &newname=existing name;
Reference Pointer
References are less powerful. Pointers are more powerful.
References are easier to use.  Pointers are not that easy to use.
It is used to refer to an existing variable in another name. It is used to store the address of a variable.
References cannot have a null value. Pointers can have a null value.
You have to initialize it on declaration. But in the case of a pointer, you do not have to do it.

Q.2 What is the use of ‘this’ pointer?

Ans. The ‘this’ is a keyword. It is mainly used to refer to the current instance of the class. It can be used inside a member function to refer to the invoking objects. Only member functions have this pointer. But friend functions do not have this pointer.
There are some main usages of this pointer in C++:-

  • You can use this pointer to pass the current object as a parameter to another object.
  • It is also used to refer to the current class instance variable.
  • The ‘this’ pointer is an implicit parameter to all member functions.
  • It also helps you to reduce the complexity of the code and also provides better readability.
  • It also helps you to distinguish data members from local variables of member functions if they have the same name.

Q.3 In C++, why should you use the friend class and function?

Ans. In C++, the Friend function is one of the most useful and important functions that you should watch out for. It helps you to access the protected and private members of the class in which it is declared as a friend. Also, using the friend function, you will be able to access the private and protected class members.
Friend Function is mainly of 2 types:-

  • A global function:- It will allow you to access all the private and protected members of the class.
  • Method of another class:- If you have more than one class and you want to access the non-public data members of a particular class then you can use the Friend function.

Q.4 What is the main difference between function overloading and operator overloading?

Ans. With the help of operator overloading, you can redefine the way how an operator works for user-defined types. For example, you can overload an operator ‘+’ in a class-like string to concatenate two strings with the help of ‘+’.

In function overloading, the compiler will identify the appropriate function by examining the number of the types of parameters or arguments in the overloaded function. It also helps you in reducing the investment of different function names. With this, you can also perform similar functionality by more than one function.

You do not have to change the fundamental language, it provides a pleasant appearance without it. Function calls that are equivalent are being provided syntactic sugar by all overloaded operators. Function overloading allows two or more functions with different types and the number of parameters have to be the same. You can redefine a function by using different types of arguments or by using a different number of arguments.

Q.5 Can you compile a C++ program without the main() function in it?

Ans. Yes, you can compile a C++ program without the main() function. But the program will not execute after compiling.

In a program code, main is known as the entry point. Instead of the main function, you can rename the main function as something else by recompiling the program code of the startup file. You can change the name of main to something if you make another language or hack the sources of these language compilers. You will face several dysfunctionalities because it will violate the C++ standards.

Q.6 What will be the output of the following program?

#include <iostream>
using namespace std;
int main()
{
 int num[4]={4,5,6,8}, sum = 0;
 for (int i = 0; i < 4; ++i)
 {
 sum += num[i];
 }
 cout << "Sum = " << sum << endl;
 return 0;
}

Ans.
Output:-
23

Q.7 Why do you use static members in C++?

Ans. A static member is allocated storage. It is mainly used for recording data common to all objects of a class. You can use a static member as a counter to store the number of objects of a particular class type which is created. You can call a static member function even if no objects of the class exist. Also, you can only access the static members using the class name and the scope resolution operator.

Q.8 Can you use access specifiers to perform data hiding in C++?

Ans. Yes, you can use access specifiers to perform data hiding. These include private and protected. It is mainly used to perform data hiding in object oriented programming. There are three types of access specifiers such as private, public and protected. Data hiding maintains object integrity.

Q.9 What is the output of the following program?

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
  int num=10,num1=2,out;
  
  out = num*pow(num,num1);
  out = out % 2;
  out += 1;
  cout<<out;
  return 0;
}

Ans.
Output:-
1

Q.10 What is the output of the following program?

#include<iostream>
using namespace std;
int main(){
   int n = 128,a=0;
   int res = n/a;
   cout<<res;
   return 0;
}

Ans. This program will give you an error. Because you are dividing a number by 0. It will throw you an exception.

Q.11 Why do you use namespace in C++?

Ans. It is used to organize codes into logical groups and to prevent name collisions that can happen when your code includes multiple libraries. At namespace scope, all identifiers are visible to one another without qualification.
It is a declarative space which provides a scope to the identifiers inside it.

It works the same as the namespace. Usage of namespace is considered as a bad practice. The problem with putting ‘using namespace’ in the header files of your classes is that it forces anyone who wants to use your classes to also be using those other namespaces.

Intermediate C++ Programming Interview Questions Answers

Q.12 State the difference between delete[] and delete in C++?
Ans.

delete[] delete
This is used to release an array.  delete is used to release a unit of memory.
It deallocates memory and calls destructors for an array of objects created with new[]. It deallocates memory and calls the destructor for a single object created with new.
You should always use delete to destroy the objects that are created with new. You should always use delete[] to destroy the arrays that are created with new[].
It is used for deleting an array through a pointer. It is used for one single pointer.

Q.13 Write a program to check for equality without using arithmetic and comparison operators?
Ans.

#include<iostream>
using namespace std;
int main(){
   int a = 16;
   int b = 13;
   if ( (a ^ b) )
  	cout<<"a is not equal to b";
   else
  	cout<<"a is equal to b";
  	return 0;
}

Output:-
a is not equal to b

Q.14 What is the usage of scope resolution operator in C++?

Ans. It is denoted by ‘::’. It is used to define the member function outside the class. This is a very important one when it comes to C++. This is also used to define a function outside of the class and used to access the static variables of the class.

You have to be careful while using this operator. It will always call the global variable if the global variable has the same name as the local variable.

Q.15 In C++, can you define a String Primitive data type?

Ans. No, you cannot have a string primitive data type in C++. But you can have a class from the Standard Template Library.

Primitive data types are those data types which are used by users when creating variables in their program.

Example:-
boolean
char
byte
long
int
float
short

Non Primitive data types:
String
array
class
enum
etc.

Q.16 What is the use of the Auto keyword in C++?

Ans. The keyword ‘Auto’ is used by default for various functions to make functions work automatically.
In case of any functions, if the return type is auto then it will be evaluated by return type expression at runtime.
It is a simple way to declare a variable that has a complicated type.

Example:-

auto a=10;

Q.17 When you use multiple inheritance, what is the problem you face?

Ans. When you inherit more than one class in the same derived class and all these base classes also inherit the same single class, multiple references of the super parent class become available to the derived class.
That’s why it becomes unclear to the derived class, which version of the super parent class it should refer to.

Q.18 Can you overload a destructor?

Ans. With the help of destructors, you can easily deallocate memory. And it is also used to do other cleanup for a class object. It is called for a class object and when that object is explicitly deleted. It is a member function which is invoked when the object goes out of scope or if it is deleted by a call to delete.

No, a destructor cannot be overloaded. It has the only form without the parameters. There should be only one empty destructor per class. And it must have a void parameter list.

Destructor does not take any parameters and it also does not return anything. That’s why there is the possibility of multiple destructors with different signatures in a class.

Example:- Overloading a destructor

class Tech{
public:
  Tech(){
    cout<<"constructor...";
  }
  //Destructor
  ~Tech(){
    cout<<"Destructor...";
  }

  ~Tech(int a){
    cout<<"Destructor overloaded...";
  }
};

int main(){
  Tech obj;
  return 0;
}

The compiler will give you an error if we try to overload the destructor for the class Tech.

Output:-
main.cpp:13:5: error: ‘Tech::~Tech()’ cannot be overloaded
~Tech(int a){

Q.19 State the difference between C and C++?

Ans.

C C++
It does not support data hiding. This programming language supports data hiding.
Function and operator overloading is not supported in C. C++ supports function and operator overloading.
C is a function driven language.  This is an object driven language.
C does not support inheritance. C++ supports inheritance.
There is no direct support for exception handling in C.  This supports Exception Handling.
C contains 32 keywords. C++ contains 52 keywords.
It does not reference variables. C++ supports the reference variables.

Q.20 What is the output of the following program?

#include <iostream>  
using namespace std;  
int main()  
{  
int n=365, reverse=0, rem;    
while(n!=0)    
{    
rem=n%10; 	 
reverse=reverse*10+rem;    
n/=10;    
}
cout<<reverse<<endl;	 
return 0;  
} 

Ans. 563

Q.21 What is the output of the following program?

#include <iostream>
using namespace std;
int main() {
    int  *ip = NULL;
    cout << ip;
    return 0;
}

Ans. 0

Q.22 What will be the output of the following program?

#include <iostream>
using namespace std;
int main() {
  int a[] = {2,3,6,9};
  cout << (1 + 1)[a] + (a + 2)[1];
}

Ans. 15.

Explanation:- (1+1)[a] is equal to a[2]. And a[2] = 6.
And (a+2)[1] is equal to a[3]. And a[3] = 9.

C++ Language Advance Interview Questions

Q.23 What is the output of the following program?

#include<iostream>
using namespace std;
int main()
{
  int dec=6, bin[10], i=0;
  while(dec!=0)
  {
    	bin[i] = dec%2;
    	i++;
    	dec = dec/2;
  }
  for(i=(i-1); i>=0; i--)
    	cout<<bin[i];
  return 0;
}

Ans. 110

Q.24 Spot the error in the following code.

Tech *ptr = 0;
delete ptr;

Ans. In the above example, the pointer is a null pointer. It is valid to call delete on a NULL pointer. So, there is nothing wrong in the above program.

Q.25 Can you tell me the names of the operators which cannot be overloaded?
Ans.

  • sizeof:- sizeof operator
  • . – Dot operator
  • .*:- dereferencing operator
  • ->:- member dereferencing operator
  • ::- scope resolution operator
  • ?: – conditional operator

Q.26 Does a derived class inherit or doesn’t inherit?

Ans. The derived class basically inherits all the features and ordinary members of the base class. But a derived class does not inherit the base class’s constructors and destructors.

The derived class also does not inherit the assignment operator of the base class and friends of the class.

For example, suppose the private stuff is:
int i;
and the class has a geti() and seti(). The value of i has to be put somewhere, even if it is private.

Q.27 Does C++ support Exception handling?

Ans. Yes, C++ does support Exception handling.

Sometimes when you execute a program, you may face exceptions like run-time abnormal conditions. It arises due to some abnormal conditions such as dividing a number by zero. It arises while a program is running.
In C++, you can perform exception handling with the help of three keywords such as try, catch and throw.

  • try:- Mainly used to represent a block of code which might throw an exception.
  • catch:- block of code inside this keyword will get executed when an exception is thrown.
  • throw:- Mainly used to throw an exception.

When an exception occurs, the compiler will throw it. When an exception is thrown, the compiler has to ensure that it is handled properly. C++ uses three keywords to perform exception handling such as try, catch and throw.

Q.28 State the difference between the internal and external iterator?

Ans. Iterator is an object and it points to an element inside the container. With the help of iterators, you can move through the contents of that container.

An iterator which is implemented by the member functions of a class and has the iteration logic is known as internal iterator.

An iterator which is implemented by a separate class and can be attached to the object is known as an external iterator. An external iterator is easy to implement.

A big advantage of an external iterator is that you can make many iterators that can be active simultaneously on the same object.

Q.29 What is the output of the following program?

main()
 {
extern int i;
 cout<<i<<endl;
 }
int i=20;

Ans. 20

Q.30 Can you get the source code of a C++ program back from the binary file?

Ans. Yes, you can generate the source code from the binary file. It is called reverse engineering. There are many reverse engineering tools available such as ghidra, ida, cutter etc.

You can use a decompiler but don’t expect it to produce compileable or readable code. If you lost your code and all you have is a binary, you’re in for a real hassle, sorry to say.

Your best option is a decompiler. Several options, take a look here for a starting point :

You will recover C code to some extent, but there is no way you will get any objects or proper source out of this. You will have to extensively edit the resulting output to get anything useful.

Summary

In this tutorial, we discussed some basic to advanced level interview questions. These questions will help you to prepare better for placements and interviews. Also it will help you to improve your basic understanding of the C++ programming language.