c/c++ Interview Question


Hand-picked the most important questions that are frequently asked in the interviews on c/c++.

category

Most asked question in the interview regarding c/c++ Interview Question

Ques. ) What is the role of a mutable storage class specifier?


Answer:

A constant class object’s member variable can be altered by declaring it using a mutable storage class specifier. Applicable only for non-static and non-constant member variables of the class.

Ques. ) Define a friend function.


Answer:

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.

Ques. ) What is an overflow error?


Answer:

It is a type of arithmetical error. It happens when the result of an arithmetical operation is greater than the actual space provided by the system.

Ques. ) What is a virtual destructor?


Answer:

A virtual destructor in C++ is used in the base class so that the derived class object can also be destroyed. A virtual destructor is declared by using the ~ tilde operator and then the virtual keyword before the constructor.

Ques. ) Explain Call by Value?


Answer:

Call by Value method passes only the values of the actual parameters to the formal parameters. So, two different copies are made. Any change made to the formal parameters will not affect the actual parameters

Ques. ) When Should I Use a Unitbuf Flag?


Answer:

The unit buffering flag ought to be turned on when we need to guarantee that each character is yield when it is embedded into a yield stream. The equivalent should be possible utilizing unbuffered yield however unit buffering gives a superior execution than the unbuffered yield.

Ques. ) What is this pointer?


Answer:

The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘This’ pointer is not available in static member functions as static member functions can be called without any object (with class name).

Ques. ) What is the use of pure virtual function?


Answer:

A pure virtual function does not have an implementation in the base class. This will be useful during scenarios where no actual implementation is required in the base class context. For example, let’s say we have a base class named Shape which is having a function “DrawShape()” as pure virtual. In the base class, there is actually no need to give a definition. Now, when a Class Square derives from Class Shape, it provides an actual definition for DrawShape() as drawing a square.

Ques. ) If you want to share some functions or variables in some files maintaining the consistency how would you part it?


Answer:

To keep up the consistency between a few records right off the bat place every definition in ‘.c’ document than utilizing outside statements place it in ‘.h’ document after it is incorporated .h record we can utilize it in a few documents utilizing #include as it will be in one of the header documents, along these lines to keep up the consistency we can make our own header document and incorporate it any place required.

Ques. ) Distinguish between shallow copy and deep copy.


Answer:

The shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copying field by field from one object to another. A deep copy is achieved using a copy constructor and or overloading assignment operator.

Ques. ) How delete [] is different from delete?


Answer:

Delete is used to release a unit of memory, delete[] is used to release an array.

Most recent interview's QNA


category-image
c/c++ Interview Question
Get QNA
category-image
Python Interview Questions
Get QNA
category-image
Machine Learning Q/A
Get QNA
More