C++ Programmer

  Home  C++ Programming  C++ Programmer


“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 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.

 200 views

2⟩ 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.

 217 views

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;

};

 202 views

6⟩ Tell me is it possible to get the source code back from binary file?

Technically it is possible to generate the source code from binary. It is called reverse engineering. There are lot of reverse engineering tools available. But, in actual case most of them will not re generate the exact source code back because many information will be lost due to compiler optimization and other interpretations.

 189 views

7⟩ Explain what do you mean by storage classes?

Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.

 184 views

8⟩ 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.

 200 views

9⟩ 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.

 208 views

10⟩ 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.

 206 views

11⟩ 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.

 186 views

12⟩ 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.

 193 views

14⟩ Explain is it possible to have a recursive inline function?

Although you can call an inline function from within itself, the compiler may not generate inline code since the compiler cannot determine the depth of recursion at compile time. A compiler with a good optimizer can inline recursive calls till some depth fixed at compile-time (say three or five recursive calls), and insert non-recursive calls at compile time for cases when the actual depth gets exceeded at run time.

 201 views

17⟩ Tell us how to make sure a C++ function can be called as e.g. void foo(int, int) but not as any other type like void foo(long, long)?

Implement foo(int, int)…

void foo(int a, int b) {

// whatever

}

…and delete all others through a template:

template <typename T1, typename T2> void foo(T1 a, T2 b) = delete;

Or without the delete keyword:

template <class T, class U>

void f(T arg1, U arg2);

template <>

void f(int arg1, int arg2)

{

//...

}

 164 views

19⟩ Explain me what will i and j equal after the code below is executed? Explain your answer. int i = 5; int j = i++;?

After the above code executes, i will equal 6, but j will equal 5.

Understanding the reason for this is fundamental to understanding how the unary increment (++) and decrement (--) operators work in C++.

When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i;, i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6.

However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i (i.e., 5) and then i is incremented to 6.

 202 views

20⟩ Explain how to create a reference variable in C++?

Appending an ampersand (&) to the end of datatype makes a variable eligible to use as reference variable.

int a = 20;

int& b = a;

The first statement initializes a an integer variable a. Second statement creates an integer reference initialized to variable a

Take a look at the below example to see how reference variables work.

int main ()

{

int a;

int& b = a;

a = 10;

cout << "Value of a : " << a << endl;

cout << "Value of a reference (b) : " << b << endl;

b = 20;

cout << "Value of a : " << a << endl;

cout << "Value of a reference (b) : " << b << endl;

return 0;

}

Above code creates following output.

Value of a : 10

Value of a reference (b) : 10

Value of a : 20

Value of a reference (b) : 20

 206 views