Top 25 C++ Interview Questions

C++ language is the most popular object-oriented programming languages. It is mainly used language in software development companies. So if you are looking for a job as a software developer, you should know the C++ language.

We have the set of C++ interview questions, which helps you in cracking the C++ interview.

Read Also: Top 25 CSS Interview Questions

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

2] What is the difference between C and C++ language.

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

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

3] 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.
  • main() 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.

4] 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.

5] 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.

Syntax for defination is as follows:

variable_name = value;

Examples:

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

6] Explain comments in C++ in brief.

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.

Single line comment:

It uses “ // “ for commenting on the note. The compiler ignores everything after “ // “ in the line.

Multiline comment or block comment

It uses “ /* ” “ */ “ for commenting the note. Compiler ignores everything between “ /* “ “ */ “.

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

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.

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 gtotal = 0; // global variable
int main(){

int ltotal = 0; // local variable

}

Explanation:

Here gtotal is a global variable, and ltotal is the local variable.

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

Ans. C++ supports the following OOP concepts.

  • Class
  • Object
  • Inheritance
  • Abstraction
  • Data hiding
  • Polymorphism

9] 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;
}

Ans. The output of the above code is:

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.

10] 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

11] 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 following types:

TypeExample
Integer50
Floating point8.80
Character“E”
String“errorsea”
Octal0113
Hexadecimal0x5c

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.

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

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

  • Addition +
  • Subtraction –
  • Multiplication *
  • Division /
  • Modulo %

13] 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.

14] 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:

Private:

In this method, we can access the variables and functions only inside the class. They cannot be accessed outside the class.

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.

Public:

In this method, we can access the variables and functions anywhere we want throughout the program.

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

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

16] 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

17] State the difference between array and the list.

Ans. The array and list are having the following differences:

ArrayList
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 memory location.On the list, we must be aware of the next memory location.

18] 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.

19] 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 valued 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().

20] 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:

Suppose we want to create the destructor of demo class:

~demo(); // destructor of demo class

Read Also: Top 25 PHP OOPS Interview Questions

21] 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:

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.

Operator overloading:

It is compile-time polymorphism in which we overload the standard operators to provide them user-defined definition.

22] 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.

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

24] Explain namespace in brief.

Ans. A namespace is mainly used to remove naming conflicts in the code.
It is also used to reduce ambiguity, which happens when multiple tasks occur with the same name.
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.

25] 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

That is all for C++ Interview Questions and Don’t worry if you were 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 🙂

Leave a Reply

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