Interview Questions.

C++ Interview Questions (Frequently Asked)


C++ Interview Questions (Frequently Asked)

C++ continues to be as applicable these days as it changed into all through its creation in the mid-80s. The vital, object-orientated programming language is widely used as a general-purpose programming language. As such, numerous jobs require applicants to have a profound information of C++. If you want to sharpen your C++ Language capabilities in addition, right here are some of the first-rate C++ tutorials that assist you to out.

Best C++ Interview Questions and Answers

If you’re getting ready for a activity role with emphasis on C++, then here are the 20 maximum important C++ interview inquiries to self-verify your C++ interview education:

Basic Level Interview Questions

Question: Define C++?

Answer: C++ is a pc programming language that is a superset of C wherein extra functions are made inside the C language. 

Question: Can we name C++ OOPS? And Why?

Answer: Yes, C++ may be referred to as OOPS. The full form of OOPS is an Object-Oriented Programming System, which means that a paradigm that gives an utility of numerous principles, such as information binding, polymorphism, inheritance, and numerous others.

Question: Define Class in C++?

Answer: Class is known as the designing of the user-described statistics type. It reflects the distinctive entities, attributes, and actions.

Question: Define Object in C++?

Answer: Object is an instance of the elegance. An item could have fields, strategies, constructors, and associated. For example, a motorcycle in actual life is an item, however it has diverse capabilities along with brakes, color, size, design, and others, which are times of its class.

Question: Define Encapsulation in C++?

Answer: Encapsulation is the manner of binding together the data and capabilities in a class. It is implemented to prevent direct access to the facts for protection reasons. The capabilities of a class are carried out for this motive. For example, the customers' internet banking facility permits only the authorized person with the specified login id and password to get get admission to. That is just too most effective for his/her part of the statistics within the financial institution records source.

Question: What is an abstraction in C++?

Answer: An abstraction in C++ is hiding the internal implementations and displaying handiest the specified details. For instance, while you ship an vital message via e-mail, at that time, simplest writing and clicking the ship choice is used. This outcome is just the achievement message this is displayed to verify that your email has been despatched. However, the procedure accompanied in moving the information via e-mail isn't always displayed because it is of little need to you.

Question: Briefly provide an explanation for the idea of Inheritance in C++.

Answer: C++ allows instructions to inherit a number of the commonly used nation and behavior from different lessons. This technique is referred to as inheritance.

Question: Define get right of entry to specifier and its diverse kinds in C++

Answer: An get admission to specifier gives how it is possible to outline how the magnificence contributors, i.E., features and variables, will be accessed out of doors the elegance's scope. There are 3 styles of get right of entry to specifier in C++:

Private – Such magnificence contributors can’t be accessed out of doors the elegance wherein they may be declared and are handiest reachable in the identical magnificence. Even toddler training are disabled to get admission to private members of its figure class.

Protected – In addition to the elegance in which they are declared, the child lessons can access its parent elegance's included members.

Public – Class members declared as public may be accessed throughout the program (code)

Question: Define a namespace?

Answer:  A namespace is used for resolving the name war of the identifier, that's done by putting them under numerous namespaces. This way, it enables in the logical department of the one of a kind codes.

Question: Define a category template?

Answer: A class template is a name given to the normal class. The use of the key-word template is made for defining a category template.

Question: What is the function of the key-word “Volatile”?

Answer: "Volatile" is a feature that helps in maintaining that the unique variable is volatile and thereby directs the compiler to exchange the variable externally- this way, the compiler optimization on the variable reference may be avoided.

Question: Define garage elegance in C++? Name some?

Answer: Storage elegance in C++ especially resemble existence or maybe the scope of symbols, inclusive of the variables, features, etc. Some of the storage class names in C++ include mutable, car, static, extern, sign in, etc.

Question: Can we've a recursive inline function in C++?

Answer: Even though it's miles feasible to call an inline characteristic from within itself in C++, the compiler might not generate the inline code. This is so due to the fact the compiler received’t decide the intensity of the recursion on the bring together time.

Nonetheless, a compiler with a very good optimizer is able to inline recursive calls till some depth is fixed at bring together-time and insert non-recursive calls at collect time for the instances whilst the real depth exceeds 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 lessen the function call overhead, C++ gives inline features. As the name suggests, an inline characteristic is extended in line while it's miles known as.

As soon because the inline function is called, the complete code of the equal gets both inserted or substituted on the specific point of the inline feature name. The substitution is whole via the C++ compiler at bring together time. Small inline functions would possibly boom program efficiency.

The syntax of an ordinary 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 forget about it.

Question: Explain ‘this’ pointer?

Answer: The ‘this’ pointer is a consistent pointer, and it holds the memory cope with of the modern object. It passes as a hidden argument to all of the nonstatic member feature calls. Also, it's miles to be had as a local variable within the frame of all of the nonstatic functions.

As static member functions can be referred to as even with none item, i.E., with the magnificence name, the ‘this’ pointer isn't available for them.

Question: What are the maximum crucial differences among C and C++?


C++ helps references whilst C doesn’t

Features like pal features, feature overloading, inheritance, templates, and digital features are inherent to C++. These are not available within the C programming language.

In C, exception handling is taken care of inside the traditional if-else style. On the other hand, C++ gives 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 usual enter movement at the same time as cout serves as the same old output circulate.

While C is a procedural programming language, C++ offers assist for both procedural and item-orientated programming strategies.

Question: Why can we want the Friend elegance and function?

Answer: Sometimes, there is a need for allowing a particular magnificence to access non-public or covered contributors of a category. The answer is a chum elegance, that could get admission to the blanketed and private members of the class in which it is declared as a pal.

Similar to the pal class, a pal function is capable of access private and protected magnificence contributors. A friend characteristic can either be a global function or a technique of some class.

Some important factors approximately friend elegance and friend function:

Friendship is not inherited.

Friendship isn’t mutual, i.E., if some elegance known as Friend is a chum of a few other class known as NotAFriend, then it doesn’t routinely turn out to be a pal of the Friend magnificence.

The overall range of pal training and pal features must be restricted in a software because the overabundance of the identical would possibly lead to a depreciation of the concept of encapsulation of separate training, that is an inherent and acceptable first-rate of object-oriented programming.

Intermediate Level Interview Questions

Question: Explain the significance of vTable and vptr in C++ and how the compiler offers with them

Answer: vTable is a table containing feature guidelines. Every class has a vTable. Vptr is a pointer to vTable. Each object has a vptr. In order to hold and use vptr and vTable, the C++ compiler provides additional code at  places:

In each constructor – This code units vptr:

Of the item being created

To factor to vTable of the elegance

Code with the polymorphic purposeful call – At every place wherein a polymorphic name is made, the compiler inserts code as a way to first search for vptr using the bottom class pointer or reference. The vTable of a derived class may be accessed as soon as the vptr is successfully fetched. Address of derived elegance feature display() is accessed and known as the usage of the vTable.

Question: How is function overloading one of a kind from operator overloading?

Answer: Function overloading permits  or more capabilities with one-of-a-kind kind and variety of parameters to have the equal name. On the opposite hand, operator overloading permits for redefining the manner an operator works for person-defined types.

Question: Is it feasible for a C++ software to be compiled without the primary() function?

Answer: Yes, it is possible. However, as the principle() feature is critical for the execution of this system, this system will prevent after compiling and will no longer execute.

Question: What is a destructor?

Answer: A destructor is the member function of the elegance. It has the equal name because the magnificence call and also prefixed with a tilde symbol. It may be carried out routinely on every occasion an object loses its scope.

Question: Can we overload a destructor?

Answer: No, a destructor can not be overloaded, and it has the simplest form without the parameters.

Question: What is the default constructor?

Answer: The compiler offers a constructor to every class in case the provider does no longer offer the identical. This is whilst the programmer gives the constructor without a unique parameters than it is referred to as a default constructor. The code for default constructor can be displayed inside the following example.

// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;
class construct {
    int a, b;
    // Default Constructor
        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;

Question: Can we offer one default constructor for our magnificence?

Answer: No, we cannot provide one default constructor for our elegance. When a variable inside the elegance type is set to null, it manner that it become in no way initialized and the results will be 0.

Question: What is the primary distinction between the key-word struct and sophistication?

Answer: The key-word struct is used for corresponding to public individuals with the aid of default, even as the keyword magnificence is used for similar to private contributors by means of default.

Question: Draw a comparison among C++ and Java


C++ has destructors, which can be invoked robotically when an object is destroyed. Java has something referred to as automatic garbage collection

C++ helps a couple of inheritance, operator overloading, suggestions, systems, templates, and unions. Java doesn’t have any of them

Java has a Thread magnificence this is inherited in an effort to create a new thread. C++ has no built in aid for threads

In C++, a goto statement gives a manner to leap from a place to a few categorized assertion in the equal function. There is not any goto announcement in Java

C++ run and assemble using the compiler, which converts the source code into gadget level language. Hence, it is platform-established. Java compiler, then again, converts the supply code into JVM bytecode, that's platform-unbiased.

Question: Take a study the following C++ software:

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;

What may be the output?

Answer: The application will ask the person to go into five numbers and then gift with their sum. For example,

Enter five numbers: 22




sixty six

Sum = 191

Question: Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code instance.

Answer: Any feature while accompanying the virtual key-word well-knownshows the behavior of a virtual characteristic. Unlike ordinary features which can be known as according with the kind of pointer or reference used, virtual capabilities are referred to as as in step with the form of the item pointed or referred.

In easy terms, digital features remedy at runtime, now not whenever faster. Use of virtual functions could also be understood as writing a C++ application leveraging the concept of runtime polymorphism. Things crucial to writing a virtual characteristic in C++ are:

A base magnificence

A derived class

A feature with the equal call in each the training i.E. The bottom elegance and the derived elegance

A pointer or reference of base elegance type that factors or refers, respectively to an item of the derived class

An instance demonstrating the usage of virtual capabilities (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->display() calls show() feature of the Derived class. This is due to the fact bp factors to an object of the Derived magnificence.

Question: What differences separate structure from a category in C++?

Answer: There are two critical distinctions among a class and a structure in C++. These are:

When deriving a shape from a class or a few other shape, the default get admission to specifier for the bottom class or structure is public. On the opposite, default get right of entry to specifier is personal when deriving a class.

While the individuals of a shape are public by way of default, the participants of a category are private by using default

Question: What does a Static member in C++ suggest?

Answer: Denoted with the aid of the static keyword, a static member is allotted storage, within the static garage area, only as soon as all through the program lifetime. Some crucial data bearing on the static members are:

Any static member characteristic can’t be virtual

Static member functions don’t have ‘this’ pointer

The const, const risky, and risky declaration aren’t to be had for static member functions

Question: Define the Reference variable?

Answer: The reference variable in C++ is the name given to the present variables. The variable call and reference variable point share the same reminiscence vicinity in C++, which allows in updating the unique variable the usage of the reference variable. The code can be displayed inside the following example.

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;

 Advanced Level Interview Questions

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++. Copy Constructor can also be made private. A call to the Copy Constructor can happen in any of the following 4 scenarios when:

The compiler generates a temporary object

An object is constructed or based on some another object of the same class

An object of the class is returned by value

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 benefit of the use of one over the alternative?

Answer: Though each codes will generate the equal output, sample code 2 is a greater performant alternative. This is due to the reality that the put up-increment ‘itr++’ operator is more highly-priced than the pre-increment ‘++itr’ operator.

The put up-increment operator generates a replica of the element earlier than proceeding with incrementing the element and returning the copy. Moreover, maximum compilers will mechanically optimize sample code 1 by converting it implicitly into pattern code 2.

Question: Suppose you have got the GPA (Grade Point Average) of n variety of college students, and you need to keep and display it the usage of C++. Can you write a software 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 a mutable storage class specifier? How can they be used?

Answer: A mutable garage class specifier is applied simplest on the magnificence's non-static and non-consistent member variable. It is used for changing the regular class object's member by means of maintaining it. This can be performed by way of the use of a storage class specifier.

Question: What are the variations among a shallow reproduction and a deep copy?

Answer: The differences between a shallow copy and a deep copy can be said as under.

Shallow Copy Deep Copy
It allows memory dumping on a bit by bit basis from one object to another. It allows the copy field, which is done by field from one object to another.
It is achieved by using a copy instructor and an overloading assignment operator. It is used for shallow copy purposes.

Question: Define an Abstract elegance in C++?

Answer: An summary magnificence in C++ is known as the bottom class, which has as a minimum one pure digital characteristic. In any such characteristic, a person cannot instantiate an abstract magnificence. This way, an Abstract class a pure digital feature is defined by the use of a natural specifier which is identical to 0 in the course of the announcement of the virtual member function inside the elegance assertion. The code pattern can be displayed as follows in instance.

// An abstract class
class Test
    // Data members of class
    // Pure Virtual Function
    virtual void show() = 0;
   /* Other members */

Question; Can we've a String primitive information type in C++?

Answer: No, we can't have a String Primitive facts type in C++. Instead, we are able to have a category from the Standard Template Library (STL).

Question: Can we use get entry to specifiers to attain records hiding in C++?

Answer: Yes, we will use get admission to specifiers to gain information hiding in C++. These consist of Private and Protected.

Question: Define Block scope variable?

Answer: A Block scope variable is the one that is certain as a block the use of the C++ that can be declared anywhere within the block.

Question: What are the capabilities of the scope decision operator?

Answer: The features of the scope decision operator consist of the subsequent.

It helps in resolving the scope of diverse global variables.

It facilitates in associating the function with the class while it's far defined outside the magnificence.

The code of the scope resolution operator can be displayed as follows.

#include <iostream>  
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;

Question: What is the feature of the key-word "Auto"?

Answer: The key-word “Auto” is utilized by default for various local variables to make function paintings mechanically. 

Question: Define a token in C++? Give examples?

Answer: A token is a name given to the diverse functions in C++ programs. Examples of tokens include a key-word, symbol, string literal, identifier, consistent, and many others. The code of token in C++ apart from C, may be displayed inside the following example.

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

Question: What is the ‘diamond problem’ that happens with more than one inheritance in C++? Explain the use of an instance.

Answer: The diamond trouble in C++ represents the incapability of the programming language to help hybrid inheritance the usage of a couple of and hierarchical inheritance.

Suppose we have a university with some faculty individuals and some graduate college students. A simple inheritance scheme on this state of affairs might have unique varieties of people in one-of-a-kind roles. However, all of them inherit from the identical Person elegance.

The Person magnificence defines an abstract getRole() method that might then be overridden through its subclasses with a purpose to return the correct role kind. Things up till this factor is simple, but, if we wish to model the position of a TA or Teaching Assistant then things get weird.

A Teaching Assistant is both a pupil and a school member. This will yield the diamond problem, as illustrated within the figure beneath:

The hassle generates an inheritance diagram akin to a diamond, hence the name, diamond hassle.

Which getRole() implementation have to the Teaching Assistant inherit? Graduate Student or the Faculty Member? A capability solution is probably to have the Teaching Assistant magnificence override the getRole() approach and go back a newly-defined position, say TA.

However, such a solution could additionally be a ways from whole as it will disguise the truth that a teaching assistant is each a school member and a graduate student.


This article also covers a few C++ Coding interview questions and answer that will help you. Tutorials are an amazing manner of getting to know. Moreover, you would possibly get get entry to to additional resources. Don’t leave out out on them. Here are a few critical C++ tutorials to strengthen your C++ information.

There is plenty more required than having good enough understanding of all the ideas which are probable to be asked at some point of a job interview. The demeanor and mannerism is something that is additionally paid interest to through the interviewer. So, be clean and precise, don’t wander away the topic.

For every interview, you also are anticipated to know the Data Structures and Algorithms fundamentals. To put together for the identical, take this Udemy direction: Mastering Data Structures & Algorithms the use of C and C++.

If you select reading bodily books then right here is the best C++ interview questions e-book: Elements of Programming Interviews: The Insiders' Guide 2d Edition.

Also, you want not beat across the bush. If you don’t understand the answer, it’s good enough. Simply accept it, instead of ranting meaninglessly. Remember, it’s now not only what you say, however additionally how you say it!

All the very high-quality!