A list of top frequently asked C++ interview questions and answers are given below.
1. What is C++?
C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985.
C++ is a superset of C with the major addition of classes in C language.
Initially, Stroustrup called the new language "C with classes". However, after some time the name was changed to C++. The idea of C++ comes from the C increment operator ++.
2. What are the advantages of C++?
C++ doesn't only maintains all aspects from C language, it also simplifies memory management and adds several features like:
a. C++ is a highly portable language that means that the software developed using C++ language can run on any platform.
b. C++ is an object-oriented programming language that includes concepts such as classes, objects, inheritance, polymorphism, abstraction.
c. C++ has the concept of inheritance. Through inheritance, one can eliminate the redundant code and can reuse the existing classes.
d. Data hiding helps the programmer to build secure programs so that the program cannot be attacked by the invaders.
e. Message passing is a technique used for communication between the objects.
f. C++ contains a rich function library.
3. What is the reference ?
Reference behaves like an alias for an existing variable, i.e., it is a temporary variable.
The reference variable does not require any indirection operator to access the value. A reference variable can be used directly to access the value.
Once the reference variable is assigned, then it cannot be reassigned with different address values.
A null value cannot be assigned to the reference variable.
It is necessary to initialize the variable at the time of the declaration.
4. What is the pointer?
The pointer is a variable that stores the address of a variable.
The pointer variable requires an indirection operator to access the value of a variable.
The pointer variable is an independent variable that means that it can be reassigned to point to different objects.
A null value can be assigned to the reference variable.
It is not necessary to initialize the variable at the time of the declaration.
5. What is a class?
The class is a user-defined data type. The class is declared with the keyword class. The class contains the data members, and member functions whose access is defined by the three modifiers are private, public and protected. The class defines the type of definition of the category of things. It defines a datatype, but it does not define the data it just specifies the structure of data.
You can create N number of objects from a class.
6. Define namespace in C++.
The namespace is a logical division of the code which is designed to stop the naming conflict.
The namespace defines the scope where the identifiers such as variables, class, functions are declared.
The main purpose of using namespace in C++ is to remove the ambiguity. Ambiguity occurs when a different task occurs with the same name.
For example: if there are two functions that exist with the same name such as add(). In order to prevent this ambiguity, the namespace is used. Functions are declared in different namespaces.
C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement "using namespace std;" includes the namespace "std" in our program.
7. Define token in C++.
A token in C++ can be a keyword, identifier, literal, constant and symbol.
8. Who was the creator of C++?
Bjarne Stroustrup.
9. Define 'std'.
Std is the default namespace standard used in C++.
10. Which programming language's unsatisfactory performance led to the discovery of C++?
C++was discovered in order to cope with the disadvantages of C.
11. How delete [] is different from delete?
Delete is used to release a unit of memory, delete[] is used to release an array.
12. What is an object?
The Object is the instance of a class. A class provides a blueprint for objects. So you can create an object from a class. The objects of a class are declared with the same sort of declaration that we declare variables of basic types.
13. What is the difference between an array and a list?
An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
Array memory allocation is static and continuous while List memory allocation is dynamic and random.
In Array, users don't need to keep in track of next memory allocation while In the list, the user has to keep in track of the next location where memory is allocated.
14. What is function overriding?
If you inherit a class into a derived class and provide a definition for one of the base class's function again inside the derived class, then this function is called overridden function, and this mechanism is known as function overriding.
15. What is virtual inheritance?
Virtual inheritance facilitates you to create only one copy of each object even if the object appears more than once in the hierarchy.
16. What is a constructor?
A Constructor is a special method that initializes an object. Its name must be the same as the class name.
17. Explain this pointer?
This pointer holds the address of the current object.