Are you searching for resources on C++ interview questions? Here are 10 CPP interview questions and answers to strengthen your preparation.
C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It is popularly known as “C with Classes”. The language has expanded significantly over time, and modern C++ has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation. It is almost always implemented as a compiled language, and many vendors provide C++ compilers, including the Free Software Foundation, LLVM, Microsoft, Intel, Oracle, and IBM, so it is available on many platforms. Source: Wikipedia
1. What is a Class in C++?
A class specifies the structure of data. It can be defined as the blueprint that describes the states of supported types, e.g., the details of a student in a school like a student’s age, name, address, etc. can be treated as a blueprint. And a class provides access to this blueprint or data structure.
2. What is an Object in C++?
C++ interview questions that target the fundamentals of the language are incomplete without a question on objects. An object is the instance of a class, and it has states and behavior. Once an object has been created, you can access the defined members and functions of that object’s class.
3. What are the features of Object-oriented programming (OOP)?
The core features of OOP are:
- Abstraction: It is used for hiding the internal implementation and displays only the required details to the user. Abstraction can be implemented with the help of an abstract class or interface. An example of abstraction is a calculator. A calculator only shows the final result and not the processing. Thus, a calculator abstracts the calculations and shows only the necessary information, i.e., the output to the user.
- Encapsulation: It enables the concept of data hiding. Encapsulation is a mechanism that binds the data and operations together and hides the details from the user. Encapsulation can be achieved with the help of access specifiers in C++: public, private, and protected. With the help of these access specifiers, we can provide access or prevent the access directly to a user.
- Inheritance: Inheritance is the process of establishing a relationship between a “parent” class and a “child” class. The latter class can inherit the properties of the former class. The parent class is also known as the “base class” and the child class is known as the “derived class”. Inheritance is used for code reusability and extending the parent class.
- Polymorphism: Polymorphism refers to many forms of the same message. It describes the behavior of a class’ function when it is called. A different function may be called based on the type of object that invokes the function.
4. What are the access specifiers in C++?
In an object-oriented language, access specifiers are the keywords that set the accessibility of classes, function, methods, and other members. There are 3 access specifiers in C++:
- Public: The members or fields that are declared as public are accessed both inside and outside the class.
- Private: The members or fields that are declared as private cannot be accessed outside the class. They can be accessed only inside the class.
- Protected: The members or fields that are declared as protected can be accessed outside the class but only in a class derived from the former class.
5. What is the difference between Abstract class and Interface?
The differences between an abstract class and an interface are as follows:
|1||It can have an instance method and implementation.||It is implicitly abstract and cannot have any state or implementation.|
|2||It can extend both classes and interfaces.||It can extend only the interface.|
|3||It cannot be instantiated.||It is absolutely abstract and cannot be instantiated.|
|4||Members can be declared as public, private, or protected.||Members are public by default.|
|5||The Abstract class is fast.||It is comparatively slow.|
|6||Variables or fields can be declared as non-final.||Variables or fields are final.|
6. What are the types of Inheritance?
The different types of inheritance are:
- Single Inheritance: Inheritance consisting of only one parent class.
- Multiple Inheritance: Inheritance where a derived class inherits the properties of two or more classes, i.e., multiple parent classes are involved.
- Multilevel Inheritance: In this type of inheritance, the derived class is a base class for another class.
- Hierarchical Inheritance: It refers to the inheritance structure where multiple derived classes inherit the properties of the same base class.
- Hybrid Inheritance: It is also known as virtual inheritance. It is a combination of multilevel and hierarchical inheritance.
7. What are data types and variables in C++?
Data types are used to define the type of the variable and the OS allocates the memory space to that variable accordingly. A variable can be thought of as a name to access the memory space. A value can then be stored at this memory space and be retrieved using the variable.
Consider an example: int X. Here ‘int’ is the data type and ‘X’ is the variable. The data types you can specify are int, char, float, long, bool, double, and void.
8. What are constructors and destructors in C++?
The constructor is a function that is executed when an object of the class is created. It has the same name as the class name. A constructor may be either default or parameterized. The default constructor, need not be defined and does not have any parameter. A parameterized constructor needs to be declared in the class and the parameters need to be initialized.
A destructor is executed when the objects of a class are no longer in use. It has the same name as the class name but should be prefixed with a tilde (~) sign. It is used for releasing the resources.
9. What are virtual functions in C++?
The concept of virtual function falls in the list of advanced C++ interview questions. A virtual function is used to replace the implementation of the parent class. These functions are declared in the class with the keyword ‘virtual’. When the parent class type reference is initialized with an object of child class type and the overridden method (declared as virtual) is invoked using the parent class reference, then the method of child class will get invoked.
10. What are the differences between C and C++?
|1.||It is based on procedural programming.||It is based on object-oriented.|
|2.||It follows the top-down approach.||It follows a bottom-up approach.|
|3.||It does not support reference variables||C++ supports reference variables|
|4.||In C, the data is less secure.||C++ is more secure comparatively.|
|5.||Scanf() and printf()are used for input and output.||Cin and cout are used for input and output.|
|6.||C cannot use functions in structure.||C++ can use functions in structure.|
If you have appeared for a C++ interview and think there are some other concepts and topics one must be aware of, do comment below.