CPP Interview Questions And Answers

[150+] CPP Interview Questions And Answers

CPP Interview Questions And Answers:

Frequently asked CPP Interview Questions And Answers are as follows-

  1. What is the full form of OOPS?

Object-Oriented Programming System.

  1. What is a class?

Class is a blue print which reflects the entities attributes and actions. Technically defining a class is designing an user defined data type.

  1. What is an object?

An instance of the class is called as object.

  1. List the types of inheritance supported in C++.

Single, Multilevel, Multiple, Hierarchical and Hybrid.

  1. What is the role of protected access specifier?

If a class member is protected then it is accessible in the inherited class. However, outside the both the private and protected members are not accessible.

  1. What is encapsulation?

The process of binding the data and the functions acting on the data together in an entity (class) called as encapsulation.

  1. What is abstraction?

Abstraction refers to hiding the internal implementation and exhibiting only the necessary details.

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

  1. Explain the purpose of the keyword volatile.

Declaring a variable volatile directs the compiler that the variable can be changed externally. Hence avoiding compiler optimization on the variable reference.

  1. What is an inline function?

A function prefixed with the keyword inline before the function definition is called as inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros.

  1. What is a storage class?

Storage class specifies the life or scope of symbols such as variable or functions.

  1. Mention the storage classes names in C++.

The following are storage classes supported in C++

auto, static, extern, register and mutable

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

  1. Distinguish between shallow copy and deep copy.

Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator.

  1. What is a pure virtual function?

A virtual function with no function body and assigned with a value zero is called as pure virtual function.

  1. What is an abstract class in C++?

A class with at least one pure virtual function is called as abstract class. We cannot instantiate an abstract class.

  1. What is a reference variable in C++?

A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.

  1. What is role of static keyword on class member variable?

A static variable does exit though the objects for the respective class are not created. Static member variable shares a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.

  1. Explain the static member function.

A static member function can be invoked using the class name as it exits before class objects comes into existence. It can access only static members of the class.

  1. Name the data type which can be used to store wide characters in C++.


  1. What are/is the operator/operators used to access the class members?

Dot (.) and Arrow ( -> )

  1. Can we initialize a class/structure member variable as soon as the same is defined?

No, Defining a class/structure is just a type definition and will not allocated memory for the same.

  1. What is the data type to store the Boolean value?

bool, is the new primitive data type introduced in C++ language.

  1. What is function overloading?

Defining several functions with the same name with unique list of parameters is called as function overloading.

  1. What is operator overloading?

Defining a new job for the existing operator w.r.t the class objects is called as operator overloading.

  1. Do we have a String primitive data type in C++?

No, it’s a class from STL (Standard template library).

  1. Name the default standard streams in C++.

cin, cout, cerr and clog.

  1. Which access specifier/s can help to achive data hiding in C++?

Private & Protected.

  1. When a class member is defined outside the class, which operator can be used to associate the function definition to a particular class?

Scope resolution operator (::)

  1. What is a destructor? Can it be overloaded?

A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.

  1. What is a constructor?

A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.

  1. What is a default constructor? Can we provide one for our class?

Every class does have a constructor provided by the compiler if the programmer doesn’t provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler doesn’t provides the constructor.

  1. Which operator can be used in C++ to allocate dynamic memory?

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

  1. What is the purpose of ‘delete’ operator?

‘delete’ operator is used to release the dynamic memory which was created using ‘new’ operator.

  1. Can I 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.

  1. Can I use ‘delete’ operator to release the memory which was allocated using malloc() function of C language?

No, we need to use free() of C language for the same.

  1. What is a friend function?

A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword ‘friend’.

  1. What is a copy constructor?

A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.

  1. Does C++ supports exception handling? If so what are the keywords involved in achieving the same.

C++ does supports exception handling. try, catch & throw are keyword used for the same.

  1. Explain the pointer – this.

This is the pointer variable of the compiler which always holds the current active object’s address.

  1. What is the difference between the keywords struct and class in C++?

By default the members of struct are public and by default the members of the class are private.

  1. Can we implement all the concepts of OOPS using the keyword struct?


  1. What is the block scope variable in C++?

A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block.

  1. What is the role of the file opening mode ios::trunk?

If the file already exists, its content will be truncated before opening the file.

  1. What is the scope resolution operator?

The scope resolution operator is used to –

Resolve the scope of global variables.

To associate function definition to a class if the function is defined outside the class.

  1. What is a namespace?

A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under different name space.

  1. What are command line arguments?

The arguments/parameters which are sent to the main() function while executing from the command line/console are called so. All the arguments sent are the strings only.

  1. What is a class template?

A template class is a generic class. The keyword template can be used to define a class template.

  1. How can we catch all kind of exceptions in a single catch block?

The catch block with ellipses as follows




  1. What is keyword auto for?

By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables.

void f()


int i;


auto int j;


NOTE − A global variable can’t be an automatic variable.

  1. What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice.

void f()


static int i;



printf(“%d “,i);


If a global variable is static then its visibility is limited to the same source code.

  1. What is the purpose of extern storage specifier.

Used to resolve the scope of global symbol

#include <iostream>

using namespace std;

main() {

extern int i;



int i = 20;

  1. What is the meaning of base address of the array?

The starting address of the array is called as the base address of the array.

  1. When should we use the register storage specifier?

If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.

  1. Can a program be compiled without main() function?

Yes, it can be but cannot be executed, as the execution requires main() function definition.

  1. Where an automatic variable is stored?

Every local variable by default being an auto variable is stored in stack memory

  1. What is a container class?

A class containing at least one member variable of another class type in it is called so.

  1. What is a token?

A C++ program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

  1. What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

  1. What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system.


main( int count, char *args[]) {


  1. What are the different ways of passing parameters to the functions? Which to use when?

Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.

Call by address − We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.

Call by reference − The actual parameters are received with the C++ new reference variables as formal parameters. We choose this if we do want the actual parameters to be modified with formal parameters.

  1. What is reminder for 5.0 % 2?

Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

  1. Which compiler switch to be used for compiling the programs using math library with g++ compiler?

Opiton –lm to be used as > g++ –lm <file.cpp>

  1. Can we resize the allocated memory which was allocated using ‘new’ operator?

No, there is no such provision available.

  1. Who designed C++ programming language?

Bjarne Stroustrup.

  1. Which operator can be used to determine the size of a data type/class or variable/object?


  1. How can we refer to the global variable if the local and the global variable names are same?

We can apply scope resolution operator (::) to the for the scope of global variable.

  1. What are valid operations on pointers?

The only two permitted operations on pointers are

i)Comparision ii) Addition/Substraction (excluding void pointers)

  1. What is recursion?

Function calling itself is called as recursion.

  1. What is the first string in the argument vector w.r.t command line arguments?

Program name.

  1. What is the maximum length of an identifier?

Ideally it is 32 characters and also implementation dependent.

  1. What is the default function call method?

By default the functions are called by value.

  1. What are available mode of inheritance to inherit one class from another?

Public, private & protected

  1. What is the difference between delete and delete[]?

Delete[] is used to release the array allocated memory which was allocated using new[] and delete is used to release one chunk of memory which was allocated using new.

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

  1. Is it legal to assign a base class object to a derived class pointer?

No, it will be error as the compiler fails to do conversion.

  1. What happens if an exception is thrown outside a try block?

The program shall quit abruptly.

  1. Are the exceptions and error same?

No, exceptions can be handled whereas program cannot resolve errors.

  1. What is function overriding?

Defining the functions within the base and derived class with the same signature and name where the base class’s function is virtual.

  1. Which function is used to move the stream pointer for the purpose of reading data from stream?


  1. Which function is used to move the stream pointer for the purpose of writing data from stream?


  1. Are class functions taken into consideration as part of the object size?

No, only the class member variables determines the size of the respective class object.

  1. Can we create and empty class? If so what would be the size of such object.

We can create an empty class and the object size will be 1.

  1. What is ‘std’?

Default namespace defined by C++.

  1. What is the full form of STL?

Standard template library

  1. What is ‘cout’?

cout is the object of ostream class. The stream ‘cout’ is by default connected to console output device.

  1. What is ‘cin’?

cin is the object of istream class. The stream ‘cin’ is by default connected to console input device.

  1. What is the use of the keyword ‘using’?

It is used to specify the namespace being used in.

  1. If a pointer declared for a class, which operator can be used to access its class members?

Arrow (->) operator can be used for the same

  1. What is difference between including the header file with-in angular braces < > and double quotes “ “

If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path

  1. S++ or S = S+1, which can be recommended to increment the value by 1 and why?

S++, as it is single machine instruction (INC) internally.

  1. What is the difference between actual and formal parameters?

The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.

  1. What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable.

  1. Which key word is used to perform unconditional branching?


  1. What is the purpose of #undef preprocessor?

It will be used to undefine an existing macro definition.

  1. Can we nest multi line comments in a C++ code?

No, we cannot.

  1. What is a virtual destructor?

A virtual destructor ensures that the objects resources are released in the reverse order of the object being constructed w.r.t inherited object.

  1. What is the order of objects destroyed in the memory?

The objects are destroyed in the reverse order of their creation.

  1. What is a friend class?

A class members can gain accessibility over other class member by placing the class declaration prefixed with the keyword ‘friend’ in the destination class.

  1. What are the Comments in C++?

Comments in C++ are simply a piece of source code ignored by the compiler. They are only helpful for a programmer to add a description or additional information about their source code.

In C++ there are two ways to add comments:

//single-line comment

/* block comment */

The first type will discard everything after the compiler encounters “//”. In the second type, the compiler discards everything between “/*” and “*/”.

  1. Difference between Declaration and Definition of a variable.

The declaration of a variable is merely specifying the data type of a variable and the variable name. As a result of the declaration, we tell the compiler to reserve the space for a variable in the memory according to the data type specified.


int Result;

char c;

int a,b,c;

All the above are valid declarations. Also, note that as a result of the declaration, the value of the variable is undetermined.

Whereas, a definition is an implementation/instantiation of the declared variable where we tie up appropriate value to the declared variable so that the linker will be able to link references to the appropriate entities.

From above Example,

Result = 10;

C = ‘A’;

These are valid definitions.

  1. Comment on Local and Global scope of a variable.

The scope of a variable is defined as the extent of the program code within which the variable remains active i.e. it can be declared, defined or worked with.

There are two types of scope in C++:

Local Scope: A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block.

Global Scope: A variable has a global scope when it is accessible throughout the program. A global variable is declared on top of the program before all the function definitions.

  1. When there are a Global variable and Local variable with the same name, how will you access the global variable?

When there are two variables with the same name but different scope, i.e. one is a local variable and the other is a global variable, the compiler will give preference to a local variable.

In order to access the global variable, we make use of a “scope resolution operator (::)”. Using this operator, we can access the value of the global variable.



int x= 10;

int main()


int x= 2;

cout<<”Global Variable x = “<<::x;

cout<<”\nlocal Variable x= “<<x;



Global Variable x = 10

local Variable x= 2

  1. What is the difference between equal to (==) and Assignment Operator (=)?

In C++, equal to (==) and assignment operator (=) are two completely different operators.

Equal to (==) is an equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.

The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.

  1. What are the various Arithmetic Operators in C++?

C++ supports the following arithmetic operators:

+ addition

– subtraction

* multiplication

/ division

% module

  1. State the difference between Pre and Post Increment/Decrement Operations.

C++ allows two operators i.e ++ (increment) and –(decrement), that allow you to add 1 to the existing value of a variable and subtract 1 from the variable respectively. These operators are in turn, called increment (++) and decrement (–).





The second statement, a++, will cause 1 to be added to the value of a. Thus a++ is equivalent to

a = a+1; or

a += 1;

A unique feature of these operators is that we can prefix or suffix these operators with the variable. Hence, if a is a variable and we prefix the increment operator it will be


This is called Pre-increment. Similarly, we have pre-decrement as well.

If we prefix the variable a with an increment operator, we will have,


This is the post-increment. Likewise, we have post-decrement too.

The difference between the meaning of pre and post depends upon how the expression is evaluated and the result is stored.

In the case of the pre-increment/decrement operator, the increment/decrement operation is carried out first and then the result passed to an lvalue. Whereas for post-increment/decrement operations, the lvalue is evaluated first and then increment/decrement is performed accordingly.


a = 5; b=6;

++a;       #a=6

b–;         #b=6

–a;         #a=5

b++;      #6

  1. What are the Extraction and Insertion operators in C++? Explain with examples.

In the iostream.h library of C++, cin, and cout are the two data streams that are used for input and output respectively. Cout is normally directed to the screen and cin is assigned to the keyboard.

“cin” (extraction operator): By using overloaded operator >> with cin stream, C++ handles the standard input.

int age;


As shown in the above example, an integer variable ‘age’ is declared and then it waits for cin (keyboard) to enter the data. “cin” processes the input only when the RETURN key is pressed.

“cout” (insertion operator): This is used in conjunction with the overloaded << operator. It directs the data that followed it into the cout stream.



cout<<”Hello, World!”;


  1. What is the difference between while and do while loop? Explain with examples.

The format of while loop in C++ is:

While (expression)


The statement block under while is executed as long as the condition in the given expression is true.

The general format of do-while is:

do {statement;} while(condition);

the statement inside the loop is executed at least once as the loop condition is at the end. These are the main differences between the while and do-while.

In case of the while loop, we can directly exit the loop at the beginning, if the condition is not met whereas in the do-while loop we execute the loop statements at least once.

  1. Difference between delete and delete[].

“delete[]” is used to release the memory allocated to an array which was allocated using new[]. “delete” is used to release one chunk of memory which was allocated using new.

  1. When to use “const” reference arguments in a function?

Using “const” reference arguments in a function is beneficial in several ways:

“const” protects from programming errors that could alter data.

As a result of using “const”, the function is able to process both const and non-const actual arguments, which is not possible when “const” is not used.

Using a const reference will allow the function to generate and use a temporary variable in an appropriate manner.

  1. Difference between Class and Structure.

Structure: In C language, the structure is used to bundle different types of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name.

Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.

  1. What are the various Access Specifiers in C++?

C++ supports the following access specifiers:

Public: Data members and functions are accessible outside the class.

Private: Data members and functions are not accessible outside the class. The exception is the usage of a friend class.

Protected: Data members and functions are accessible only to the derived classes.

  1. What is a Constructor and how is it called?

Constructor is a member function of the class having the same name as the class. It is mainly used for initializing the members of the class. By default constructors are public.

There are two ways in which the constructors are called:

Implicitly: Constructors are implicitly called by the compiler when an object of the class is created. This creates an object on a Stack.

Explicit Calling: When the object of a class is created using new, constructors are called explicitly. This usually creates an object on a Heap.

  1. What is a Conversion Constructor?

It is a constructor that accepts one argument of a different type. Conversion constructors are mainly used for converting from one type to another.

  1. What is an Explicit Constructor?

A conversion constructor is declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. Its purpose is reserved explicitly for construction.

  1. Explain Function Overloading and Operator Overloading.

C++ supports OOPs concept Polymorphism which means “many forms”.

In C++ we have two types of polymorphism, i.e. Compile-time polymorphism, and Run-time polymorphism. Compile-time polymorphism is achieved by using an Overloading technique. Overloading simply means giving additional meaning to an entity by keeping its base meaning intact.

C++ supports two types of overloading:

Function Overloading:

Function overloading is a technique that allows the programmer to have more than one function with the same name but different parameter list. In other words, we overload the function with different arguments i.e. be it the type of arguments, number of arguments or the order of arguments.

Function overloading is never achieved on its return type.

Operator Overloading:

This is yet another type of compile-time polymorphism that is supported by C++. In operator overloading, an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type. But while doing this, the standard definition of that operator is kept intact.

For Example, an Addition operator (+) that operates on numerical data types can be overloaded to operate on two objects just like an object of complex number class.


  1. What is the difference between Method Overloading and Method Overriding in C++?

Method overloading is having functions with the same name but different argument lists. This is a form of compile-time polymorphism.

Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions.

  1. Name the Operators that cannot be Overloaded.

sizeof – sizeof operator

. – Dot operator

.* – dereferencing operator

-> – member dereferencing operator

:: – scope resolution operator

?: – conditional operator

  1. What are the benefits of Operator Overloading?

By overloading standard operators on a class, we can extend the meaning of these operators, so that they can also operate on the other user-defined objects.

Function overloading allows us to reduce the complexity of the code and make it more clear and readable as we can have the same function names with different argument lists.

  1. What is Inheritance?

Inheritance is a process by which we can acquire the characteristics of an existing entity and form a new entity by adding more features to it.

Inheritance allows code re-usability, thereby saving time on code development.

  1. What are Multiple Inheritances (virtual inheritance)? What are its advantages and disadvantages?

In multiple inheritances, we have more than one base classes from which a derived class can inherit. Hence, a derived class takes the features and properties of more than one base class.

For Example, a class driver will have two base classes namely, employee and a person because a driver is an employee as well as a person. This is advantageous because the driver class can inherit the properties of the employee as well as the person class.

But in the case of an employee and a person, the class will have some properties in common. However, an ambiguous situation will arise as the driver class will not know the classes from which the common properties should be inherited. This is the major disadvantage of multiple inheritances.

  1. What is Polymorphism?

The basic idea behind polymorphism is in many forms. In C++, we have two types of Polymorphism:

(i) Compile-time Polymorphism

In compile-time polymorphism, we achieve many forms by overloading. Hence, we have an Operator overloading and function overloading. (We have already covered this above)


(ii) Run-time Polymorphism

This is the polymorphism for classes and objects. General idea is that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

This means, that an object reacts differently to the same function call. This type of polymorphism can use a virtual function mechanism

  1. What is a friend function?

C++ class does not allow its private and protected members to be accessed outside the class. But this rule can be violated by making use of the “Friend” function.

As the name itself suggests, friend function is an external function that is a friend of the class. For friend function to access the private and protected methods of the class, we should have a prototype of the friend function with the keyword “friend” included inside the class.

  1. What is a friend class?

Friend classes are used when we need to override the rule for private and protected access specifiers so that two classes can work closely with each other.

Hence, we can have a friend class to be a friend of another class. This way, friend classes can keep private, inaccessible things in the way they are.

When we have a requirement to access the internal implementation of a class (private member) without exposing the details by making the public, we go for friend functions.

  1. Comment on C++ standard exceptions?

C++ supports some standard exceptions that can be caught if we put the code inside the try block. These exceptions are a part of the base class “std:: exception”. This class is defined in the C++ header file <exception>.

Few Examples of Exceptions supported by this class include:

bad_alloc – thrown by ‘new’

runtime_error – thrown for runtime errors

bad_typeid – thrown by type id

  1. What are the differences between a shallow copy and a deep copy?

The differences between a shallow copy and a deep copy can be stated as under.

Shallow Copy Deep Copy
It allows memory dumping on a bit by bit basis from one object to another. It allows the copy field, which is done by field from one object to another.
It is achieved by using a copy instructor and an overloading assignment operator. It is used for shallow copy purposes.
  1. Can we overload a destructor?

No, a destructor cannot be overloaded, and it has the only form without the parameters.

  1. What are the basics concepts of OOP?
  2. Classes and Objects:

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.

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;

  1. B) Encapsulation

Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from outside world. It’s also called data hiding. In c++, encapsulation achieved using the access specifiers (private, public and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data.

  1. C) Data abstraction

Data abstraction refers to hiding the internal implementations and show only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes.

  1. D) Inheritance

Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class.

  1. What is realloc() and free()? What is difference between them?

void* realloc (void* ptr, size_t size):

This function is used to change the size of memory object pointed by address ptr to the size given by size. If ptr is a null pointer, then realloc will behave like malloc(). If the ptr is an invalid pointer, then defined behaviour may occur depending the implementation. Undefined behaviour may occur if the ptr has previously been deallocated by free(), or dealloc() or ptr do not match a pointer returned by an malloc(), calloc() or realloc().

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.

  1. What do you mean by persistent and non persistent objects?

Persistent objects are the ones which we can be serialized and written to disk, or any other stream. So before stopping your application, you can serialize the object and on restart you can deserialize it. [ Drawing applications usually use serializations.]

Objects that can not be serialized are called non persistent objects. [ Usually database objects are not serialized because connection and session will not be existing when you restart the application. ]

  1. What you mean by early binding and late binding? How it is related to dynamic binding?

[This c++ interview question is related to question about virtual functions ]

Binding is the process of linking actual address of functions or identifiers to their reference. This happens mainly two times.

During compilation: This is called early binding

For all the direct function references compiler will replace the reference with actual address of the method.

At runtime: This is called late binding.

In case of virtual function calls using a Base reference, as in shown in the example of question no: 2, compiler does not know which method will get called at run time. In this case compiler will replace the reference with code to get the address of function at runtime. Dynamic binding is another name for late binding.

  1. Explain STL. What are the different types of STL containers?

STL stands for Standard Template Library. It is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification.

Following are the 3 types of STL containers :

  1. Adaptive containers : For e.g. queue, stack
  2. Associative containers : For e.g. set, map
  3. Sequence containers : For e.g. vector, deque
  4. List the advantages of inheritance.

– Inheritence permits code reusability.

– Reusability saves time in program development.

– It encourages the reuse of proven and debugged high-quality software which reduces the problems after a system becomes functional.

  1. What is a stack? How it can be implemented?

A Stack is a linear data structure which is a collection of homogenous elements but where insertion and deletion operations take place at one end only, called TOP of the stack. Stack is also known as LIFO (Last In First Out) structure as the element which is inserted in the last will be the first element to come out from the stack.

It can be implemented by using a linear array say S [] containing stksize locations along with an integer variable TOP storing the index of the top most element in the stack.

  1. What are the various operations performed on stack?

Various operations which can be performed on stack are as follows :

creatempty(): It creates an empty stack by initializing TOP to -1.

Isempty(): It determines whether stack is empty or not. It returns value 1 if stack is empty otherwise return 0.

Push(): Adding a new element at the top of the stack is called Push.

Pop(): Removing an element from the top of the stack is called Pop.

  1. What are the four partitions in which C++ compiler divides the RAM?
  2. Stack Area:

This part of memory is used to store formal parameters, local variables, return addresses of function call etc.

  1. Program Code Area:

This part of memory is used to store the object code of the program.

  1. Global Variable Section:

This part of memory is used to store global variables defined in the program.

  1. Heap Area or Free Storage Pool:

It consists of unallocated memory locations which are allocated dynamically during program execution using new operator.

  1. What are the advantages of using a pointer? Define the operators that can be used with a pointer.

Advantages of pointer:

– Through pointer, one can access a memory location directly and manipulate it.

– Pointers support dynamic memory allocation using new & delete operator.

– A pointer makes execution of certain routines faster.

– The operators that can be used with a pointer:

At Address operator (*):

This operator gives the rvalue of the memory location whose address is stored in its operand.

For example: If P is a pointer that hold the address of variable A, then *P gives the rvalue of the variable A.

ADDRESS operator (&):

This operator gives the address of its operand.

For Example: &A gives the address of variable A.

  1. Define macro.

There is no way for the compiling to verify that the macro parameters are of compatible types.

The macro can be expanded without any special type checking.

If macro parameter is having a post incremented variable ( like c++ ), the increment will be performed twice.

for example:


#define min(i, j) ( i < j ? i : j )



T min ( T i, T j )


return i < j ? i : j ;


  1. What is C++?

C++ is created by Bjarne Stroustrup of AT&T Bell Labs as an extension of C, C++ is an object-oriented computer language used in the development of enterprise and commercial applications. Microsoft’s Visual C++ became the premier language of choice among developers and programmers.

  1. Define Constructors?

A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

  1. What are the differences between new and malloc?

New initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete.

Malloc allocates uninitialized memory.

The allocated memory has to be released with free. New automatically calls the constructor while malloc (doesn’t).

  1. What is a Standard Template Library (STL)? What are the various types of STL Containers?

A Standard Template Library (STL) is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification. We have various types of STL containers depending on how they store the elements.

Queue, Stack – These are the same as traditional queue and stack and are called adaptive containers.

Set, Map – These are basically containers that have key/value pairs and are associative in nature.

Vector, deque – These are sequential in nature and have similarities to arrays.


  1. Why do we need the Friend class and function?

Answer: Sometimes, there is a need for allowing a particular class to access private or protected members of a class. The solution is a friend class, which is capable of accessing the protected as well as the private members of the class in which it is declared as a friend.

Similarly to the friend class, a friend function is able to access private and protected class members. A friend function can either be a global function or a method of some class.

Some important points about friend class and friend function:

  • Friendship is not inherited
  • Friendship isn’t mutual i.e. if some class called Friend is a friend of some other class called NotAFriend then it doesn’t automatically become a friend of the Friend class
  • The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming
  1. A comparison between C++ and Java

C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection

C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn’t have any of them

Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads

In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java

C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent.

  1. What are the most important differences between C and C++?
  • C++ supports references while C doesn’t
  • Features like friend functions, function overloading, inheritance, templates, and virtual functions are inherent to C++. These are not available in C programming language
  • In C, exception handling is taken care of in the traditional if-else style. On the other hand, C++ offers support for exception handling at the language level
  • Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is the standard input stream while cout serves as the standard output stream
  • While C is a procedural programming language, C++ provides support for both procedural and object-oriented programming approaches
Related Posts:

For more Interview Questions And Answers click here

Leave a Reply

Your email address will not be published. Required fields are marked *