C++ Programmer Interview Preparation Guide
Download PDF

C++ Programmer related Frequently Asked Questions by expert members with professional career as C++ Programmer. These list of interview questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts

59 C++ Programmer Questions and Answers:

1 :: Explain me what is an Object/Instance?

Object is the instance of a class, which is concrete. From the above example, we can create instance of class Vehicle as given below
Vehicle vehicleObject;
We can have different objects of the class Vehicle, for example we can have Vehicle objects with 2 tyres, 4tyres etc. Similarly different engine capacities as well.

2 :: Explain void free (void* ptr)?

This function is used to deallocate a block of memory that was allocated using malloc(), calloc() or realloc(). If ptr is null, this function does not doe anything.

3 :: Tell me how to create a pure virtual function?

A function is made as pure virtual function by the using a specific signature, " = 0" appended to the function declaration as given below,
class SymmetricShape {
public:
// draw() is a pure virtual function.
virtual void draw() = 0;
};

4 :: Tell me what will the line of code below print out and why?

#include <iostream>

int main(int argc, char **argv)
{
std::cout << 25u - 50;
return 0;
}

5 :: Please explain is there a difference between class and struct?

The only difference between a class and struct are the access modifiers. Struct members are public by default; class members are private. It is good practice to use classes when you need an object that has methods and structs when you have a simple data object.

6 :: Explain what are VTABLE and VPTR?

vtable is a table of function pointers. It is maintained per class.
vptr is a pointer to vtable. It is maintained per object (See this for an example).
Compiler adds additional code at two places to maintain and use vtable and vptr.
1) Code in every constructor. This code sets vptr of the object being created. This code sets vptr to point to vtable of the class.
2) Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (In the above example, since pointed or referred object is of derived type, vptr of derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived derived class function show() is accessed and called.

7 :: Explain what is inheritance?

Inheritance is the process of acquiring the properties of the exiting class into the new class. The existing class is called as base/parent class and the inherited class is called as derived/child class.

8 :: Tell me does an abstract class in C++ need to hold all pure virtual functions?

Not necessarily, a class having at least one pure virtual function is abstract class too.

9 :: Tell me what is difference between C and C++?

☛ C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object oriented) while C follows procedural style programming.
☛ In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
☛ C follows top-down approach ( solution is created in step by step manner, like each step is processed into details as we proceed ) but C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
☛ C++ supports function overloading while C does not support it.
☛ C++ allows use of functions in structures, but C does not permit that.
☛ C++ supports reference variables ( two variables can point to same memory location ). C does not support this.
☛ C does not have a built in exception handling framework, though we can emulate it with other mechanism. C++ directly supports exception handling, which makes life of developer easy.

10 :: Tell us what is the use of volatile keyword in c++? Give an example?

Most of the times compilers will do optimization to the code to speed up the program. For example in the below code,
int a = 10;
while( a == 10){
// Do something
}
compiler may think that value of 'a' is not getting changed from the program and replace it with 'while(true)', which will result in an infinite loop. In actual scenario the value of 'a' may be getting updated from outside of the program.
Volatile keyword is used to tell compiler that the variable declared using volatile may be used from outside the current scope so that compiler wont apply any optimization. This matters only in case of multi-threaded applications.
In the above example if variable 'a' was declared using volatile, compiler will not optimize it. In shot, value of the volatile variables will be read from the memory location directly.