Learn Programming through LIVE online classrooms

C++ and Interview Questions

C++ Interview Questions (Frequently Asked)

Posted in C++, Interview Questions
C++ Interview Questions (Frequently Asked)

C++ is still as relevant today as it was during its advent in the mid-80s. The imperative, object-oriented programming language is widely used as a general-purpose programming language. As such, several jobs require candidates to have a profound understanding of C++. If you wish to sharpen your C++ Language skills further, here are some of the best C++ tutorials that can help you out.

C++ Interview Questions

If you’re preparing for a job role with emphasis on C++, then here are 20 most important C++ interview questions to self-assess your C++ interview preparation:

Question: Briefly explain the concept of Inheritance in C++.
Answer: C++ allows classes to inherit some of the commonly used state and behavior from other classes. This process is known as inheritance.

Question: Can we have a recursive inline function in C++?
Answer: Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won’t be able to determine the depth of the recursion at the compile time.

Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth fixed at compile time, and insert non-recursive calls at compile time for the cases when the actual depth exceed at run time.

Question: Define an Inline Function in C++? Write its syntax. Is it possible for the C++ compiler to ignore inlining?
Answer: In order to reduce the function call overhead, C++ offers inline functions. As the name suggests, an inline function is one that is expanded in line when it is called.

As soon as the inline function is called, the whole code of the same gets either inserted or substituted at the particular point of the inline function call. The substitution is complete by the C++ compiler at compile time. Small inline functions might increase program efficiency.

The syntax of a typical inline function is:

Inline return-type function-name(parameters)
// Function code goes here

As the inlining is a request, not a command, the compiler can ignore it.

Question: Explain ‘this’ pointer?
Answer: The ‘this’ pointer is a constant pointer and it holds the memory address of the current object. It passes as a hidden argument to all the nonstatic member function calls. Also, it is available as a local variable within the body of all the nonstatic functions.

As static member functions can be called even without any object, i.e. with the class name, the ‘this’ pointer is not available for them.

Question: Why do we need the Friend class and function?
Answer: Sometimes, there is a need for allowing a particular class to access private or protected members of a class. The solution is a friend class, which is capable of accessing the protected as well as the private members of the class in which it is declared as a friend.

Similarly to the friend class, a friend function is able to access private and protected class members. A friend function can either be a global function or a method of some class.

Interested in C++? Enquire for the best course suited for you.

Some important points about friend class and friend function:

  • Friendship is not inherited
  • Friendship isn’t mutual i.e. if some class called Friend is a friend of some other class called NotAFriend then it doesn’t automatically become a friend of the Friend class
  • The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming

Question: Explain the significance of vTable and vptr in C++ and how the compiler deals with them
Answer: vTable is a table containing function pointers. Every class has a vTable. vptr is a pointer to vTable. Each object has a vptr. In order to maintain and use vptr and vTable, the C++ compiler adds additional code at two places:

  1. In every constructor – This code sets vptr:
    1. Of the object being created
    2. To point to vTable of the class
  2. Code with the polymorphic functional call – At every location where a polymorphic call is made, the compiler inserts code in order to first look for vptr using the base class pointer or reference. The vTable of a derived class can be accessed once the vptr is successfully fetched. Address of derived class function show() is accessed and called using the vTable.

Question: How is function overloading different from operator overloading?
Answer: Function overloading allows two or more functions with different type and number of parameters to have the same name. Operator overloading, on the other hand, allows for redefining the way an operator works for user-defined types.

Question: Is it possible for a C++ program to be compiled without the main() function?
Answer: Yes, it is possible. However, as the main() function is essential for the execution of the program, the program will stop after compiling and will not execute.

Question: Draw a comparison between C++ and Java

  • C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection
  • C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn’t have any of them
  • Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads
  • In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java
  • C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent.

Question: Take a look at the following C++ program:

using namespace std;
int main() 
 int numbers[5], sum = 0;
 cout << "Enter 5 numbers: ";
 for (int i = 0; i < 5; ++i) 
 cin >> numbers[i];
 sum += numbers[i];
 cout << "Sum = " << sum << endl; 
 return 0;

Question: What will be the output?
Answer: The program will ask the user to enter 5 numbers and will then present with their sum as the output. For instance,

Enter 5 numbers: 22

Sum = 191

Question: What are the most important differences between C and C++?

  • C++ supports references while C doesn’t
  • Features like friend functions, function overloading, inheritance, templates, and virtual functions are inherent to C++. These are not available in C programming language
  • In C, exception handling is taken care of in the traditional if-else style. On the other hand, C++ offers support for exception handling at the language level
  • Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is the standard input stream while cout serves as the standard output stream
  • While C is a procedural programming language, C++ provides support for both procedural and object-oriented programming approaches

Question: Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code example.
Answer: Any function when accompanying the virtual keyword exhibits the behavior of a virtual function. Unlike normal functions that are called in accordance with the type of pointer or reference used, virtual functions are called as per the type of the object pointed or referred.

In simple terms, virtual functions resolve at runtime, not anytime sooner. Use of virtual functions could also be understood as writing a C++ program leveraging the concept of runtime polymorphism. Things essential to writing a virtual function in C++ are:

  • A base class
  • A derived class
  • A function with the same name in both the classes i.e. the base class and the derived class
  • A pointer or reference of base class type that points or refers, respectively to an object of the derived class

An example demonstrating the use of virtual functions (or runtime polymorphism at play) is:

 using namespace std;
 class Base { 
 virtual void show() { cout<<" In Base \n"; } 
 class Derived: public Base { 
 void show() { cout<<"In Derived \n"; } 
 int main(void) { 
 Base *bp = new Derived; 
 bp->show(); // <- Runtime Polymorphism in Action
 return 0;

In the aforementioned program bp is a pointer of type Base. A call to bp->show() calls show() function of the Derived class. This is because bp points to an object of the Derived class.

Question: What differences separate structure from a class in C++?
Answer: There are two important distinctions between a class and a structure in C++. These are:

  1. When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. On the contrary, default access specifier is private when deriving a class.
  2. While the members of a structure are public by default, the members of a class are private by default

Question: What does a Static member in C++ mean?
Answer: Denoted by the static keyword, a static member is allocated storage, in the static storage area, only once during the program lifetime. Some important facts pertaining to the static members are:

  • Any static member function can’t be virtual
  • Static member functions don’t have ‘this’ pointer
  • The const, const volatile, and volatile declaration aren’t available for static member functions

Question: Define access specifier and its various types in C++
Answer: An access specifier offers the means by which it is possible to define how the class members, i.e. functions and variables, will be accessed outside the scope of the class. There are three types of access specifier in C++:

  • Private – Such class members can’t be accessed outside the class in which they are declared and are only accessible within the same class. Even child classes are disabled to access private members of its parent class
  • Protected – In addition to the class in which they are declared, the child classes can access the protected members of its parent class
  • Public – Class members declared as public can be accessed throughout the program (code)

Question: Define the Copy Constructor used in C++ along with its general function prototype. Also, explain the various scenarios in which it is called.
Answer: A member function that initializes an object using another object of the same class is known as a copy constructor in C++. The Copy Constructor can also be made private. A call to the Copy Constructor can happen in any of the following 4 scenarios, when:

  1. The compiler generates a temporary object
  2. An object is constructed or based on some another object of the same class
  3. An object of the class is returned by value
  4. An object of the class is passed (i.e. to a function) by value as an argument

The general function prototype for the Copy Constructor is:

ClassName (const ClassName &old_obj);
Point(int x1, int y1) { x=x1; y=y1;}
Point(const Point &p2) { x=p2.x; y=p2.y; }

Question: Observe the following code snippet:

int i = 5;
int j = i++;

After execution, what will be the value of i and j? Explain your answer.
Answer: Post the execution of the code above, i and j will be 6 and 5, respectively. For understanding the output, it’s important to understand how the unary ‘++’ operator and the decrement ‘--’ operator works in C++.

When any of these operators precede a variable, the value of the variable is first modified and then this modified value is used. However, when any of the two operators follow a variable, the value is first used and then it is modified.

Therefore, in the code above j is set to the unmodified value of 5 and then i is incremented to store 6.

Question: Take a look at the following two code examples for printing a vector:

Sample Code 1:

vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); itr++) {

Sample Code 2:

vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {

Is there any advantage of using one over the other?
Answer: Though both codes will generate the same output, sample code 2 is a more performant option. This is due to the fact that the post-increment ‘itr++’ operator is more expensive than the pre-increment ‘++itr’ operator.

The post-increment operator generates a copy of the element before proceeding with incrementing the element and returning the copy. Moreover, most compilers will automatically optimize the sample code 1 by converting it implicitly into the sample code 2.

Question: Suppose you have the GPA (Grade Point Average) of n number of students and you need to store and display it using C++. Can you write a program that accomplishes this?


using namespace std;
int main()
 int num;
 cout << "Enter the total number of students: ";
 cin >> num;
 float* ptr;
 ptr = new float[num];
 cout << "Enter the GPA of students." << endl;
 for (int i = 0; i < num; ++i)
 cout << "Student" << i + 1 << ": ";
 cin >> *(ptr + i);
 cout << "\nDisplaying GPA of students." << endl;
 for (int i = 0; i < num; ++i) {
 cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
 delete [] ptr;
return 0;


Question: What is the ‘diamond problem’ that occurs with multiple inheritance in C++? Explain using an example.
Answer: The diamond problem in C++ represents the inability of the programming language to support hybrid inheritance using multiple and hierarchical inheritance.

Suppose we have a university with some faculty members and some graduate students. A simple inheritance scheme in this scenario might have different types of people in different roles. However, all of them inherit from the same Person class.

The Person class defines an abstract getRole() method that would then be overridden by its subclasses in order to return the correct role type. Things up till this point is simple, however, if we wish to model the role of a TA or Teaching Assistant then things get weird.

A Teaching Assistant is both a student and a faculty member. This will yield the diamond problem, as illustrated in the figure below:

The problem generates an inheritance diagram resembling a diamond, hence the name, diamond problem.

Which getRole() implementation should the Teaching Assistant inherit? Graduate Student or the Faculty Member? A potential answer might be to have the Teaching Assistant class override the getRole() method and return a newly-defined role, say TA.

However, such an answer would also be far from complete as it will hide the fact that a Teaching Assistant is someone who is both a faculty member as well as a graduate student.

Some Helpful Advice!

Tutorials are an excellent means of learning. Moreover, you might get access to additional resources. Don’t miss out on them. Here are some important C++ tutorials to strengthen your C++ knowledge.

There is much more required than having adequate knowledge of all the concepts that are likely to be asked during a job interview. The demeanor and mannerism is something that is also paid attention to by the interviewer. So, be clear and precise, don’t wander off the topic.

Also, you need not beat around the bush. If you don’t know the answer, it’s ok. Simply accept it, rather than ranting meaninglessly. Remember, it’s not only what you say, but also how you say it!

All the very best!

People are also Reading:

Akhil Bhadwal

Akhil Bhadwal

A Computer Science graduate interested in mixing up imagination and knowledge into enticing words. Been in the big bad world of content writing since 2014. In his free time, Akhil likes to play cards, do guitar jam, and write weird fiction. View all posts by the Author

Leave a comment

Your email will not be published