C++ Programmer Interview Questions And Answers

Download C++ Programmer Interview Questions and Answers PDF

Strengthen your C++ Programmer interview skills with our collection of 59 important questions. Each question is designed to test and expand your C++ Programmer expertise. Suitable for all experience levels, these questions will help you prepare thoroughly. Secure the free PDF to access all 59 questions and guarantee your preparation for your C++ Programmer interview. This guide is crucial for enhancing your readiness and self-assurance.

59 C++ Programmer Questions and Answers:

C++ Programmer Job Interview Questions Table of Contents:

C++ Programmer Job Interview Questions and Answers
C++ Programmer Job Interview 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.
Read More

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.
Read More

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;
};
Read More

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;
}
Read More

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.
Read More

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.
Read More

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.
Read More

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.
Read More

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.
Read More

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.
Read More

11 :: 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.
Read More

12 :: 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.
Read More

13 :: 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.
Read More

14 :: Do you know who designed C++ programming language?

Bjarne Stroustrup.
Read More

15 :: Tell us can we use malloc() function of C language to allocate dynamic memory in C++?

Yes, as C is the subset of C++, we can all the functions of C in C++ too.
Read More

16 :: Do you know what is the role of mutable storage class specifier?

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

17 :: Tell me how can a C function be called in a C++ program?

Using an extern "C" declaration:


//C code
void func(int i)
{
//code
}

void print(int i)
{
//code
}
//C++ code
extern "C"{
void func(int i);
void print(int i);
}

void myfunc(int i)
{
func(i);
print(i);
}
Read More

18 :: 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)
{
//...
}
Read More

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.
Read More

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
Read More

21 :: Tell us why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?

Pure virtual functions are used when it doesn't make sense to provide definition of a virtual function in the base class or a proper definition does not exists in the context of base class. Consider the above example, class SymmetricShape is used as base class for shapes with symmetric structure(Circle, square, equilateral triangle etc). In this case, there exists no proper definition for function draw() in the base class SymmetricShape instead the child classes of SymmetricShape (Cirlce, Square etc) can implement this method and draw proper shape.
Read More

22 :: Explain what is 'Copy Constructor' and when it is called?

This is a frequent c++ interview question. Copy constructor is a special constructor of a class which is used to create copy of an object. Compiler will give a default copy constructor if you don't define one. This implicit constructor will copy all the members of source object to target object.
Implicit copy constructors are not recommended, because if the source object contains pointers they will be copied to target object, and it may cause heap corruption when both the objects with pointers referring to the same location does an update to the memory location. In this case its better to define a custom copy constructor and do a deep copy of the object.
class SampleClass{
public:
int* ptr;
SampleClass();
// Copy constructor declaration
SampleClass(SampleClass &obj);
};

SampleClass::SampleClass(){
ptr = new int();
*ptr = 5;
}

// Copy constructor definition
SampleClass::SampleClass(SampleClass &obj){
//create a new object for the pointer
ptr = new int();
// Now manually assign the value
*ptr = *(obj.ptr);
cout<<"Copy constructor...n";
}
Read More

23 :: Tell me what are C++ inline functions?

C++ inline functions are special functions, for which the compiler replaces the function call with body/definition of function. Inline functions makes the program execute faster than the normal functions, since the overhead involved in saving current state to stack on the function call is avoided. By giving developer the control of making a function as inline, he can further optimize the code based on application logic. But actually, it's the compiler that decides whether to make a function inline or not regardless of it's declaration. Compiler may choose to make a non inline function inline and vice versa. Declaring a function as inline is in effect a request to the compiler to make it inline, which compiler may ignore. So, please note this point for the interview that, it is upto the compiler to make a function inline or not.
inline int min(int a, int b)
{
return (a < b)? a : b;
}

int main( )
{
cout << "min (20,10): " << min(20,10) << endl;
cout << "min (0,200): " << min(0,200) << endl;
cout << "min (100,1010): " << min(100,1010) << endl;
return 0;
}
If the complier decides to make the function min as inline, then the above code will internally look as if it was written like
int main( )
{
cout << "min (20,10): " << ((20 < 10)? 20 : 10) << endl;
cout << "min (0,200): " << ((0 < 200)? 0 : 200) << endl;
cout << "min (100,1010): " << ((100 < 1010)? 100 : 1010) << endl;
return 0;
}
Read More

24 :: Tell me what is a class?

Class defines a datatype, it's type definition of category of thing(s). But a class actually does not define the data, it just specifies the structure of data. To use them you need to create objects out of the class. Class can be considered as a blueprint of a building, you can not stay inside blueprint of building, you need to construct building(s) out of that plan. You can create any number of buildings from the blueprint, similarly you can create any number of objects from a class.
class Vehicle
{
public:
int numberOfTyres;
double engineCapacity;
void drive(){
// code to drive the car
}
};
Read More

25 :: Explain me which operator can be used in C++ to allocate dynamic memory?

‘new’ is the operator can be used for the same.
Read More