Programs

21 Most Read C++ Interview Questions & Answers [For Freshers]

Even today, C++ is as popular as it was back in the 80s. This general-purpose, compiled, and multi-paradigm (object-oriented, procedural, and functional) programming language plays a crucial role in the IT industry, particularly in software development.

Developers worldwide use C++ to build systems software, database software, embedded software, enterprise applications, GUI-based applications, compilers, advanced computation & graphics, operating systems, browsers, games, cloud systems, etc. Naturally, C++ is still a highly relevant programming language. 

In this post, we’ve created a list of 21 C++ interview questions that you should know if you aspire to build a career in Software Development. These C++ interview questions and answers will help you break the ice on the subject! 

C++ Interview Questions & Answers

  1. Define “Class” and “Object.”

In C++, a class is a user-defined data type that reflects different entities, actions, and features. A class contains the data members along with their functions. The access to these data members and functions is defined by three modifiers – private, public, and protected. While classes define a data type, they merely specify the data structure instead of defining the data. 

An object is an instance of a class. It is a run-time entity that can represent a person, place, or a thing. 

Objects operate on data members and member functions. It contains fields, methods, and constructors. For instance, while a car is an object, its various features like wheels, design, color, brackets, etc., are instances of its class. 

  1. What is an “Access Specifier?”

An access specifier determines how a class’s members (functions and variables) can be accessed outside the class’s scope. C++ has three types of access specifiers

  • Private – A private specifier means that the class members can only be accessed within the same class in which they are declared. In this case, child classes cannot access private members of the parent class.
  • Protected – A protected specifier allows child classes to access the protected data members of the parent class.  
  • Public – The class members that are declared “public” can be accessed throughout a C++ code.
  1. What is a mutable storage class specifier?

A mutable storage class specifier applies only to a class’s non-static and non-constant member variables for altering the constant class object’s member by declaring it.

  1. What is Encapsulation?

Encapsulation refers to wrapping or binding data members and their functions within a class. This process is done for security reasons as it restricts access from any outside method. If the data member is private, only its member function can access the data. 

  1. What is Abstraction?

Abstraction is a process of hiding internal implementations and displaying only the essential details. The access to data members and functions depends on the modifier being used. So, if a public keyword is used to define the members, they can be accessed by anyone. However, if a private keyword defines the members, you cannot access them via outside methods. 

  1. What is a Namespace?

A namespace facilitates the logical division of a code. It is used in C++ programs to prevent/resolve the naming conflict of identifiers by assigning different namespaces to them. A namespace defines the scope where an identifier (variables, class, and functions) is declared. Thus, the main aim of using a namespace is to eliminate ambiguity from code. 

  1. Define “Polymorphism.” How many types of polymorphism exist in C++?

Literally, polymorphism means the presence of multiple forms. In C++, polymorphism occurs when more than one function exists under the same name but with varied functionalities. 

Polymorphism is of two types:

  • Dynamic polymorphism, a.k.a. “overriding” in C++, helps you determine which function method must be executed at run-time instead of compile time. Function overriding is an excellent instance of dynamic polymorphism. In function overriding, the child class contains the method that already exists in the parent class. Consequently, the child class overrides the method of the parent class. In this case, both the parent and child classes hold the same function with different definitions. 
  • Static polymorphism is implemented at the time of code compilation (hence, it is also known as compile-time polymorphism). Method overloading is a perfect example of static polymorphism. Method overloading allows the presence of more than one function having the same name but with different functionalities. 
  1. Explain the purpose of the “Volatile” keyword.

The “volatile” function is used to declare that a particular variable is volatile. This directs the compiler to change the variable externally, thereby avoiding compiler optimization on the variable reference. 

Read: Java Interview Questions & Answers

  1. What is an Inline function?

Any function having the “inline” keyword as its prefix before the function definition is an inline function. They are primarily used to reduce the function call overhead. When an inline function is called, the entire code will either get inserted or substituted at the specific point of the inline function call. Since the compiler treats inline functions as macros, they execute much faster. Thus, declaring small inline functions help increase the code’s efficiency.

The standard syntax of an inline function is:

Inline return-type function-name(parameters)

{

// Function code goes here

}

  1. What is “this” pointer?

In C++, “this” pointer is a constant pointer that contains the memory address of the current object. It is passed as a hidden argument to all the non-static member function calls. Also, this pointer exists a local variable within the body of all the non-static functions.

ReadSQL Interview Questions & Answers

  1. Does C++ have a string primitive data type?

C++ cannot have a string primitive data type, but rather it has a class from the Standard Template Library (STL).

  1. What is the function of the scope resolution operator?

The scope resolution operator serves two purposes:

  • It helps resolve the scope of global variables.
  • When a function is defined outside a class, the scope resolution operator associates that function with the class.
  1. What is a destructor?

The member function of a class is called a destructor. It’s name is same as that of the class name but with a tilde symbol (~) as a prefix. Whenever an object loses its scope, destructors are executed automatically.

  1. Explain the difference between keyword class and keyword struct.

The keyword class represents private members by default, whereas the keyword struct resembles public members by default. 

  1. Define a Pure Virtual function. 

A pure virtual function is one that does not contain any definition. You only need to declare the pure virtual function by assigning 0 in the declaration. Abstract classes in C++ have at least one pure virtual function. 

The standard syntax of a pure virtual function is:

virtual void abc()=0; //pure virtual function.

Check out: 15 Interview Tips to Stand Out in Your Job Interview

  1. What is the purpose of a class template?

A class template is used for creating a family of classes and functions. For instance, you can create a template of an array class that will allow you to create an array including various types (int, float, etc.). You can also create a template for a function. So, if there’s an add() function, you can create multiple versions of add().

  1. What does “Overflow Error” mean?

Overflow error is an arithmetical error that occurs when the output of a mathematical calculation is greater than the actual space allotted by the system.

  1. How are delete and delete[ ] different?

The delete function frees up a single unit of memory allocated using the “new” operator, whereas delete[ ] releases the array of allocated memory was allocated by the new[ ] operator.

  1. What is the purpose of vTable and vptr in C++? How does the compiler treat them?

In C++, a vTable contains function pointers. On the other hand, vptr is a pointer assigned to the vTable. While each class has a vTable, each object has a vptr. The C++ compiler adds additional code at two places to effectively use and maintain vTable and vptr:

In each constructor to set the vptr of the object being created and to point to the vTable of a class. 

In the code having a polymorphic function call. The compiler inserts the code at every location where a polymorphic call is declared to search for the vptr using the base class pointer/reference. Once the vptr is obtained, you can access the vTable of the derived class. You can use the vTable to access and call the derived class function show().

  1. In C++, where is an automatic variable stored?

All local variables are auto variables by default and are stored in the stack memory.

  1. How can you export a function from a DLL?

There are two ways to export a function from a DLL:

  • You can use DLL’s type library.
  • You can refer to the function from the DLL instance. 

Conclusion

These C++ questions and answers are some of the most commonly asked questions in job interviews. Though we’ve listed only a few questions, this should give you a fair idea of how to prepare for a C++ interview. 

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×