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 is released in 1985.
2) What are the advantages of C++?
C++ doesn't only maintains all aspects from C language, it also simplify memory management and add several features like:
- Includes a new datatype known as a class.
- Allows object oriented programming.
3) What is the difference between C and C++?
No. | C | C++ |
---|---|---|
1) | C follows the procedural style programming. | C++ is multi-paradigm. It supports both procedural and object oriented. |
2) | Data is less secured in C. | In C++, you can use modifiers for class members to make it inaccessible for outside users. |
3) | C follows the top-down approach. | C++ follows the bottom-up approach. |
4) | C does not support function overloading. | C++ supports function overloading. |
5) | In C, you can't use functions in structure. | In C++, you can use functions in structure. |
6) | C does not support reference variables. | C++ supports reference variables. |
6) | In C, scanf() and printf() are mainly used for input/output. | C++ mainly uses stream cin and cout to perform input and output operations. |
4) What is the difference between reference and pointer?
No. | Reference | Pointer |
---|---|---|
1) | References are less powerful than pointers. Once a reference is created, it can't refer to other object later. | Pointers provide the powerful facilities than references. |
2) | References are safer and easier to use than pointers. | Pointers are comparatively difficult to use. |
5) What is a class?
Class is a user-defined data type. Class defines the type definition of 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) What is an object?
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.
7) What are the C++ access specifiers?
The access specifiers are used to define how to functions and variables can be accessed outside the class.
There are three types of access specifiers:
- Private: Functions and variables declared as private can be accessed only within the same class and they cannot be accessed outside the class they are declared.
- Public: Functions and variables declared under public can be accessed from anywhere.
- Protected: Functions and variables declared as protected cannot be accessed outside the class except a child class. This specifier is generally used in inheritance.
8) What is Object Oriented Programming (OOP)?
OOP is a methodology or paradigm that provides many concepts. The basic concepts of Object Oriented Programming are given below:
Classes and Objects: Classes are used to specify the structure of the data. They define datatype. You can create any number of objects from a class. Objects are the instances of classes.
Encapsulation: Encapsulation is a mechanism which binds the data and associated operations together and thus hide the data from outside world. Encapsulation is also known as data hiding. In C++, It is achieved using the access specifiers i.e. public, private and protected .
Abstraction: Abstraction is used to hide the internal implementations and show only the necessary details to the outer world. Data abstraction is implemented using interfaces and abstract classes in C++.
Some people confused about Encapsulation and abstraction. But they both are different.
Inheritance: Inheritance is used to inherit the property of one class into another class. It facilitates you to define one class in term of another class.
9) What is the difference between array and a list?
- Array is a collection of homogeneous elements while 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 list user has to keep in track of next location where memory is allocated.
10) What is the difference between new() and malloc()?
- new() is a preprocessor while malloc() is a function.
- There is no need to allocate the memory while using "new" but in malloc() you have to use sizeof().
- "new" initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.
11) What are the methods of exporting a function from a DLL?
There are two ways:
- By using the DLL's type library.
- Taking a reference to the function from the DLL instance.
12) Define friend function.
Friend function acts as friend of the class. It can access the private and protected members of the class. The friend function is not a member of the class but it must be listed in the class definition.
13) What is virtual function?
A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.
14) When should we use multiple inheritance?
You can answer this question in three manners:
15) What is the destructor?
Destructor is used to delete any extra resources allocated by the object.16) What is an overflow error?
It is a type of arithmetical error. It is happen when the result of an arithmetical operation been greater than the actual space provided by the system.17) What is overloading?
C++ facilitates you to specify more than one definition for a function name or an operator in the same scope. It is called function overloading and operator overloading respectively.18) 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.19) What is virtual inheritance?
Virtual inheritance facilitates you to create only one copy of each object even if the object appears more than one in the hierarchy.20) What is constructor?
Constructor is a special method that initializes object. It name must be same as class name.21) What is the purpose of "delete" operator?
The "delete" operator is used to release the dynamic memory created by "new" operator.22) Explain this pointer?
This pointer holds the address of current object.23) What does Scope Resolution operator?
A scope resolution operator(::) is used to define the member function outside the class.24) What is the difference between delete and delete[]?
Delete [] is used to release the array of allocated memory which was allocated using new[] whereas delete is used to release one chunk of memory which was allocated using new.25) Define the private, protected and public in C++?
Private: The data members and functions cannot be accessed from outside the class.Protected: The data members and functions are accessible to derived class only.
Public: The data members and functions can be accessed from outside the class.
COMMENTS