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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
- 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.
- 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).
- 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.
- 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.
- 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.
- 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.
- 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().
- 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.
- 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.
- 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().
- In C++, where is an automatic variable stored?
All local variables are auto variables by default and are stored in the stack memory.
- 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.
Enroll in Software Engineering Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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 Executive PG Program 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.