Home >>Interview Questions >C++ Interview Questions

C++ Interview Questions

Interview questions Answers of C++

1. What is C++?

Bjarne Stroustrup is known as the founder and developer of the C++ language that is basically an object-oriented programming language. 1985 was the year in which it was released. In the starting phase the C++ language was called as”C with classes” because of the fact that this language is basically a superset of C with the major addition of classes in the C language. The idea of the existing name of the C++ language came from the C increment operator ++ and after a while this was renowned as the permanent name of the language that is C++.

2. List the advantages of C++

C++ language has many features that are hard to enlist all at once. C++ language is known to maintain every aspects of the C language and to simplify memory management. Here are the most important advantages of the C++ language:

  • C++ language is a highly adaptable and is very portable language that simply boils down to the fact that the software that has been developed using C++ language can be performed or run on various platforms.
  • C++ language is basically an object-oriented programming language that generally includes the concepts like inheritance, classes, objects, abstraction, and polymorphism.
  • The concept of inheritance is included in the C++ language and in layman’s terms the inheritance is used to eliminate the redundant code and reuse the existing classes of the C++ language.
  • In order to build the secure programs; Data Hiding is proved to be most helpful to the programmers. This is basically done to protect the program from the attack of the invaders.
  • Message passing is a technique used for In order to communicate between the objects in C++ language the technique that is called message passing is generally used.
  • C++ is generally known for its extremely rich function library.

3. Elaborate the basic or common differences that are present between C and C++?

Here are the most common differences between C and C++:

C C++
Dennis Ritchie is known as the developer of the C language. Bjarne Stroustrup is known as the developer and founder of the C++ language.
C is generally known as a structured programming language. C++ is generally known as the language that supports both structural and object-oriented programming language.
C is generally referred as a subset of C++. C++ is generally referred as a superset of C.
The data and functions are the free entities in the C language. The data and the functions are generally encapsulated together in the form of a project in the C++ language.
The data hiding is not supported by the C language hence, the data can be used generally by the outside world. Data hiding is generally supported by the C++ language hence, the data cannot be accessed by the outer world.
Neither the function nor operator overloading is supported by the C language. Both function and operator overloading is generally supported by the C++ language.
The function cannot be implemented inside the structures in the C language. The function can generally be implemented inside the structures in the C++ language.
In the C language, reference variables are not supported. The reference variables are generally supported by the C++ language.
The virtual and friend functions are not supported by the C language. The virtual and friend functions both are supported by the C++ language.
scanf() and printf() are generally used for input/output in the C language. Stream cin and cout are generally used in the C++ language in order to perform input and output operations.

4. Explain the various OOPs concepts in C++?

Here are the various concepts that are in the OOPs in the C++ language:

The class in C++ is basically a data type that is user-defined data type that generally defines the properties and functions of the class. For instance, Human being can be defined as a class. The properties of human being will be the body parts and the functions will be the actions that will be performed by the body parts. Any memory space is generally not occupied by the class. Hence, it can be said that the only representation of the data is the class.

Reusability is generally provided by the inheritance. By reusability it is meant that the functionalities of the existing class can be used by the users. It is generally known to eliminate the redundancy of the code. A technique that is used to derive a new class from the old class is known as inheritance, where the old class is called as the base class and the new class is referred as the derived class.

A run-time entity in C++ is basically known as an Object. The instance of the class can also be referred as the object and they can be used in order to represent a person, place or any other item. Operations on both the data members and the member functions can be performed by an object. As we know that the class does not occupy any of the memory space and the space is allocated for the variable in a heap whenever an object is created generally by the use of a new keyword after this in the stack memory the starting address is stored. And whenever an object is created without the use of a new keyword, then the space is not allocated in the heap memory and hence the object consists of the null value that is in the stack.

The technique by which the wrapping of the data members and member functions is done in to a single unit is known as Encapsulation. It mainly does the work of binding the data within a class except any outside method that can access the data. The member function is only allowed to access the data if and only if the data member is private.

The technique by which only the essential details are shown without even representing the implementation details is known as the Abstraction. The members can be accessed from the outside also provided that the members are defined with a public keyword. The members cannot be accessed from the outside methods, if the members are defined with a private keyword

The process by which the application UI and business logic are banded is known as Data binding. The application UI will automatically reflect any changes that are made in the business logic.

Polymorphism basically means multiple forms. In other words, Polymorphism defines the fact of having more than one function that are having the same name and having very different functionalities. Polymorphism is basically of two types that are depicted below:

  • Class:
  • Inheritance:
  • Object:
  • Encapsulation:
  • Abstraction:
  • Data binding:
  • Polymorphism:
  • Static polymorphism, it is also known as the early binding in the C++.
  • Dynamic polymorphism it is also known as the late binding in the C++.

5. What are the types of polymorphism that are present in the C++ programming language?

Polymorphism basically means multiple forms. In other words, Polymorphism defines the fact of having more than one function that are having the same name and having very different functionalities.

Polymorphism is generally of two types in the C++ programming language:

Runtime polymorphism is generally called as the dynamic polymorphism. In order to learn this in the layman’s terms, it can be said that the function overriding is basically an example of runtime polymorphism. Function overriding basically elaborates to the fact of the presence of the same method in the parent class that is contained by the child class. Because of it the child class will override the method of the parent class. Parent and child class both will contain the same function just with the different definitions, whenever there is a case of function overriding. Runtime polymorphism is basically the call to the function that is generally determined at the runtime.

Compile-time polymorphism is generally called as the static polymorphism in C++. Compile-time polymorphism is basically the polymorphism that is implemented at the time of compiling. In order to understand the compile-time polymorphism in layman’s terms it can be said that one of its example is Method overloading.

Method overloading: A technique that permits you to have more than one function that too with the exact same function name but the functionality will be different is known to be the Method Overloading.

The possibility of the Method overloading depends on the following basis:

  • Runtime polymorphism
  • Compile time polymorphism
  • The overloaded function’s return type.
  • The parameter’ types that are being passed to the function.
  • The parameter’s numbers that are being passed to the function.

6. What is meant by the namespace in C++

  • A logical division of the code that is basically designed to stop the naming conflict in C++ programming language is known as the Namespace.
  • The namespace is generally used to define the scope where the identifiers are declared like variables, class, functions.
  • Removing the ambiguity in the C++ is known to be the main reason of using the namespace. Whenever some of the different task occurs in C++ with the same name then there is an occurrence of Ambiguity in C++.
  • For instance: Let’s suppose that there are two functions that exists with the exact same name like add(). In this case the namespace will be used to prevent this ambiguity. Functions are generally known to be declared in different namespaces in the C++.
  • std that generally consists of inbuilt classes and functions is basically a standard namespace that is contained in the C++. Hence, The namespace "std" is included in the program by the use of the statement “using namespace std;”.

7. Define token in C++

A token in C++ programming language can be defined as a keyword, literal, identifier constant and symbol.

8. Name the creator of C++?

Bjarne Stroustrup is known as the creator of the C++ programming language.

9. Name the operations that are permitted on pointers.

Here are the operations that can be performed generally on the pointers in C++:

1. Incrementing or decrementing a pointer: Incrementing a pointer basically means that the programmer has the access to increment a particular pointer by the size of a data type towards which it is pointing.

Increment pointers are two types in C++:

a. Pre-increment pointer: The operand is generally incremented by 1 by the pre-increment operator, and the value of the expression takes the place of the resulting value of the incremented operand.

b. Post-increment pointer: The operand is generally incremented by 1 by the post-increment operator and the value of the expression becomes the value of the operand that is generally prior to the incremented value of the provided operand.

10. Define 'std'

Std can generally be defined as the default namespace standard that is majorly used in C++programming language.

11. Which programming language is responsible for the discovery of C++?

The disadvantages of C and the inability of it towards the object oriented programming led to the discovery of C++ programming language.

How delete [] is different from delete?

In order to release a unit of memory delete is used in the C++ programming language. Delete[] is also majorly used in order to release an array.

13. What is the full form of STL in C++?

STL in C++ expands to Standard Template Library.

14. What do you mean by an object?

The instance of a class is generally known as the Object in the C++ programming language. In order to create an object from a class in C++, a blueprint for objects is generally provided by the class. The same declaration that is used by the programmers to declare variables of basic types in C++ is used to declare the objects of a class.

15. What do you mean by the C++ access specifiers?

In order to define the process of accessing the functions and variables outside the class, the access specifiers are used in the C++ programming language.

There are generally three types of access specifiers that are present in the C++:

  • Private: The Functions and variables that are declared as private by the use of private specifier can only be accessed within the same class and hence, they are not accessible from the outside of the class in which they are declared.
  • Public: The Functions and variables that are declared public under the public specifier can be accessed from anywhere in the program s as they have been declared public.
  • Protected: The Functions and variables that are declared protected under the protected specifier can only be accessed in by the child class and the outside world has no authority to access the same. Please note that this specifier is widely used in inheritance in the C++ programming language.

16. What is Object Oriented Programming (OOP)

A methodology or paradigm that generally provides various concepts is known as OOP in C++ programming language. Here are the basic concepts that are included in the Object Oriented Programming is depicted below:

  • Classes and Objects: In order to specify the structure of the data, classes are used in C++. As discussed, these are basically used to define the data type. Programmers can create numerous of objects from a class and as a fact we known that objects are basically the instances of classes.
  • Encapsulation: A mechanism that is used to bind the data and operations that are associated with it together and also hides the data from the outside world’s reach is known as Encapsulation in C++. In simple words, Encapsulation can be called data hiding. By using the access specifiers (public, private and protected) in C++ encapsulation can be achieved.
  • Abstraction: In order to hide the internal implementations and only display the mandatory details to the outside world; the abstraction is used in the C++. Just by using the interfaces and abstract classes in the C++ programming language, the implementation of the Data abstraction is done. Please note that numerous programmers gets confused between Encapsulation and abstraction and use them interchangeably but as a fact we know that these are different from each other.
  • Inheritance:Inheritance is used In order to inherit the property of one class into another class; the inheritance is used in C++ programming language. With the use of inheritance, programmers can easily define one class in the terms of another class.

17. List the differences between an array and a list?

Here are the difference between the arrays and the list in the C++ programming language:

  • A collection of homogeneous elements is known as Arrays in C++ whereas a collection of heterogeneous elements is known as a List in C++.
  • The memory allocation in the Arrays is generally static and continuous whereas the memory allocation is basically dynamic and random in the case of Lists.
  • The programmers don't have to keep in track of the next memory allocation in Array whereas the programmers do have to keep in track of the next location where the memory has been allocated in Lists.

18. List the differences between new() and malloc().

Here are the list of differences that are present between the new() and malloc() in the C++ programming language:

  • new() is basically known as a preprocessor whereas malloc() is known as a function.
  • While using new(), there is generally no need to allocate the memory whereas there is a need to allocate the memory by the use of sizeof() in malloc().
  • The new memory is generally initialized to 0 by new () whereas a random value is provided by malloc () in the memory location that has been newly allotted.
  • The constructor is called for the object initialization after the memory allocation is done by the new() operator whereas the memory is allocated by the malloc() function but for the object initialization the constructor is not called.
  • The new() is generally known to be the fastest that the malloc() function in C++ as the operators are faster than the function and new() is an operator.

19. What are the methods that are used for exporting a function from a DLL?

There are generally two ways by which a function can be exported from a DLL in C++ programming language:

  • By the use of the DLL's type library in C++.
  • Another method is to take a reference from the DLL instance to the function.

20. When the multiple inheritance should be used?

There are three simple ways to answer this question:

  • Rarely
  • Never
  • In case it is detected that the problem domain cannot be modeled accurately in any other way.

21. What do you mean by Friend Function?

As the name suggests, the friend function, generally behaves as a friend of the class in C++ programming language. The private and protected members of the class are generally accessed by the friend function. It is mandatory to list the friend function in the class definition even when we know for a fact that it is not even a member of the class. The private data of the class cannot be accessed by the non-member functions. But in various times it is seen that accessing the data becomes mandatory for the non-member functions. And as a fact, the friend function also belongs to the family of the non-member function and as discussed above it possesses the ability to access the data of the class that has been specified as private.

To make an outside function friendly to the class, we need A function is needed to be declared the function as a friend of the class, in order to make an outside function friendly to the class.

Here are the characteristics that are important for the friend function in C++:

  • The friend function generally does not lie in the scope of the class in which it has been declared basically.
  • The friend function in C++ cannot be called just by using the object of the class as it has been already been discussed that it is not in the scope of the class. Hence, the friend function has the ability that it can be invoked like a normal function.
  • The private members cannot be accessed by the friend function directly hence, an object name and dot operator should be used by the friend function with each member name.
  • The objects are basically used as arguments by the friend function.

22. What do you mean by a destructor?

In order to delete any extra resources that have been allocated by the object, the destructor is used in C++. Whenever the object goes out of the scope, the destructor function is called automatically.

Here are some of the rules of the destructor that are generally implemented:

  • The first rule is that the destructors generally have the exact same name as of the class name preceded by tilde (~).
  • The second rule is that the destructor does not consist of any argument and no return type.

23. What do you mean by an overflow error?

Overflow error in C++ programming language is basically a type of arithmetical error that generally happens whenever the outcome of an arithmetical operation comes out to be greater than the actual space that has been provided by the system.

24. What is overloading?

Overloading is basically a single object that behaves in numerous ways in C++. In simple words it can be understood as, a single object that has the exact same name instead of this it provides the different versions of the exact same function. The C++ programming language generally facilitates a privilege to the programmers by which then can specify more than one definition for a function name or an operator that is in the same scope and it is known as the function overloading and operator overloading respectively.

Types of Overloading:

There are generally two types of overloading that are depicted below:

  • Operator overloading: A compile-time polymorphism that consists of a standard operator that is being overloaded in order to deliver a user-defined definition to the same is known as Operator Overloading. For instance, in order to perform the addition operation on the data types like int, float, etc., the’+’ operator is generally overloaded in the C++ programming language.

Here are the ways in which the operator overloading can be implemented:

  1. Member function
  2. Non-Member function
  3. Friend Function
  • Function overloading: Function overloading in C++ is also a type of compile-time polymorphism. Function overloading can be used to define a family of functions that has the exact same name. Different operations would be performed by the function that is generally based on the argument list in the function call. The number of the arguments and the type of the arguments that are present in the argument list are ones that are responsible to choose the function that is to be invoked
  • 25. What is function overriding?

Whenever a class is inherited into a derived class by the programmer and a definition is provided for one of the base class's function repeatedly inside the derived class, then this function is known to be the overridden function in the C++ programming language. And the mechanism that is responsible for driving all this is called as the function overriding.

26. What do you mean by virtual inheritance?

Whenever there is a need to create one copy of each object only, even when the object makes the appearance more than once in the order of hierarchy in the C++ programming language then the Virtual inheritance is used.

27. What do you mean by a constructor?

A special method that is generally used to initialize an object is known as the Constructor. Please note that the name of the constructor must be the same as the class name.

28. What is the work of the "delete" operator?

In order to release the dynamic memory that has been created by the "new" operator, the “delete” operator is generally used in the C++ programming language.

29. Elaborate this pointer?

The address of the current object is generally held by the ‘This pointer’ in C++ language.

30. What is the work of does Scope Resolution operator?

In order to define the member function outside of the class, the scope resolution operator that is generally denoted by the double colon (::) is used.

31. Please list the main difference between delete and delete []?

In order to release the array of allocated memory that has been allocated by the use of new[] at the first place, Delete [] is used. And on the other hand delete is generally used to release only one chunk of memory that has been allocated by the use of new.

32. What do you mean by a pure virtual function?

As the name suggests it is a virtual function that generally does not consists of any definition is known as the pure virtual function in C++ programming language. The pure virtual function generally ends with 0, whereas the normal function in C++ is preceded with a keyword virtual.

33. List the main differences that are between struct and class in C++?

There are many differences that are present between the struct and class in the C++ programming language, here is the list of differences depicted below:

structures Class
A user-defined data type that consists of the variables that are of dissimilar data types is known as the structure in C++. A user-defined data type that generally consists of the member variables and the member functions is known as class in C++ language.
The stack memory is the place where all the variables of a structure are stored. The heap memory is the place where the variables of a class are stored.
The variables cannot be initialized directly by the programmers. The variables can be initialized directly by the programmers.
The access specifier of the variable is set to "public" by default, in case the access specifier is not specified. The access specifier of the variable is set to "private" by default, in case the access specifier is not specified.
The "structure variable" is just an instance of the structure.
Here is how the structure is declared in C++: struct structure_name { // body of structure; } ; Here is how the class is declared in C++: class class_name { // body of class; }
By the use of a struct keyword, a structure is declared in the C++ language. By the use of a class keyword, a class is declared in the C++ language.
The inheritance is generally not supported by the structure. The concept of inheritance is generally supported by the structure.
A structure's type is basically a value type. A class's type is basically a reference type.

34. What do you mean by a class template?

In order to create a family of classes and functions in the C++ programming language, a class template is used. For instance, a template of an array class can be created by the programmer that will enable them to create an array that will be of various types like int, float, char, etc. simultaneously, the programmer can create a template for a function, as for implementation, let’s suppose a programmer possesses a function add () and by that he/she can able to create the multiple versions of add ().

35. Describe the difference between function overloading and operator overloading in C++ language?

Function overloading: Function overloading is generally defined when the programmer has more than one version of the exact same function. The versions of a function can generally have a different set of parameters in other words it can be said that the versions of a function will have different signature every time.

Operator overloading: The standard operator in C++ can generally be redefined in order to have a very different meaning, whenever it gets applied to the instances of a class. This scenario in C++ programming language is known as the Operator overloading.

36. What do you mean by a virtual destructor?

In order to destroy the derived class object, the virtual destructor in C++ programming language is generally used in the base class. There is a process of declaring the virtual destructor, It is generally declared by the use of tilde (~) followed by the virtual keyword before the constructor.

37. What do you mean by class?

A user-defined data type is known as a Class in the C++ language. In order to declare a class you have to use the keyword class. The class in C++ generally contains the data members, and member functions and their access is generally defined by these three modifiers that are: private, public and protected. The type definition of the category of things is defined by the class in C++ language. The class in C++ is used to define a data type, but when it defining the data the class just specifies the structure of data. Programmers can create N number of objects from a class in C++.

38. What is the difference between reference and pointer?

Here are the major differences between reference and pointer:

Reference Pointer
Reference are basically a temporary variable and hence they generally behaves like an alias for an existing variable. A variable that stores the address of a variable is known as a pointer.
Any indirection operator is not required by the reference in order to access the value. In order to directly access the value a reference variable is used. An indirection operator is required by the pointer variable in order to access the value of a variable.
In references, if the reference variable is assigned once then it cannot be reassigned with different address values. The pointer variable is generally known as an independent variable that means, it can be reassigned to point towards the different objects.
A null value cannot be get assigned to the reference variable. A null value can generally be assigned to the reference variable.
It is mandatory to initialize the variable just at the time of declaration. It is not at all mandatory to initialize the variable just at the time of declaration.

40. Can you explain about the virtual functions with an example?

Virtual functions are basically called as per the type of the object pointed or referred instead of being called according to the type of pointer or reference and they are generally used with inheritance. In simple words, virtual functions are known to get resolved late basically at the runtime. In order to make a function virtual, the virtual function is used.

Here are the things that are necessary to write a C++ program with runtime polymorphism (also known as the use of virtual functions):

  • A base class and a derived class are needed.
  • A function that has the same name in the base class and the derived class.
  • A pointer or reference of base class type that is basically pointing or referring towards an object of derived class.