Doubts About C++ Interview


#1

Hello All, I am confused about which type of questions is putting in C++ interview. Is anyone here C++ expert which helps me to suggest some tricks and tips. I am fresher and I have good programming skills. My interview has been scheduled for next week. So I have some days to prepare myself.


#2

Hi Adi,

I am not an expert but having programming background, Why i don’t want to say myself as an expert because I am learning right now as well, The question I faced while attending programming interview are:

Question: Briefly explain the concept of Inheritance in C++.
Answer: C++ allows classes to inherit some of the commonly used state and behavior from other classes. This process is known as inheritance.

Question: Can we have a recursive inline function in C++?
Answer: Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won’t be able to determine the depth of the recursion at the compile time.

Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth fixed at compile time, and insert non-recursive calls at compile time for the cases when the actual depth exceed at run time.

Question: Define an Inline Function in C++? Write its syntax. Is it possible for the C++ compiler to ignore inlining?
Answer: In order to reduce the function call overhead, C++ offers inline functions. As the name suggests, an inline function is one that is expanded in line when it is called.

As soon as the inline function is called, the whole code of the same gets either inserted or substituted at the particular point of the inline function call. The substitution is complete by the C++ compiler at compile time. Small inline functions might increase program efficiency.

The syntax of a typical inline function is:

Inline return-type function-name(parameters)

{

// Function code goes here

}

As the inlining is a request, not a command, the compiler can ignore it.

Question: Explain ‘this’ pointer?
Answer: The ‘this’ pointer is a constant pointer and it holds the memory address of the current object. It passes as a hidden argument to all the nonstatic member function calls. Also, it is available as a local variable within the body of all the nonstatic functions.

As static member functions can be called even without any object, i.e. with the class name, the ‘this’ pointer is not available for them.

Question: Why do we need the Friend class and function?
Answer: Sometimes, there is a need for allowing a particular class to access private or protected members of a class. The solution is a friend class, which is capable of accessing the protected as well as the private members of the class in which it is declared as a friend.

Similarly to the friend class, a friend function is able to access private and protected class members. A friend function can either be a global function or a method of some class.

Some important points about friend class and friend function:

Friendship is not inherited
Friendship isn’t mutual i.e. if some class called Friend is a friend of some other class called NotAFriend then it doesn’t automatically become a friend of the Friend class
The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming

Question: Explain the significance of vTable and vptr in C++ and how the compiler deals with them
Answer: vTable is a table containing function pointers. Every class has a vTable. vptr is a pointer to vTable. Each object has a vptr. In order to maintain and use vptr and vTable, the C++ compiler adds additional code at two places:

In every constructor – This code sets vptr:
    Of the object being created
    To point to vTable of the class
Code with the polymorphic functional call – At every location where a polymorphic call is made, the compiler inserts code in order to first look for vptr using the base class pointer or reference. The vTable of a derived class can be accessed once the vptr is successfully fetched. Address of derived class function show() is accessed and called using the vTable.

Question: How is function overloading different from operator overloading?
Answer: Function overloading allows two or more functions with different type and number of parameters to have the same name. Operator overloading, on the other hand, allows for redefining the way an operator works for user-defined types.

Question: Is it possible for a C++ program to be compiled without the main() function?
Answer: Yes, it is possible. However, as the main() function is essential for the execution of the program, the program will stop after compiling and will not execute.

To know more question and answer visit here with C++ Interview Questions Post.