top of page

Top C ++ Interview Questions & Answers

Updated: Oct 17, 2022

Because of the availability of a comprehensive library known as the Standard Template Library, C++ is the most recommended programming language for competitive programming. This library allows you to successfully deal with various data structures such as lists, graphs, stacks, arrays, trees, and others. C++ also aids you in solving real-time challenges in coding competitions since it provides Object-Oriented Programming approaches.

Here are the 40 most important interview questions on C-plus-plus.


Top Data Structures Interview Questions PDF

Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions with detailed explanations to ensure the best placement preparation.

Top 40 C++ Interview questions
Download PDF • 1.18MB


These top Data-Structures Interview questions have been segmented into three sections:

Basic C++ Interview Questions

Question 1. What is the difference between C and C++?

  1. C, being a procedural programming, it is a function driven language. While, C++, being an object-oriented programming, it is an object driven language.

  2. C does not support function and operator overloading. While, C++ supports both function and operator overloading.

  3. C does not support reference variables. C++ supports reference variables

  4. C provides malloc() and calloc() functions for dynamic memory allocation, and free() for memory de-allocation. C++ provides new operator for

  5. memory allocation and free operator for memory de-allocation.

  6. C programs use top-down approach. While, C++ programs use bottom-up approach.

  7. C has no support for virtual and friend functions. C++ supports virtual and friend functions.


Question 2. Write some applications of C++

  1. Internet browser Firefox is written in C++ programming language.

  2. Some of the Google app. such as Google file system, Google Chromium are developed using C++.

  3. C++ is used for design database like MySQL.

  4. To Develop Graphical application like computer and mobile games.

  5. To evaluate any kind of mathematical equation one can use C++ language.

  6. C++ Language are also used to design OS. Like window xp.

  7. Few parts of apple OS X are written in C++ programming language.


Question 3. What do you mean by tokens in C++? Give examples

Tokens are the basic buildings blocks in language which are constructed together to write a program. Each and every smallest individual units in a program are known as tokens.

C++ tokens are of six types:

1. Keywords (int, while)

2. Identifiers (main, total),

3. Constants (10, 20),

4. Strings (“Rootworkz”, “hello”),

5. Special symbols ( (), {}),

6. Operators (+, /,-,*)


Question 4. Write and explain structure of a C++ program.

General format of a C++ program:

Preprocessor commands
Globe declaration.
void main()

Preprocessor Commands:

These are the instructions which we want to give to our compiler (Its preprocessor). They indicate how the program is to be compiled. A preprocessor commands s tarts with the symbol #. Example #include, #define, #ifdef etc.

Global declaration:

In this section we can define variables, functions, Structures, classes etc. Variables define in this section are accessible anywhere within the program.

main() Function:

This is the entry point of our program. Execution of the program being with function main.


Question 5. What is inline function? Explain with an example.

Inline functions are the functions which are not actually called, rather their code is expanded in line at the point of each invocation.

> The process is similar to using a function-like macro.

> The definition of the function should be preceded with the inline‘ keyword in order for it to be an inline function.

> Inline functions are important because they allow us to create very efficient code.

> Expanding function calls in line can produce faster run times.

> The significant amount of overhead which is generated by the calling and return mechanism can be reduced.


Using namespace std;
Inline int max( int a, int b)
return a>b ? a : b ;
int main()
cout << max (10,20);
cout << max ( 99, 98 );
return 0;

Question 6. Explain continue statement with an example.

The continue statement forces the next iteration of the loop to take place, skipping any code in between.

>For the for loop, continue causes the conditional test and increment portions of the loop to execute.

>For the while and do-while loops, program control passes to the conditional tests.

The following program shows the use of continue statement:

void code ( void )
char done, ch;
done = 0;
while (! done)
ch = getchar ( );
if ( ch = = ‗$‘) {
done = 1;
putchar ( ch + 1);

The break statement has two uses:

>We can use it to terminate a case in the switch statement.

>We can use it to force immediate termination of a loop, by passing the normal conditional test.

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop


# include <iostream.h>
void main()
{ int i=0;
if (i==5)

Question 7. Define #define and const.

The #define directive is a preprocessor directive. The preprocessor replaces those macros by their body before the compiler even sees it. Think of it as an automatic search and replace of your source code.

Example: #define PI 3.14

const :

A const variable declaration declares an actual variable in the language, which you can use like a real variable: It take its address, pass it around, use cast it, convert it, etc.

Exampe: const float PI=3.14;

Difference B/W const and #define

1.const means the data can't be changed while define has no such meaning.

2.const is used by compiler and define is performed by preprocessor.

3.const has scope while define can be considerd global.

4.const variable can be seen from debugger while macro can be seen.


Question 8. What do you mean by enumerated constants or enums?

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.

The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally a closing brace and a semicolon.

For Example:


This statement performs two tasks:

1. It makes COLOR the name of an enumeration, that is, a new type.

2. It makes RED a symbolic constant with the value 0, BLUE a symbolic constant with the value 1, GREEN a symbolic constant with the value 2, and so forth.


Question 9. Explain switch statement with an example.

The switch statement is used to check variable for different values and based on the variable different cases are executed.


case value :
case value :
case value :
default :

>If no matching case is found then code is the default block is executed.

>Default block is optional.

>We can use a break statement to throw program control out of a switch otherwise next cases will be executed without checking their values.


Question 10.Differentiate between if and switch statement.

1. switch is usually more compact than lots of nested if else and therefore, more readable

2. If you omit the break between two switch cases, you can fall through to the next case .No such problems with if statement

3. switch case values can only be constants.i.e. we can compare only with constant values. In if statement we can compare any type of values.

4. switch only accepts only int ,char data types and float or double.

5. switch uses only equality operator for comparision.


Question 11. Explain arrays in C++. Write a program to read an array to 10 integer and find sum of all the elements.

An array is a group of elements of same type sharing same name and stored in a consecutive memory location. Arrays are define if we want to store multiple values of same type.


Datatype arrayname[size];
# include <iostream.h>
void main()
int a[10] , s=0 , i;
cout << “Enter 10 no.’s”;
for( i = 0; i<=9 ; i++)
cin >> a[i];
for( i = 0; i<=9 ; i++)
cout << “Sum is ” << s;

Question 12. What are string functions in C++? Give examples.

C++ provide us different library functions to perform operations on strings. These functions are declared in the header file cstring.


1. int strlen (char *str) => returns length of the string.

2. strupr (char *str)=> convert the string into uppercase.

3. strlwr (char *str)=> convert the string into lowercase.

4. strrev (char *str)=> will reverse the contents of the string.

5. strcpy (char *dest, char *source)=> will copy source string at specified destination.

6. strcat (char *s1, char *s2)=> It joins two strings.

7. int strcmp (char *s1, char *s2)=> will compare two strings and return difference between their ASCII values.


Question 13. Explain C++ structure.

structure is user defined data type that allows to combine data items of different kinds.

To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:

struct [structure tag]
member definition;
member definition;
} [one or more structure variables];

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;

To access any member of a structure, we use the member access operator (.)

Syntax: VariableName.MemberName


Question 14. What is anyonamoyus structures in c++?

If a structure variable is defined while defining a structure then there is no need to name the structure. Such a structure is called as anyonamous structure.


{ int rollno;
char name[20];
{ cout<<"Enter the rollno,name=>";
cout<<"Rollno =”<< a.rollno<<” Name =”<<;

Question 15. What are classes and objects in C++?

CLASS: A class is a user defined datatype where we can bind its data and its related functions together.

A class is defined in terms of its data members and member function.

Data members indicate informations about objects or current state of objects. While, member function indicates operationwhich we can perform on an object.


class name
Data Members
Member Function

OBJECTS: A class provides the blueprints for objects, so basically an object is created from a class i.e. An object is an instance of class.


Intermediate C++ Interview Questions

Question 16. Define all OOPs concepts in C++.(Abstraction, Encapsuation, Inheritence, Polymorphism)


Abstraction is the concept of taking some object from the real world, and converting it to programming terms. Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object.


Encapsulation in C++ is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as as single unit. In encapsulation the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class, therefore it is also known as data hiding.


Inheritance is the ability, offered by many OOP languages, to derive a new class (the derived or inherited class) from another class (the base class). The derived class automatically inherits the properties and methods of the base class.


Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.


Question 17. What do you mean by static variables in C++? Write example program.

A static variable tells the compiler to persist the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static is initialized only once and remains into existence till the end of program.

void test();
void test()
static int a = 0; //Static variable
a = a+1;
Output : 1 2 3

Question 18. Explain Storage classes in C++.

In C++ language, each variable has a storage class which decides scope, visibility and lifetime of that variable. The following storage classes

are most oftenly used in C++ programming:

1. Automatic variables: A variable declared inside a function without any storage class specification, is by default an automatic variable. They are created when a function is called and are destroyed automatically when the function exits.


void main()
int Arjun; or auto int Arjun;

2. External variables: A variable that is declared outside any function is a Global variable. Global variables remain available throughout the entire program.


int number; // global variable
void main()

3. Static variables: A static variable tells the compiler to persist the variable until the end of program. Instead of creating and destroying a variable every time when it comes into and goes out of scope, static is initialized only once and remains into existence till the end of program.

4. Register variables: Register variable inform the compiler to store the variable in register instead of memory.


Question 19. Explain functon overloading.

Defining multiple functions in a program having same name is called as function overloading. Only precaution to be taken is that function must have different no of or type of argument.

Compiler will decide which to call depending on no. of or type of arguments passed while calling the function.


# include<iostream.h>
int volume(int L, int B, int H)
int v=L*B*H;
return v;
int volume(int N)
int v=N*N*N;
return v;
void main()
int a=volume(5,7,8);
cout<<”Volume of box is “<<a;
int b=volume(5);
cout<<”Volume of cube is “<<b;

Question 20. What do you mean by typedef in C++.

typedef is a keyword used to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated.


typedef existing_name alias_name

Question 21. Differentiate between structure and classes.

1) In structure have a by default public. In class have a by default private.

2) There is no data hiding features comes with structures. Classes do, private, protected and public.

3) A structure can't be abstract, a class can.

4) Structure are value type, They are stored as a stack on memory. where as class are reference type. They are stored as heap on memory.


Question 22. What are the advantages and disadvantages of passing values by reference in a function?

Advantages of passing by reference:

> It allows a function to change the value of the argument, which is sometimes useful.

> Because a copy of the argument is not made, it is fast, even when used with large structs or classes.

> References can be used to return multiple values from a function.

> References must be initialized, so there’s no worry about null values


Disadvantages of passing by reference:

> Because a non-const reference cannot be made to an rvalue (e.g. a literal or an expression), reference arguments must be normal variables.

> It can be hard to tell whether a parameter passed by nonconst reference is meant to be input, output, or both.

> It’s impossible to tell from the function call whether the argument may change. An argument passed by value and passed by reference looks the same.


Question 23. List and explain access specifiers in C++.

Access specifiers indicate accessibility of member of class. It can be private , public or protected.


Private keyword, means that no one can access the class members declared private outside that class. If someone tries to access the private member, they will get a compile time error. By default class variables and member functions are private.


Public, means all the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. Hence there are chances that they might change them. So the key members must not be declared public.


Protected, is the last access specifier, and it is similar to private, it makes class member inaccessible outside the class. But they can be accessed by any subclass of that class.


Question 24. What are the conditions where inline functions cannot be extended?

The conditions where inline functions cannot be expanded are:

1) For functions returning values, if a loop, a switch or goto exists.

2) For functions not returning values, if a return statement exists.

3) If functions contain static variables.

4) If inline functions are recursive.


Question 25. Mention the restrictions that are placed on static member


The restrictions that are placed on static member functions are:

i) They may only directly refer to other static members of the class.

ii) A static member function does not have a this pointer.

iii) There cannot be a static and a non-static version of the same function.

iv) A static member function may not be virtual.

v) They cannot be declared as const or volatile.


Advanced C++ Interview Questions

Question 26. What are friend functions? What are the advantages of using friend functions? Write a C++ program using a friend function

Friend function: It is a function by which it is possible to grant a nonmember function access to the private members of a class by using it. A friend function has access to all private and protected members of the class for which it is a friend.

The advantages of using friend functions are:

a) Friend functions can be useful when we are overloading certain types of operators.

b) Friend functions make the creation of some types of I/O functions easier.

c) Friend functions may be desirable in cases as two or more classes may contain members that are interrelated relative to other parts of the program.

Program to find sum of two numbers using friend function:

using namespace std;
class A {
int x, y;
void set_xy ( int i, int j );
friend int add ( A ob);
add ( );
void A : : set_xy ( int i, int j)
x = i;
y =j;
int add ( A ob)
return ob.x + ob.y;
void main ( )
A ob1;
ob1.set_xy (10, 20);
cout << add ( ob1);

Question 27. What are exceptions in C++ and how do you handle it ?

An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw.

> try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

> throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

> catch: A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.


{ int r;
float a,c;
cout<<”Enter radius in range (1-100) “;
throw 1001;
throw 1002;
cout<<”Area is “<<a<<endl;
cout<<”Circumference is “<<c<<endl;
catch(int err)
cout<<”Radius cannot be –ve”;
cout<<”Radius must be in range 1-100”;

Question 28. What is sope resolution operator in C++ and when do we use it?

In C++, scope resolution operator is :: It is used for following purposes.

1. To access a global variable when there is a local variable with same name

2. To define a function outside a class.

3. To access a class’s static variables.

4. In case of multiple Inheritance.


Question 29. What do you mean by virtual functions in C++?

A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class. To create virtual function, precede the function’s declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.


class shape()
virtual void area()

class circle:public shape
int r;
circle(int n)
{ r=n;
void area()
{float a=3.14*r*r;
cout<<”area is”<<a;

{ shape *p;
p=new circle(5);

Question 30. Explain "this" pointer in C++.

> Every object has a special pointer "this" which points to the object itself.

> This pointer is accessible to all members of the class but not to any static members of the class.

> Can be used to find the address of the object in which the function is a member.

> Friend functions do not have a this pointer, because friends are not members of a class.


class student
{ int rollno;
char name[20];
student(int rollno,char name[])
{ this->rollno=rollno;
void showInfo()
{ cout<<”Roll No is “<<rollno<<endl;
cout<<”Name is “<<name<<endl;
student* getAddress() // function returning Address of object
{ return this;
student getObject()// function returning object
{ return *this;
void main()
{student a(4117,”Amit”);
student *p=a.getAddress();
student b=a.getObject();

Question 31. Differentiate between run time polymorphism and compile time polymorphism.

1. In Compile time Polymorphism, call is resolved by the compiler. In Run time Polymorphism, call is not resolved by the compiler .

2. Overloading is compile time polymorphism where more than one methods share the same name with different parameters or signature and different return type. Overriding is run time polymorphism having same method with same parameters or signature, but associated in a class & its subclass.

3. Overloading is achieved by function overloading and operator overloading. While, overriding is achieved by virtual functions and pointers.

4. Compile time polymorphism is less flexible as all things execute at compile time.Run time polymorphism is more flexible as all things execute at run time.


Question 32. What are constructors. Write characterists of constructors.

Constructors Are special member functions of a class which gets automatically invoked when ever an object of its class is created.


> They have the same name of the class name.

> They doesn’t have any return type not even void.

> They are used for initialization of the objects.

> Multiple constructors can be defined in a class

> Constructors can have default arguments.

> C++ automatically adds an empty constructor without arguments if constructor is not defined

> Constructors are implicitly called when objects are created but they can also be explicitly called if required.

There are three types of constructors:

1.Default constructor: while declaring constructor we can assign some default value to its that same constructor will be used to initialize different ways of object.

2. Copy Costructor: It is used to initialize an object by using data of another object of same type. The copy constructor is automatically called when an object is assigned to it.

3. Parameterised constructor: while creating an object we can pass some data. This data is passed on to constructor as an argument where it is used for initialization of object.


Question 33. Differentiate between constructors and member functions.

1. Constructor name must be same as class name but functions cannot have same name as class name.

2. Constructor does not have return type whereas functions must have.

3. Member function can be virtual, but, there is no concept of virtual constructor in C++.

5. Constructors are invoked at the time of object creation automatically and can be called explicitly but functions are called explicitly using class objects.


Question 34. What are destructors in C++?

A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.



Question 35. What is the output of the following program?


using namespace std;
void RootWorkz() {
static int i;

cout<<i<<" ";

main() {

The output of the following program will be:

1 2 3

This is because a static local variable retains its value between the function calls and the default value is 0.


Question 36. What will be the output of the following program?


using namespace std;
void swap(int P, int Q) {
int x = P;

P = Q;
Q = x;
main() {
int x = 5, y = 3;

cout<<x<<" "<<y;

The output of the following program will be:

5 3

The values of x and y will not be swapped because call by value mechanism can’t alter actual arguments.


Question 37. What will be the output of the following program?

using namespace std;
int a = 1;
void fun()
int a = 2;
int a = 3;
cout << ::a << endl;
int main()
return 0;

The output of the program will be: 1 This is because the value of ::a is 1. The scope resolution operator when used with a variable name, always refers to global variable. The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them.


Question 38. Predict the output.

using namespace std;

int X[100];
int main()
cout << X[50] << endl;

The output will be 0. Because in C++ all the uninitialized global variables are initialized to 0.


Question 39. Predict the output-

using namespace std;

union HELLO {
int a;
unsigned int b;
HELLO() { a = 10; }
unsigned int get() {return b;}

int main()
HELLO obj;
cout << obj.getb();
return 0;

Output of the program will be 10. We know that like structures, unions can also have methods. Members of union are public by default. Since data members of union share memory, the value of b becomes same as the value of a. Union: A union is a special data type available in C that allows storing different data types in the same memory location.



These C++ questions would give you an insight into what kind of questions could be asked.
Free Practice Tests
Most Important Interview Questions
Last Minute Class Notes


99 views0 comments


bottom of page