0% found this document useful (0 votes)
241 views

CPP Interview Questions and Answers For Experienced

The document provides interview questions and answers related to C++. It begins with defining C++ as an object-oriented programming language created in 1985 by Bjarne Stroustrup as an enhanced version of C with classes and objects. It then compares C and C++, highlighting key differences such as C++ supporting function overloading, inheritance, exception handling, and reference variables. The document continues providing examples of basic C++ structures, advantages of C++, differences between variable declarations and definitions, comments in C++, scopes, OOP concepts, operators, and more advanced topics like inheritance and templates.

Uploaded by

Arpit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views

CPP Interview Questions and Answers For Experienced

The document provides interview questions and answers related to C++. It begins with defining C++ as an object-oriented programming language created in 1985 by Bjarne Stroustrup as an enhanced version of C with classes and objects. It then compares C and C++, highlighting key differences such as C++ supporting function overloading, inheritance, exception handling, and reference variables. The document continues providing examples of basic C++ structures, advantages of C++, differences between variable declarations and definitions, comments in C++, scopes, OOP concepts, operators, and more advanced topics like inheritance and templates.

Uploaded by

Arpit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C++ Interview Questions

We have the set of C++ interview questions, which helps both experienced and freshers to
crack the C++ interview.

Read Also: Top 25 CSS Interview Questions

What is C++?

Ans. It is an object-oriented programming language developed in 1985. Bjarne Stroustrup


created it. C++ language is the updated version of C language with having classes and
objects. It was called “C language with class”. After sometimes, it was known as C++
language.

What is the difference between C and C++ language?

Ans. The main difference between C and C++ language is as follows.

C language C++ language


It is a procedure-oriented programming It supports both procedure and object-
language. oriented programming.
It is a superset of C language. So C
It is a subset of the C++ language.
programs also work in the C++ language.
It doesn’t support function overloading, It supports function Overloading,
inheritance, friend functions, and templets. inheritance, friend functions, and templets.
It doesn’t support exception handling. It supports exception handling.
In C language, there is no concept of In C++, there is a concept of the reference
reference. variable.
It uses printf(), and scanf() for performing the It uses streams like cin and cout for
input-output operation. performing the input-output operation.

Give one example of C++’s basic structure.

Ans. The following program shows the basic structure of the C++ language.

#include<iostream.h>
int main(){
cout<<"Welcome to Errorsea";
return 0;
}

Explanation

 Here # is the preprocessor directive.


 Include keyword tells the compiler to add some header files before compilation.
 iostream.h is the header file for input-output operation.

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


 The main () method is the function form, where the compiler starts its compilation. Its
return type is an integer.
 cout is the output stream, which prints the result as an output.
 At the end of curly braces return 0 in the returning point of main() function.

What are the advantages of the C++ language?

Ans. C++ language has the following advantages.

 It is a portable language as it can be run on any platform.


 It is an object-oriented programming language. It includes classes and objects.
 It has rich function libraries.
 It supports the concept of inheritance, polymorphism, and friend functions.
 Data hiding is used in C++ language for providing data security.

Differentiate the declaration and definition of the variable.

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.

The syntax for definition is as follows.

variable_name = value;

Examples

a = 10;
Avg = 8.80;
name = “errorsea”;

Explain comments in C++.

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.

C++ supports two types of comments.

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


1. Single line comment - It uses “// “for commenting on the note. The compiler ignores
everything after “// “in the line.
2. Multiline comment or block comment - It uses “ /* ” “ */ “ for commenting the
note. Compiler ignores everything between “ /* “ “ */ “.

Explain scopes in the C++ language.

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(){

int local_total = 0; // local variable

Explanation

Here global_total is a global variable, and local_total is the local variable.

List out various OOP concepts of the C++ language.

Ans. C++ supports the following OOP concepts.

 Class
 Object
 Inheritance
 Abstraction
 Data hiding
 Polymorphism

What will be the output of code and also give a reason?


#include<iostream.h>
char car = “BMW”;
int main()
{
char car = “Audi”;
cout << car;
return 0;
}

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


Ans. The output of the above code is as given below.

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.

Which types of token are supported in the C++ language.

Ans. C++ language supports the following type of token.

 Keyword
 Identifier
 Literal
 Constant
 Symbol

Read Also: Download Turbo C++ for Windows 7, 8, and 10

What is constant? Give types of constants.

Ans. Constant is one type of expression having a fixed value. We can define different types
of constants according to its data type.

Constant can be of the following types.

Type Example
Integer 50
Floating point 8.80
Character “E”
String “errorsea”
Octal 0113
Hexadecimal 0x5c

Note

 We define constant as octal by adding “0” at the starting point.


 We define constant as hexadecimal bt adding “0x” at the starting point.

What are the arithmetic operators used in the C++ language?

Ans. C++ language supports the following arithmetic operators.

 Addition +
 Subtraction -
 Multiplication *
 Division /

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


 Modulo %

Differentiate the assignment ( = ) and equal to operator ( == ).

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.

Explain C++ access specifiers.

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.

What is the full name of STL in C++?

Ans. The full form of STL is a standard templet library.

List out different types of inheritance in C++.

Ans. C++ language supports the following types of inheritance.

 Single level inheritance


 Multi-level inheritance
 Multiple inheritances
 Hierarchical inheritance
 Hybrid inheritance
 Multipath inheritance

State the difference between array and the list.

Ans. The array and list have the following differences.

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.

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


What is the static variable?

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.

Explain the constructor in brief.

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

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


Suppose we want to create the destructor of the demo class.

~demo(); // destructor of demo class

Read Also: Top 25 PHP OOPS Interview Questions

Explain overloading and its types.

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.

Differentiate the method overloading and method overriding.

Ans. Method overloading is the compile-time polymorphism. It is the method having


multiple functions with the same name but with different arguments type and number of
arguments.

Method overriding is runtime polymorphism. It happens when we define the method derived
from base class into derived class.

What is the friend function in the C++ language?

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();
}

Explain namespace in brief.

Ans. C++ uses namespaces for extended support for programming on a larger scale.

 A namespace mainly removes naming conflicts in the code.


 It is also helpful to reduce ambiguity, which happens when multiple tasks occur with
the same name.

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.


 It defines the scope where the class, variables, and functions are declared. C++ has the
standard namespace.
 By using the code “using namespace std;” we can declare the standard namespace in
our code.

List the operators that can not be overloaded.

Ans. The following operators can not be overloaded.

 sizeof – sizeof operator


 :: – scope resolution operator
 ?: – conditional operator
 .* – dereferencing operator
 . – Dot operator
 -> – member dereferencing operator

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.

I hope you found this post informative.

Thank you for reading :)

Visit https://errorsea.com/interview-questions/ for more interview questions and answers.

You might also like