C++ Interview Questions [List of 25+ Most Frequently Asked]

c++-interview-questions

C++ came into existence in 1985. It was developed by Bjarne Stroustrup to be an advanced – or better – version of the C programming language. Since then, it has come out on the top of the list of best programming languages.

There are several ways to learn the leading OOPS language, which include reading books, going through online tutorials, and enrolling in offline and online C++ courses.

In this article, we will discuss some of the most popular C++ interview questions that will give you an idea of the type of questions that you can expect to come your way in a C++ job interview. So, here it goes.

Table of Contents

Most Frequently Asked C++ Interview Questions

Basic C++ Interview Questions

1. What are the advantages of C++?

The advantages that the C++ language provides are as follows:

  • Using the object-oriented programming language provides a high degree of portability, so the code written in C++ can run on any platform.
  • Inheritance is a feature of C++. One may remove unnecessary code and reuse the pre-existing classes using inheritance.
  • A crucial component of C++ is memory management, which permits dynamic memory allocation.
  • Since C++ is an OOPs language, data is seen as objects.

2. Enumerate the differences between C and C++.

CC++
It is a structured and procedure-oriented programming language.It is an object-oriented programming language.
Operator and function overloading are not supported in C.Operator and function overloading is supported in C++.
Data in the C programming language can be used by others also as there is no feature of data hiding.Data in C++ cannot be used by outside people as it supports data hiding features.
It is not possible to implement the function inside of structures in C.In C++, a function may be implemented inside of a structure.
C does not support reference variables.The reference variables can be used in C++.

3. What do you understand by ‘std’?

std stands for Standard. It is basically a namespace. The compiler is instructed to add everything from the std namespace and incorporate it into the global namespace with the instruction “using namespace std.” The global namespace is advantageous since it allows us to utilize “cout” and “cin” without needing “std:: operator_.”

4. Define class and object in C++.

A class is a form of user-defined data that includes both member functions and data members. The data variables are known as data members, and the methods used to manipulate these variables are known as member functions. A class is defined as shown below:

The instances of a class are objects. An object can alternatively be referred to as a variable of a class since a class is a user-defined data type. An example of an object is:

5. What are access modifiers?

Access modifiers are used to accomplish data hiding. Accessibility standards for class members are set using access modifiers. It specifies how class members can be accessed from beyond the scope of the class. C++ provides three types of access modifiers, namely Public, Private, and Protected.

6. Explain the difference between prefix and postfix.

Before assigning the value to the expression, the prefix increases the value while in postfix, the expression is given a value before the variable’s value is increased.

7. What do you understand by operator overloading?

We can construct operators for user-defined classes in C++. This indicates that C++ has the capability of giving the operators a distinct meaning specific to a data type; this capability is referred to as operator overloading. For example, if we want to concatenate two strings then we can do it easily by using the “+” operator in the code.

8. What are references in C++?

A variable becomes an alias of an existing variable when it is represented as a reference. A referenced variable is just a named copy of an existing variable, with the understanding that any changes made to the reference variable will also be reflected in the original variable. To describe a reference variable one must use the & symbol before the variable.

Intermediate Level C++ Interview Questions

9. Explain the difference between call by value and call by reference.

Call by ValueCall by Reference
In call by value, functions are called by passing values to them by copying the variables.In call by reference, instead of passing a value, the address of the passed variable is sent while calling a function.
The variable does not ever change as a result of modifications made inside the function. In call by value, the initial value is never changed.Outside of the function, on the passed function, the changes done to the functions are visible. Call by reference essentially modifies the original value.
Different memory locations are used to hold the actual and formal parameters that have been passed. Thus, call by value suffers from a slight memory deficit.The same memory address stores both passed formal and actual parameters. Thereby, it results in increasing the memory efficiency of call by reference in the process.

10. What is a constructor in C++?

A special method that is called automatically at the time of object creation is known as a constructor. It is used for initializing new object’s data members. Constructors should always be given the same name as the class name or structure name. Also, constructors lack a return type since they don’t have a return value. Example:

11. What do you understand from friend class and friend function?

A class that is declared as a friend can access both private and protected members of another class. Accessing private members of another class’s members can be handy occasionally. For instance, the private members of Node might be accessible to a LinkedList class.

When a function is declared in C++ as a friend function, it has access to a class’s protected and private data. By utilizing the friend keyword, the compiler recognizes that the provided function is a friend function. The declaration of a friend function should be made inside the body of a class that starts with the friend keyword in order to access the data. For instance:

12. Define pointers.

A pointer is a type of variable used to hold the memory address of an object. Pointers are used for the following purposes:

  • New objects on the heap are allocated by pointers.
  • Used to pass one function to another.
  • Used to repeatedly iterate through data structures’ or arrays’ elements.

Syntax of a pointer:

                    

13. Define a virtual function.

Virtual functions are members of the base class that are redefined in derived classes. The virtual keyword is used to declare it. Regardless of the reference/pointer type used for the function call, it guarantees that the appropriate function is called for an object. Runtime polymorphism typically makes use of virtual functions.

14. What is the difference between a pointer and a reference?

PointerReference
You can reassign a pointer’s value.The reference value cannot be reassigned.
It can work with arrays.It can’t work with arrays.
A pointer can hold a null value.A reference cannot hold a null value.
A pointer’s memory location cannot be easily accessed since a dereference ‘*’ must be used.Either directly using or readily accessing the reference memory location is possible.

15. What do you understand by the term polymorphism?

Numerous variations of the same entity are referred to as polymorphism. To put it simply, polymorphism is the capacity to present a member function in several ways depending on the kind of object using them.

There are two kinds of polymorphism: compile time polymorphism and run time polymorphism. Compile time polymorphism includes function overloading and operator overloading while run time polymorphism includes function overriding and virtual function.

16. Differentiate between compile time and run time polymorphism.

Compile Time PolymorphismRun Time Polymorphism
It is quick due to early execution knowledge at the time of compilation.Due to the fact that execution is known at runtime, it is slower than compile-time.
Also referred to as static or early binding.Also referred to as dynamic or late binding.

17. What are destructors in C++?

A member function that is automatically called whenever an object exits its scope or is purposefully destroyed via a call to delete is known as the destructor. It has the same name as that of a class. A tilde (~) is used before the destructor name.

There can be only one destructor defined at a time. The destructor is just one approach to getting rid of the object that is created by the constructor. Hence, the destructor cannot be overloaded.

Syntax:

18. When is the return type void() used?

If you don’t wish to return any values, use the void() return type. It states that there is no value returned by the function. The control is returned to the caller after a function with a void return type has finished its duty.

19. Explain the three different types of access specifiers.

The three types of access specifiers used in C++ are:

  1. Public: It allows access to all the data members and member functions outside of a class.
  2. Private: It doesn’t allow accessing data members and member functions outside the class, i.e., they can be accessed inside the class where they are declared.
  3. Protected: The only class that can access functions and variables that have been defined as protected is a child class. Inheritance typically uses this access modifier.

Advanced C++ Interview Questions

20. What is STL?

The Standard Template Library (STL) is a collection of C++ template classes that offer methods and data structures that are often used in programs, such as lists, stacks, and arrays. It is a collection of iterators, algorithms, and container classes. Due to the fact that it is a generic library, its components are parameterized.

21. What is the difference between new and malloc?

new malloc
It is a preprocessor.It is a function.
When using “new,” memory allocation is not required.When using “malloc”, memory allocation is required and it is done using sizeof().
The constructor is called and memory is specified by the new() operator for object initialization.Although the constructor is not called during object initialization, the malloc() function allocates memory.

22. Define inline function. Can there be a recursive inline function?

An inline function is a type of request, not an order, to a compiler that causes our code to be inserted into the body of the main function. If the function’s execution time is shorter than the time required to move from the calling function to the called function, the inline function may constitute overhead.

An inline function cannot be recursive because it only executes at the point from where it is called and does not keep a piece of information on the stack, which is required for recursion.

Syntax:

23. What is the difference between struct and class?

structclass
It is a user-defined variable that contains variables of different data types.It is also a user-defined variable but it consists of member variables and member functions.
If no access specifier is provided, “public” is used as the default access specifier for the variable.If no access specifier is provided, “private” is used as the default access specifier.
Variables cannot be initialized directly.Member variables can be initialized directly.
The struct keyword is used to declare the structure.The class keyword is used to declare the class.
struct doesn’t support the concept of inheritance.class supports the concept of inheritance.
Syntax:

struct structure_name{
body of structure

};

Syntax:

class class_name{
body of class

}

24. What are static data members and static member functions?

A class’s static data member is a regular data member that is preceded by the term static. It runs in a program before the main() function and is initialized to 0 when the class’s first object is created. It has a lifetime scope but is only visible to a specific class.

Syntax:

static data_type data_member;

Special functions used to access another static data member or static member function are known as static member functions. A static member function distributes a single copy of the member function to as many class objects as desired.

Using the class name or objects from the class, we can call the static member function. An error is thrown if any non-static data members or non-static member functions are accessed by a static member function.

Syntax:

classname::function name(parameter);

25. Define block scope variable.

Local scope variables are another name for block scope variables. A local variable is one that is defined within a function, such as main, or within a block, such as loops and if blocks.

It can only be utilized inside the specific function or block where it was declared. Even if the block is contained within a function, a block-scoped variable will not be accessible outside of the block.

26. What are void pointers?

A pointer with no associated datatype is referred to as a void pointer. Any kind of address can be stored in it.

27. Define namespace in C++.

We can grant namespace scope to named things by grouping them into smaller scopes using namespaces, if we don’t use namespace then those named things will be given a global scope.

This makes it possible to separate program components into named, logical scopes. Variables, methods, and class IDs can all be defined or declared in the namespace.

28. Explain the difference between shallow copy and deep copy

Shallow CopyDeep Copy
It is used to store copies of original objects. In the end, only the reference address is copied.Deep copy creates a fresh, independent copy of the complete object using the original object’s memory location.
It is faster.It is slow as compared to shallow copy.
The copied object’s modifications are reflected in the original object.It doesn’t show changes in the original object.

29. Can a program be compiled without the main function?

The main() function is the entry point from where all execution starts, so while a program can be compiled without it, it cannot run.

30. What is a mutable storage class specifier?

In order to make a class data member editable even when it is a component of an object with a const declaration, the mutable storage class specifier must be used. The mutable specifier cannot be used on members that are static, const, or reference.

Conclusion

Even though C++ is almost 4 decades old, it is still going strong. It is an essential skill in the arsenal of any programmer. These C++ interview questions will help you to not only prepare for an upcoming job role but also self-assess your ability to program in the popular object-oriented programming language.

Saw something wrong with the C++ interview questions above? Have some great suggestions to be added to this list? Let us know in the comments. Thanks in advance!

Share Your Thoughts, Queries and Suggestions!