CPP Interview Questions and Answers For Experienced
CPP Interview Questions and Answers For Experienced
We have the set of C++ interview questions, which helps both experienced and freshers to
crack the C++ interview.
What is C++?
Ans. The following program shows the basic structure of the C++ language.
#include<iostream.h>
int main(){
cout<<"Welcome to Errorsea";
return 0;
}
Explanation
Ans. The declaration is the method in which we write variable names with the appropriate
data type. It tells the compiler about to reserve the space for variable according to its data
type.
The syntax for declaration is as follows.
datatype variable_name;
Examples
int a;
float avg;
char name;
The definition is the method in which we assign value to the variable after declaration so that
the linker links the appropriate value with a variable.
variable_name = value;
Examples
a = 10;
Avg = 8.80;
name = “errorsea”;
Ans. Comments are the source code, which is ignored by the compiler. It is not part of the
program. It’s the only purpose is to inform the programmer about additional information or
description about source code.
Ans. The scope is the area in which the variable is active. It means the scope is the area in
which we declare, define, and use the variable.
C++ supports two types of scopes.
1. Local scope - A variable is said to be in a local scope when it is declared inside the
block of code. It remains active only inside that block. We can access the variable
outside the block.
2. Global scope - When it is declared at the top of the program, it is said to be in a
global scope. It is accessible throughout the program.
Example
#include<iostream.h>
int global_total = 0; // global variable
int main(){
Explanation
Class
Object
Inheritance
Abstraction
Data hiding
Polymorphism
Audi
It because of the priority between the local and global variable. When a global and local
variable has the same name, then the local variable gets more priority than the global
variable.
Keyword
Identifier
Literal
Constant
Symbol
Ans. Constant is one type of expression having a fixed value. We can define different types
of constants according to its data type.
Type Example
Integer 50
Floating point 8.80
Character “E”
String “errorsea”
Octal 0113
Hexadecimal 0x5c
Note
Addition +
Subtraction -
Multiplication *
Division /
Ans. The assignment operator and equal to the operator are used for different purposes.
The assignment operator ( = ) - The assignment operator is used to assign the value
to the variable. Sometimes it is used in some complex equations.
The equal to operator ( == ) - This operator is used as an equality operator to
compare two values. It returns true if they are equal, and it returns false if they are
unequal.
Ans. Access specifiers define how variables and functions can be accessed in the class. There
are three types of access specifiers:
1. Private - In this method, we can access the variables and functions only inside the
class. They cannot be accessed outside the class.
2. Protected - In this method, we can access the variables and functions of the parent
class into its child class. This concept is used in inheritance.
3. Public - In this method, we can access the variables and functions anywhere we want
throughout the program.
Array List
It is a collection of homogeneous elements. It is a collection of heterogeneous elements.
Memory allocation is static and fixed. Memory allocation is dynamic and random.
In array, we don’t need to think of the next On the list, we must be aware of the next
memory location. memory location.
Ans. A static variable is one type of local variable that retains the value throughout the
function call. We can declare a variable as static using the static keyword. Static variables are
having zero as a default value.
Example
void incrementnum(){
static int i;
cout << i;
i++;
}
Explanation
If we call the function five times, then it prints the output as follows.
01234
It retains the value zero as a default value. Then it increments the value after every function
call.
Ans. The constructor is the member function of the class having the same name as the class
name. It is mainly used to assign the value to the member variables of that class. The
constructor is classed when the object of the class is created. By default, it’s type public.
Example
class demo{
int x,y;
demo(){
x = 0;
y = 0;
}
}d1;
Explanation
The above class assigns the value to variable x and y zero when the object d1 is created
through the constructor demo().
What is destructor?
Ans. Destructor is used to delete the extra space allocated by the object. It is called when the
object goes out of the scope. Deconstructor has the same name as the class name. It has a
tilde ( ~ ) sign before its name. It does not have an argument or return type.
Example
Ans. It is the method when one object behaves differently according to the situations. It is
having different versions of the same function. In C++, there are two types of overloading.
1. Function Overloading - In this method, we define functions with the same name and
multiple definitions. They are differentiated according to the argument type and
number of arguments.
2. Operator Overloading - It is compile-time polymorphism in which we overload the
standard operators to provide them user-defined definitions.
Method overriding is runtime polymorphism. It happens when we define the method derived
from base class into derived class.
Ans. We usually can not access the private and protected variables of the class. A friend
function is an external function that can access the private and protected variables of the
class. It is declared using the friend keyword.
It is not a member of the class, but it is declared in the class definition. It is defined outside
the class.
Example:
class demo{
int x,y;
public:
friend void test();
}
Ans. C++ uses namespaces for extended support for programming on a larger scale.
Conclusion
That is all for C++ Interview Questions for experienced, and don’t worry if you are a fresher
and not able to answer some tricky questions. I am sure you will feel confident after
preparing for the C++ interview using this series of questions.