Last updated on Aug. 27, 2021, 3:08 p.m. by Akshay Sadawarte
Most asked C++ interview questions(part-1)
Here are some of the most asked C++ interview questions. Use these to prepare for technical interviews in advance.
1. What are the C++ access specifiers?
In C++ there are the following access specifiers:
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the derived class.
Private: All data members and member functions are not accessible outside the class
(most asked c++ interview questions)(placement preparation)
In call by value method, we pass a copy of the parameter to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.
In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument.
(most asked c++ interview questions)(placement preparation)
3. What is the difference between equal to (==) and Assignment Operator (=)?
In C++, equal to (==) and assignment operator (=) are two completely different operators.
Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.
The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.
(most asked c++ interview questions)(placement preparation)
4. What is an Inline function in C++?
Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.
Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.
5. What is Namespace?
Namespace allows us to group a set of global classes, objects and/or functions under a specific name.
format:
namespace identifier { namespace-body }
Where identifier is any valid identifier and the namespace-body is the set of classes, objects, and functions that are included within the namespace. Namespaces are especially useful in the case where there is a possibility for more than one object to have the same name, resulting in name clashes.
C++ does not only maintains all aspects from C language, it also simplifies memory management and adds several features like:
(most asked c++ interview questions)(placement preparation)
7. What is Run-time polymorphism
Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child class both contains the same function with the different definition. The call to the function is determined at runtime is known as runtime polymorphism.
STL stands for Standard Template Library.
There are two ways of exporting a function from a DLL:
(most asked c++ interview questions)(placement preparation)
Friend function acts as a 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. The non-member function cannot access the private data of the class. Sometimes, it is necessary for the non-member function to access the data. The friend function is a non-member function and has the ability to access the private data of the class.
"Volatile" is a function that helps in declaring that the particular variable is volatile and thereby directs the compiler to change the variable externally- this way, the compiler optimization on the variable reference can be avoided.
No, a destructor cannot be overloaded, and it has the only form without the parameters.
13. Why cannot a constructor and destructor be inherited?
Both constructors and destructors are not inherited as each class have its own constructor. Since each object of a class consists of multiple parts, a base class part and derived class part, constructors and destructors are not inherited.
14. What is the difference between typecasting and automatic type conversion? Explain with the help of an example.
Typecasting in C++ is done by the programmer in order to convert a value from one data type to another data type.
For example,
float pi = (float) 22/7;
On the other hand, automatic type conversion is automatically done by the C++ compiler as and when required.
For example,
float value = 20;
Bjarne Stroustrup is known as the creator of the C++ programming language.
Hope these C++ interview questions help you. Stay tuned for more such placement-related content!