C/C++ interview question set 2/C C++ Interview Questions and Answers for Freshers & Experienced

What is the role of the Static keyword for a class member variable?

The static member variable shares a common memory across all the objects created for the respective class. We need not refer to the static member variable using an object. However, it can be accessed using the class name itself.

Posted Date:- 2021-08-24 06:34:00

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.


class A{
int x; int y;
public int color;
public A() : x(0) , y(0) {} //default (no argument) constructor
public A( const A& ) ;
A::A( const A & p )
this->x = p.x;
this->y = p.y;
this->color = p.color;
A Myobj;
Myobj.color = 345;
A Anotherobj = A( Myobj ); // now Anotherobj has color = 345

Posted Date:- 2021-08-24 06:32:58

What is the difference between an Object and a Class?

Class is a blueprint of a project or problem to be solved and consists of variables and methods. These are called the members of the class. We cannot access methods or variables of the class on its own unless they are declared static.

In order to access the class members and put them to use, we should create an instance of a class which is called an Object. The class has an unlimited lifetime whereas an object has a limited lifespan only.

Posted Date:- 2021-08-24 06:31:46

What is Name Mangling?

C++ compiler encodes the parameter types with function/method into a unique name. This process is called name mangling. The inverse process is called as demangling.


A::b(int, long) const is mangled as ‘b__C3Ail’.

For a constructor, the method name is left out.

That is A:: A(int, long) const is mangled as ‘C3Ail’.

Posted Date:- 2021-08-24 06:30:35

Explain Register Storage Specifier.

“Register” variable should be used whenever the variable is used. When a variable is declared with a “register” specifier, then the compiler gives CPU register for its storage to speed up the lookup of the variable.

Posted Date:- 2021-08-24 06:29:03

What is a Storage Class? Mention the Storage Classes in C++.

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

C++ supports the following storage classes:


Posted Date:- 2021-08-24 06:28:08

What is wrong with this code?

T *p = 0;
delete p;

In the above code, the pointer is a null pointer. Per the C++ 03 standard, it’s perfectly valid to call delete on a NULL pointer. The delete operator would take care of the NULL check internally.

Posted Date:- 2021-08-24 06:26:57

Explain what is COPY CONSTRUCTOR and what is it used for?

COPY CONSTRUCTOR is a technique that accepts an object of the same class and copies its data member to an object on the left part of the assignment.

Posted Date:- 2021-08-24 06:26:11

Explain what is pre-processor in C++?

Pre-processors are the directives, which give instruction to the compiler to pre-process the information before actual compilation starts.

Posted Date:- 2021-08-24 06:25:21

Explain what is upcasting in C++?

Upcasting is the act of converting a sub class references or pointer into its super class reference or pointer is called upcasting.

Posted Date:- 2021-08-24 06:24:30

Explain what is C++ exceptional handling?

The problem that arises during execution of a program is referred as exceptional handling. The exceptional handling in C++ is done by three keywords.

a). Try: It identifies a block of code for which particular exceptions will be activated
b). Catch: The catch keyword indicates the catching of an exception by an exception handler at the place in a program
c). Throw: When a problem exists while running the code, the program throws an exception

Posted Date:- 2021-08-24 06:22:54

What is the C-style character string?

The string is actually a one-dimensional array of characters that is terminated by a null character ''.

For example, to type hello word

Using namespace std;
int main ()
char greeting[6] = { 'H' , 'e' , 'l' ,'l' , 'o' , ''};
cout << "Greeting message:" ;
cout << greeting << endl;
return 0;

Posted Date:- 2021-08-24 06:21:45

Explain what are Access specifiers in C++ class? What are the types?

Access specifiers determine the access rights for the statements or functions that follow it until the end of class or another specifier is included. Access specifiers decide how the members of the class can be accessed. There are three types of specifiers.

1. Private
2. Public
3. Protected

Posted Date:- 2021-08-24 06:20:21

Define basic type of variable used for a different condition in C++?

The variable used for a different condition in C++ are

Bool: Variable to store boolean values (true or false)
Char: Variable to store character types
int : Variable with integral values
float and double: Types of variables with large and floating point values

Posted Date:- 2021-08-24 06:19:09

.Explain Function overloading with a different number of arguments with an example?

C++ enables two or more functions with the same name but with different types of arguments or a different sequences of arguments or a different numbers of arguments. It is also called “Function Polymorphism”


void display(char c, int n)

{for ( ;n>0;n–)

cout<<c<<” “;}

void display(int a, int b)

{for (;b>0;b–)

cout<<a<<” “;}

Posted Date:- 2021-08-24 06:17:19

Explain the declaration of variables in C++?

Variables should be declared anywhere within the program

Posted Date:- 2021-08-24 06:16:13

How To Get The Current Position Of The File Pointer?

We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes) positions of put pointer and get pointer respectively.

Posted Date:- 2021-08-24 06:15:29

What is an Inline function in C++?

Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.

Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

Posted Date:- 2021-08-24 06:10:24

What is object in C++?

Class in C++ provides a blueprint for object, that means, object is created from the class.

For example,

class Circle{

float radius;

Circle C1;
Circle C2;

Posted Date:- 2021-08-24 06:09:00

How to reverse a vector in C++?

syntax: reverse(the index you want to start reversing at,the index you want to end reversing at)
vector<int> value = {11,22,33};

Posted Date:- 2021-08-24 06:07:44

How to initialize vector in C++?

There are multiple ways to do it:
You can do it like arrays:
vector<int> value{ 11, 22, 33 };
or by pushing values one by one:
vector<int> value;

Posted Date:- 2021-08-24 06:07:09

What is a Friend class?

Similar to friend function one class can be made as a friend to another class. Let’s say X and Y are separate classes. When X is made friend to class Y, the member functions of Class X becomes friend functions to Class Y. Member functions of Class X can access the private and protected data of Class Y but Class Y cannot access the same of ClassX.

class X;
class Y
// class X is a friend class of class Y
friend class X;
... .. ...
class X
... .. ...

Posted Date:- 2021-08-24 06:06:27

What are new and delete operator?

The new operator is used to dynamically allocate memory and delete operator is used to destroy the memory allocated by the new operator.

Posted Date:- 2021-08-24 06:05:31

How to concatenate two strings in C++?

"#include <iostream>
using namespace std;
int main
string str_a = ""Hi, My name is"";
string str_b = ""Raj"";
string str_c = str_a + str_b;
cout<<""The string is:""<<str_c;
return 0;

Posted Date:- 2021-08-24 06:04:54

Explain briefly Classes & Objects?

CLASS IS A collection of data member and member function

The object is an entity, used to call the functions in a class

Posted Date:- 2021-08-24 06:04:05

What is calloc() function? Describe the syntax?

calloc() used to allocate multiple blocks of contiguous memory in bytes. All the blocks are of same size.


Ptr_name= (*cast type) calloc (No.of blocks,int size);

Posted Date:- 2021-08-24 06:03:27

What is the function of the keyword "Auto"?

The keyword “Auto” is used by default for various local variables to make function work automatically.

Posted Date:- 2021-08-24 06:02:27

What are the functions of the scope resolution operator?

The functions of the scope resolution operator include the following.

1. It helps in resolving the scope of various global variables.
2. It helps in associating the function with the class when it is defined outside the class.
The code of the scope resolution operator can be displayed as follows.

#include <iostream>
using namespace std;
int my_var = 0;
int main(void) {
int my_var = 0;
::my_var = 1; // set global my_var to 1
my_var = 2; // set local my_var to 2
cout << ::my_var << ", " << my_var;
return 0;

Posted Date:- 2021-08-24 06:01:57

Define Block scope variable?

A Block scope variable is the one that is specified as a block using the C++ that can be declared anywhere within the block.

Posted Date:- 2021-08-24 06:00:49

Can we have a String primitive data type in C++?

No, we cannot have a String Primitive data type in C++. Instead, we can have a class from the Standard Template Library (STL).

Posted Date:- 2021-08-24 06:00:18

What is a mutable storage class specifier? How can they be used?

A mutable storage class specifier is applied only on the class's non-static and non-constant member variable. It is used for altering the constant class object's member by declaring it. This can be done by using a storage class specifier.

Posted Date:- 2021-08-24 05:59:47

What do you mean by ‘void’ return type?

All functions should return a value as per the general syntax.

However, in case, if we don’t want a function to return any value, we use “void” to indicate that. This means that we use “void” to indicate that the function has no return value or it returns “void”.


void myfunc()
Cout<<”Hello,This is my function!!”;
int main()
return 0;

Posted Date:- 2021-08-24 05:58:41

How to initialize a 2d vector in C++?

The syntax to initialize a 2d vector is as follows:
std::vector<std::vector<int> > name_of_vector;

For example: std::vector<std::vector<int> > v { { 1, 2, 1 },
{ 2, 6, 7 } };

Posted Date:- 2021-08-24 05:57:08

What is stl in C++ with example?

STL in C++ is a library and abbreviation of Standard Template Library. STL is a generalized library that provides common programming data structures/ container classes, functions, algorithms, and iterators. STL has four components

– Algorithms: Searching and sorting algorithms such as binary search, merge sort etc.
– Containers: Vector, list, queue, arrays, map etc.
– Functions: They are objects that act like functions.
– Iterators: It is an object that allows transversing through elements of a container, e.g., vector<int>::iterator.

Posted Date:- 2021-08-24 05:56:30

What is the use of pure virtual function?

A pure virtual function does not have implementation in base class. This will be useful during scenarios where no actual implementation is required in the base class context. For example let’s say we have a base class named Shape which is having a function “DrawShape()” as pure virtual. In base class there is actually no need to give a definition. Now, when a Class Square derives from Class Shape, it provides an actual definition for DrawShape() as drawing a square.

Posted Date:- 2021-08-24 05:54:55

What are character constants in C++?

Character constant are members of the character set in which a program is written which is surrounded by single quotation marks (‘).

Posted Date:- 2021-08-24 05:54:19

Is there any advantage of using one over the other?

Though both codes will generate the same output, sample code 2 is a more performant option. This is due to the fact that the post-increment ‘itr++’ operator is more expensive than the pre-increment ‘++itr’ operator.

The post-increment operator generates a copy of the element before proceeding with incrementing the element and returning the copy. Moreover, most compilers will automatically optimize sample code 1 by converting it implicitly into sample code 2.

Posted Date:- 2021-08-24 05:53:32

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.

Posted Date:- 2021-08-24 05:50:42

What is an Abstract Class?

A class is said to be abstract when it contains at least one pure virtual function. Instantiation is not allowed for an abstract class. And the deriving class must implement or provide definition for the pure virtual functions.

class Base { public: virtual void display () = 0; };

Posted Date:- 2021-08-24 05:49:58

How many ways are there to initialize an int with a Constant?

There are two ways:

1. The first format uses traditional C notation.
int result = 10;
2. The second format uses the constructor notation.
int result (10);

Posted Date:- 2021-08-24 05:48:02

What is the precedence when there are a Global variable and a Local variable in the program with the same name?

Whenever there is a local variable with the same name as that of a global variable, the compiler gives precedence to the local variable.


#include <iostream.h>
int globalVar = 2;
int main()
int globalVar = 5;
The output of the above code is 5. This is because, although both the variables have the same name, the compiler has given preference to the local scope.

Posted Date:- 2021-08-24 05:46:39

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++:

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


#include <iostream.h>
Int globalResult=0; //global variable
int main()
Int localVar = 10; //local variable.


Posted Date:- 2021-08-24 05:45:36

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.

Posted Date:- 2021-08-24 05:43:30

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 “*/”.

Posted Date:- 2021-08-24 05:41:56

What is destructor?

A destructor is a special member function of a class that destroys the resources that have been allocated to the class object. The destructor class name is same as that of the class but prefixed with a ~ symbol.

For example:

Class Bird
{ char name [50];
~Bird ()
This is Destructor of Class Bird

Posted Date:- 2021-08-24 05:40:08

What is vector in C++?

A sequence of containers to store elements, a vector is a template class of C++. Vectors are used when managing ever-changing data elements. The syntax of creating vector.
vector <type> variable (number of elements)

For example:

vector <int> rooms (9);

Posted Date:- 2021-08-24 05:38:39

What are the types of Polymorphism?

Polymorphism is classified into two types namely:

1. Compile-time Polymorphism or Overloading
2. Run-time Polymorphism or Overriding

Posted Date:- 2021-08-24 05:38:12

What is Polymorphism?

Polymorphism is the concepts of having one functionality do multiple things. The words “poly”(many) and “morph” (forms) is itself explain the concept.

Posted Date:- 2021-08-24 05:37:37

What is exception handling in C++?

Exceptions are errors that happen during execution of code. To handle them we use throw, try & catch keywords.

Posted Date:- 2021-08-24 05:36:31

What is inline function in C++?

Inline functions are functions used to increase the execution time of a program. Basically, if a function is inline, the compiler puts the function code wherever the function is used during compile time. The syntax for the same is as follows:

inline return_type function_name(argument list) {
//block of code

Posted Date:- 2021-08-24 05:35:37

R4R Team
R4R provides C C++ Freshers questions and answers (C C++ Interview Questions and Answers) .The questions on R4R.in website is done by expert team! Mock Tests and Practice Papers for prepare yourself.. Mock Tests, Practice Papers,C/C++ interview question set 2,C C++ Freshers & Experienced Interview Questions and Answers,C C++ Objetive choice questions and answers,C C++ Multiple choice questions and answers,C C++ objective, C C++ questions , C C++ answers,C C++ MCQs questions and answers R4r provides Python,General knowledge(GK),Computer,PHP,SQL,Java,JSP,Android,CSS,Hibernate,Servlets,Spring etc Interview tips for Freshers and Experienced for C C++ fresher interview questions ,C C++ Experienced interview questions,C C++ fresher interview questions and answers ,C C++ Experienced interview questions and answers,tricky C C++ queries for interview pdf,complex C C++ for practice with answers,C C++ for practice with answers You can search job and get offer latters by studing r4r.in .learn in easy ways .