Top C/C++ Interview Questions in 2021: Part 1

Top C/C++ Interview Questions in 2021: Part 1
Top C/C++ Interview Questions in 2021: Part 1


Dennis Ritchie developed the C language in 1972, and later in 1979, Bjarne Stroustrup developed the C++ language, an enhanced version of the C language.

C Language is one of the most easy-to-use structured programming languages. The fact that it’s machine-independent makes it much more flexible to use. Whereas the C++ Language was developed to introduce the concepts of object-oriented paradigm and an organised manner of writing programs to replicate the similarity between the natural world and the program.

The key differentiating factor of C++ from other languages is its execution speed and its Standard Template Library (STL), which is close to the hearts of many programmers around the globe. 

The whole series consists of 60 interview questions and is divided into two parts. This blog is Part 1, which covers the first 30 questions, and for the other 30, you can refer to Part 2 of the C/C++ Interview Questions and Answer series.

Basic C/C++ Interview Questions 

Q1) Is C/C++ a low-level language or a high-level language?

Answer) Strictly speaking, a low-level language is a language that has no abstraction from the hardware and can be written in the binary format and is not readable by humans. The languages which will be called low-level languages are assembly and machine-level languages, etc. 

But the ability of the C/C++ language to communicate with the hardware and write programs understood by humans and programs that are portable and machine-independent bridge the gap between a high-level language and a low-level language, making it a mid-level language.

Q2) Explain how the execution of a C/C++ program takes place?

Answer) The complete execution process of a C/C++ program consists of 4 stages:

  1. PreProcessor: It’s the first stage before compilation begins. Here, macros defined in the program are expanded, comments are removed, and includes all header file functions. Ex: test.i file is generated.
  2. Compiling: At this stage, all the syntax errors and grammatical errors are checked before generating the output file, i.e., assembly file. Ex: test.s file is generated.
  3. Assembler: The assembly code into machine code and generates an object-code file. Ex: test.o file is generated.
  4. Linker: (Usually, This stage is often said to be a loading stage, but to be precise, there is no such thing as a loader.) At this stage, all links are resolved needed by the program file and generate an executable file. 

Ex: test.exe/test.out file is generated.

blog banner 1

The program is then loaded into the memory to execute.

Q3) Explain the usage of Static variables in C/C++?

Answer) The property that differentiates static variables from normal variables is that, unlike other variables, if a variable is declared static, it retains its value even after it is out of scope. This happens because static variables are allocated memory in the data segment of the program. In C++, this concept can be extended to static class members, functions, etc.

Example of static variables using a C code

int static_demo(){
static int counter = 0;
return ++counter;
int main(){
for(int i=0;i<3;++i)
printf("%d\n", static_demo());

Q4) In C/C++ what is the difference between #include<..> and 

#include “..” ?

Answer) The difference between the two statements is that 

“..” preprocessor looks in the current source file directory for the file to be searched, whereas in the other statement, <..> preprocessor searches for the file to be included in the compiler directed directories, usually, Standard Template Library (STL) directories.

Q5) Explain Dangling pointers and memory Leaks?

Answer) Pointers pointing to freed memory locations are called Dangling Pointers.

For example: say we have a pointer pointing to a variable x containing a value that is freed later on, but the pointer is still pointing to its memory location, and hence it’s a dangling pointer.

A memory leak occurs when memory locations are not freed, and we cannot refer to the memory location.


using namespace std;
int main(){
int *ptr = (int*)malloc(sizeof(int));

Here, when we make the ‘ptr’ pointer free, it will become a dangling pointer. To avoid it, just set it to NULL.

Q6) Explain the purpose of goto statements?

Answer) Goto statements are generally used to transfer the flow of a program to a label specified in the program.


                 int f(){
goto label1;
else if(...)
goto label2;
/*do something*/
/*do something*/

If the condition is true, then it will jump to the label ‘label1’ and if the ‘else if’ condition is true, it will jump to ‘label2’ and so on.

Q7) Explain the difference between class and structure in C++?

Answer) The differences between class and structure in c++ are :

1.A class is a blueprint or prototype of an object which contains some variables and methods associated with that object.A structure is a collection of variables of different data types within a single unit.
2.The memory allocated to Class objects occurs in the heap memory.The memory allocated to Structure objects occurs in the Stack memory.                                            
3.The class is a reference type data type because of which it’s stored in heap memory.The structure is a value type data type because of which it’s stored in stack memory.
4.A class can be inherited.A structure cannot be inherited.
5.An object of a class in C/C++ is created using the “new.” Keyword.An object of a structure in C/C++ is not mandatorily Created using the “new.”Keyword.
6.It uses a large amount of memory.It uses a small amount of memory.
7.A class can be abstract.Structure does not support abstraction.
8.It can have default parametrised, copy constructors and destructors as well.It can only support a parameterised constructor.

Q8) Explain the difference between the ‘&’ and ‘*’ operators?

Answer) There is a very subtle difference between the ‘&’ and the ‘*’ operators, i.e. ‘&’ operator is used to obtain the address or reference of a variable stored in the memory whereas, the ‘*’ operator is used to obtain the value stored inside the variable.

Q9) What do you understand by late binding or dynamic binding?

Answer) The link between a function call and definition is called binding. Late Binding or dynamic binding occurs at runtime. Usually, it’s used to make calls to the same function but differs in its definition. If all information is not known to the compiler at the compile time, then dynamic binding occurs. Execution is slower in dynamic binding than static binding.

Q10) What do you think? Should the execution time of the post-increment operator be faster than the pre-increment operator or vice-versa? 

Answer) Let’s understand it using a simple code.

Before that, it’s essential to know what post-increment and pre-increment operators do.

Pre-increment operators will return the incremented value.

Example: int a = 10; then ++a = 11, and the value of a =11.

Whereas, Post-increment operators will return the incremented value and then increment the value.

Example : int a = 10;, then ++a = 10, but value of a = 11;

Now, ++a will be equivalent to :  

a = a + 1;

return a;

whereas a++ will be equivalent to :- 

int temp = a;

a = a + 1;

return temp;

Which takes more time??? The pre-increment operator wins here. Hence the pre-increment operators are faster than post-increment operators.

Medium Level C/C++ Interview Questions

Q11)  What is the difference between Union and Structure in C/C++?

Answer) The difference between Union and Structure is as follows:

1.This data type in C stores different data types at the same locationThis data type stores different types of data types at different locations 
2.Each member shares the same memory location.Each member is stored at different memory locations
3.The value assigned to a member affects the value of the other membersThe value assigned to a member doesn’t affect other member values.
4.The total memory allocated is the maximum of the memory required by each member.The total memory allocated is the sum of the memory required by each member.

Q12) You might have seen the following block of code in every C/C++ template of many IDE’s.


int main(int argc, char *argv[]){ /* some implementation .. */}

Why are the variable argc and the argv pointer to an array passed as parameters to the main function?

Answer)  To pass command-line arguments while working with command shells, we must pass the variable argc and argv pointer to array as parameters to the main function.

  • The first parameter, argc(Argument Count), denotes the number of arguments passed by users. (It also includes the name of the program).
  • The second parameter, argv(Argument Vector), denotes the pointer to the character arrays, which will store the arguments passed by the user in the command shell.

Q13 What will happen when the given code block is executed?

void beginning(){
printf(“We begin Here\n”);

Answer) If you try out the given C program and compile it, it will compile successfully. But what will happen if we try to execute it?

It won’t execute. The reason behind it is that every program needs a starting point to start its execution. By default, it’s the main function. But there is no main function in the given C program, and it does not know where to start from.

Will it never execute? The answer is No

Just tell the compiler the starting point of the program, and you are good to go.

Q14) What is a friend function?

Answer) A friend function is a function that is globally declared outside the scope of any class which can access the private, public and protected members of the class.

It requires the object of the class as an argument to access the members using the dot operator.

Example : 

using namespace std;
class A{
int x;
A(): x(0) {}
friend int print(A);
int print(A a){
return a.x;
int main(){
A a;
cout << print(a) <<”\n”;
return 0;



Q15) What is the use of the extern keyword in C/C++?

Answer) The extern keyword in C/C++ is used when we need to use any variable or a function that is present outside the source code file. The “extern” variables or functions are visible throughout the program. Their lifetime is till we reach the end of execution of a program.

Q16) Why can’t we use arithmetic operators with void pointers in C/C++?

Answer) We cannot use arithmetic operators with void pointers in C/C++ because we don’t know the size of the type of object the void pointer is pointing to.

Q17) What are the differences between Function overloading and Function overriding?

Answer) The critical differences between Function overloading and Function overriding are as follows:

S.No.Function Overloading Function Overriding 
1.When two or more functions are created with the same name but different signatures, it is known as Function Overloading.When a function signature is defined but not implemented at the time of declaration and is implemented in different classes accordingly via inheritance, it is known as Function Overriding.
2.This phenomenon occurs at compile time.This phenomenon occurs at runtime.
3.The scope of overloaded functions cannot be changed. The scope of overridden functions can be changed.
4.A function can be overloaded many times.A function can be overridden once only inside a particular class.

Q18) You might have encountered the following line in a C++ program.

“using namespace std; “

Why is the above statement written?

Answer) The above statement comprises three words, i.e., “using“, “namespace“, “std“. It means that the program will be using the namespace std, where std is an abbreviation of the word “Standard“.

A ‘Namespace’ keyword is a logical division used to organise many classes and objects and resolve name conflicts among variables, functions, etc.

And the “namespace std” means the standard namespace in the C++ Framework, globally available to all C++ programs. So all the printing objects like cout etc. all are included inside this “namespace std”.

The ‘using‘ keyword brings classes functions from the namespace to the current program’s scope.

So “using namespace std” is written so that all components inside the namespace std are in the program’s current scope.

If we do not write this statement, then we will have to access each class/function/object using scope operator(i.e. ‘::’ operator)

Example : std::cout << ”Welcome to Coding Ninjas!!” << std::endl;

Q19) Explain what a Destructor is and why is it required?

Answer) Just Like there is a constructor within a class, we have Destructors too. A Destructor is a function within a class that frees up the memory allocated to the class object when the object is being deleted. 

Q20) Is there Something known as Destructor overloading?

Answer) There is nothing known as Destructor overloading because a Destructor never takes in parameters, and there’s only a single way to delete the object.

Q21) What is the significant difference between a virtual function and a pure virtual function?

Answer) The significant difference between a virtual function and a pure virtual function is that a virtual function is a function that is initially defined and implemented but can be redefined in the derived classes accordingly. In contrast, a pure virtual function isn’t implemented initially.

Example of a Virtual function:- 

class Student{
virtual void WatchNinjaVideo(){
printf(“Hey! I am watching Coding Ninjas videos!”);
} // can be redefined further.

Example of a Pure Virtual function:- 

class Student{
virtual void WatchNinjaVideo()=0;

So you could see in the above example how pure virtual and standard virtual functions are declared in C++.

Q22) What are inline functions in C/C++? Are they useful or not?

Answer) If a function is made inline, it’s a request made to the compiler to replace the function code at all points where it’s called inside the program. This operation is carried out at compile time. An inline function may be inlined by the compiler or maybe not. 

Before inlining a function, we need to ensure that the function shouldn’t be recursive, contain loops, short, and not be marked as virtual.

They are used to enhance the execution of programs as well as reduce function calling overhead.

The disadvantage of inline functions is that it increases the program’s size, which is not desirable.

Q23) What is declared in the given C/C++ statement?

int (*fn)( int *);

Answer) The above is the declaration of a function pointer that takes an integer pointer as a parameter, and the return type will be an Integer.

Q24) What are Storage classes in C++?

Answer) In C++, a storage class is used to define the features of a variable or functions like visibility, scope, a lifetime of variables, etc. Some of the storage classes are auto, mutable, register, static, extern, etc.

Hard Level C/C++ Interview Questions 

Q25) What are copy constructors?

Answer) A copy constructor is a function within a class that initialises an object using another object of the same class. It’s called when the compiler needs a temporary object. So, an object that has the same values as the object passed as an argument needs to be created. 

The C++ compiler creates a default copy constructor if the user does not define it. The default constructor usually creates a shallow copy of the object passed as a reference. To create a deep copy user has to define its copy constructor.


class Point{
int x,y;
Point(int x1, int y1){
x = x1; 
y = y1;
Point(const Point& p){
x = p.x; 
y = p.y;
}// This is the copy constructor.

Q26) Why Can’t we pass an object by value rather than passing it by reference?

Answer) The reason behind this is simple, i.e., A copy constructor is a function that is a member of the class, and if we pass an object by value as arguments to the copy constructor, then it will have to make the copy of the object using the copy constructor again, and this will take forever to terminate. Therefore, it isn’t allowed by the compiler to pass the arguments by value in a copy constructor.

Q27) Explain what operator overloading is in C++?

Answer) Operator overloading is adding a new meaning to existing operators in the C++ library without losing its sense. It happens at compile time. We can use it to perform operations on user-defined classes and primitive data types.

Example: Overloading the ‘+’ operator to concatenate two points.

class Point{
  int x, y;
           Point(int x1, int y1){  x = x1;  y = y1; } //constructor
    Point operator + ( const Point& p){
return Point(p.x + x, p.y +y);

But certain restrictions to it say that we cannot overload few operators like a scope(::), dot(.), dereferencing(*) and conditional(?:) operators. 

Q28) What is a Virtual Destructor in C++?

Answer) A virtual destructor in C++ is introduced to resolve memory leaks while freeing up memory allocated to a derived class object pointed by a base class pointer.

Suppose we write the following 2 lines in the main function :

BaseClass *bcptr = new DerivedClass(); 

delete bcptr; 

NOTE: Assume that BaseClass and DerivedClass are both defined, and the DerivedClass inherits the BaseClass. Both the classes have their destructors too.

Example :  

using namespace std;

class BaseClass{
BaseClass(){} // default constructor
virtual ~BaseClass(){}//virtual destructor
class DerivedClass: public BaseClass{
DerivedClass(){} // default constructor
~DerivedClass(){}//virtual destructor

int main(){
BaseClass *bcptr = new DerivedClass(); 
delete bcptr; 

When we delete the bcptr pointer, the compiler will call the BaseClass destructor before the DerivedClass destructor because of the compiler’s early binding. This will lead to a memory leak.

To resolve this, we use virtual destructors so that the base class destructor is called after the derived class destructor is called.

Q29) Explain the memory allocation in C/C++? Explain different types of memory allocation? (Commonly asked C/C++ Interview Question)

Answer) Memory allocation is a process of allocating memory during the execution of a program.

There are two main types of memory allocation, i.e. static and dynamic memory allocation.

In static memory allocation, memory is allocated before the execution of a program and can’t be changed at runtime as it is fixed. A typical example is allocating memory to an array.

In dynamic memory allocation, the memory is allocated at runtime, and size is given as per requirement, and the memory gets allocated/deallocated in the heap memory.

In C/C++, it’s done using the malloc(), calloc(), free(), realloc() functions. 

Q30) What is a template in C++?

Answer) Templates are introduced in C++ for generic programming. It allows users to create generic functions which are data type independent. We can work with generic parameters inside generic functions, and we don’t need to do any function overloading for the same function. These work like macros and are expanded during compile time.


template<typename T>
T min(T a, T b){
return (a < b ? a : b);

Frequently Asked Questions

What are some programming C/C++ Interview questions?

C/C++ programming questions are based on finding outputs of given programs or finding a bug in the given program and telling how it can be fixed. There can be questions related to the basics of C/C++, which can be asked using small programs, or you might be asked to write a simple program in C/C++.

What are the basics of C/C++?

It is a subjective question to answer. But from a higher view, some important and basic concepts in C/C++ are its syntax, operators, conditional statements, loops, data structures, Standard Template Library, I/O operations, OOPS in C++, exception handling, generic programming, etc.

Key Takeaways

So, in this blog, we have covered the top 30 C/C++ Interview questions in 2021. The questions in this blog have been discussed, keeping in mind that you can learn all the theoretical concepts typically asked in C/C++ Interviews.

You can learn about the programming based and advanced level questions in part-2.

By: Aniket Verma