[2022] C++ Interview Questions – Frequently Asked

Written by

Vaaruni Agarwal


C++ is an object oriented programming language, that was developed by Bjarne Stroustrup. The language derives its name from the C language, since most of the functionalities of the language have been derived from the C language
itself.


However, C was a procedural programming language, but C++ also had the concept of classes, thus initially it was thought to be named as ‘C with classes’, the final name C++ came from
the increment operator (++) which is a part of the language. In this article we will be showing 50 C++ Interview Questions and Answers

  1. Who is the father of C++?

Bjarne Stroustrup

  1. Define token in C++.

A token in C++ can be a keyword, identifier, literal, constant and symbol.

  1. What is a class in C++?

A class in C++ is as a collection of function and related data under a single name. It is a blueprint of objects. A C++ program can have any number of classes.

  1. What is the syntax of declaring a class in C++?
class name {

// some data

// some functions

};

  1. What is C++?
  • C++ is an object-oriented programming language that was created by Bjarne Stroustrup. It was released in the year 1985.
  • C++ is a superset of C programming language with the major addition of classes in C.
  • C++ is an object oriented programming language.

  1. What are the advantages of C++?
  • C++ maintains all aspects from C language, and simplifies memory management as well.
  • C++ can run on any platform.
  • C++ is an object-oriented programming language, including the concepts such as classes, objects, inheritance, polymorphism, abstraction.
  • C++ has the concept of inheritance.
  • Data hiding helps the programmer to build secure programs
  • Message passing is a technique used for communication between the objects.

  1. Explain the concepts of OOPs in C++


OOPs stand for Object Oriented Programming, OOPs is an important feature in C++, through which the concept of classes, objects, inheritance, data encapsulation, data hiding, abstraction and polymorphism come into play. All these
features help in building a strong foundation for C++.



  1. What is void main () in C++ language?

The first step, in running a C++ program is compilation

where the conversion of C++ code to object code takes place. The second step includes linking, where combining of object code from the programmer and from libraries takes place. This function is operated by main () in C++
language. Additionally, main() is the starting point of every C++ program, whereas void is the return type of main function which ensures that the main() does not return anything

  1. What are C++ objects?


A class gives blueprints for object, so an object is created from a class, and is an instance of a class. The data and functions are bundled together as a self-contained unit called an object. An object is a real world entity that
has a real existence.

  1. What is Inheritance in C++?


Inheritance provides reusability. Reusability ensures the functionalities of the existing class extend to new classes. It eliminates the redundancy of code. Inheritance is a technique of deriving a new class from the old class. The
old class is the base class, and the new class is known as derived class.

  1. What is the syntax of inheriting classes in C++?
class derived_class :: visibility-mode base_class;

Where visibility-mode can be public, private, protected.

  1. What is Encapsulation in C++?


Encapsulation is a technique of wrapping the data members and member functions in a single unit. It encapsulates the data within a class, and no outside method can access the data. It is an important feature to hide the data from
unauthorized access

  1. What is Abstraction in C++?


Abstraction is a technique of showing only essential details of a class without representing the implementation details. If the members have a public keyword, then they are accessible outside the class also, otherwise certain
restrictions are placed depending on the visibility type.

  1. What is Data binding in C++?

Data binding is a process of binding the application UI and business logic. Any change made in the business logic will reflect directly to the application UI.

  1. What is Polymorphism in C++?

Polymorphism means multiple forms. Polymorphism means having more than one function with the same name but with different functionalities. Polymorphism is of two types:

Static polymorphism is also known as early binding.

Dynamic polymorphism is also known as late binding.

  1. Define storage class in C++.

Storage class in C++ specify the life, scope, access area and the initial values of symbols, including the variables, functions, etc. There are four storage classes in C++:

auto, static, extern, register

  1. What are the two types of Polymorhism in C++. Explain with example

Runtime polymorphism         

Runtime polymorphism or dynamic polymorphism occurs when the child class contains the method which is already present in the parent class. Function overriding is an example of runtime polymorphism.

Compile time polymorphism        

Compile-time polymorphism or static polymorphism is implemented at the compile time. Method overloading is an example of compile-time polymorphism.

  1. What is ‘this’ pointer in C++?

The ‘this’ pointer is a constant pointer and it holds the memory address of the current object. It passes as a hidden argument to all the non-static member function calls.

  1. How is function overloading different from operator overloading?

Function overloading allows two or more functions with different type and number of parameters to have the same name.

Operator overloading, on the other hand, allows for redefining the way an operator works for user-defined types.

  1. Is it possible for a C++ program to be compiled without the main() function?

Yes, it is possible to compile the program. However, as the main() function is essential for the execution of the program, the program will stop after compiling and will not execute

  1. What is the difference between a structure and a class in C++?

When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. The default access specifier is private when deriving a class.

The members of a structure are public by default, the members of a class are private by default

  1. What is a namespace in C++?


The namespace is a logical division of the code, which is designed to stop the naming conflict. It specifies the scope where the identifiers such as variables, class, functions are declared. Used to remove the ambiguity.

For example: if there are two functions exist with the same sum()

To prevent ambiguity, functions  are declared in different namespaces.

  1. What is the standard namespace in C++?

A standard namespace by the name std, contains inbuilt classes and functions.

 So, by using the statement “using namespace std;” namespace “std” is included in a C++ program.

  1. What are the characteristics of Class Members in C++?

Data and Functions are known as class members of a C++ class, their characteristics are:

Within the class definition, data members and methods must be declared

Within a class, a member cannot be re-declared

Other that in the class definition, no new member can be added to the class

  1. What is a Member Function in a C++ Class?


The member function is like a regular function, however, it regulates the behavior of the class. It provides a definition for supporting various operations on data held in the form of an object. The members can be used to initialize
and use the class data members (variables).

  1. What is a Loop in C++?

To execute a set of statements repeatedly until a particular condition is satisfied a Loop is used. The loop statement is kept under the curly braces { }, known as the Loop body.

  1. How many different types of loops are there in C++?

In C++ language, three types of loops are used

While loop

For loop, and

Do-while loop

  1. What is the difference between While and DO While Loop in C++?

While loop is an entry controlled loop, while do-while loop is an exit controlled loop.


In while loop if the condition is false then no statements are executed as the condition is checked on entry itself, in do while loop even if the condition is false then also the statements are executed once as the condition is
checked on exit.

  1. How functions are classified in C++ ?

In C++ functions are classified on the basis of

Return type        –  Void or a data type

Function Name        – Different function names

Parameters        – Accepting arguments or no arguments

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

Access specifiers determine the access rights for the statements. Access specifiers decide how the members of the class can be accessed, they are also used in inheritance. There are three types of specifiers.

Private

Public

Protected

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

A reference variable is declared using & Operator. In this variable, a reference is stored in the form of another name for an already existing variable.

Eg:

int a = 10

int &b = a  // Here b is a reference variable, holding the reference of a.

  1. What are the different types of Member Functions in C++?

They are of 5 types:

  • Simple functions
  • Static functions
  • Const functions
  • Inline functions
  • Friend functions

  1. How delete [ ] is different from delete?

Delete is used to release a unit of memory, delete[] is used to release an array.

  1. What is recursion in C++?

The process through which a function repeatedly calls itself is known as recursion.
 The corresponding function is called the recursive function

  1. What is an Inline function in C++?

An 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”.

  1. What is the keyword auto used for in C++?

By default, a local variable of the function is automatic i.e. auto.

Eg:

void f()

 {

 int a;

 auto int b;

 }

  1. What is a Static Variable in C++?

A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword “static. Their default value is zero.

Example:

void f()

{

static int i;

++i;

printf(“%d “,i);  // The function will print 1 2 3 ….

}

  1. What is Name Mangling in C++?

C++ compiler encodes the parameter types with functions into a unique name. This process is called name mangling. The reverse process of the same is called demangling.

  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 generally used for initializing the members of the class. By default constructors are public.

There are two ways in which the constructors are called:

Implicit Calling: 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 keyword, constructors are called explicitly. This usually creates an object on a Heap.

  1. What is upcasting in C++?

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

  1. 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. They include information such as definitions of built in functions and keywords like, cout,
cin, pi, sin, cos, etc.

  1. What is a copy constructor in C++?

Copy constructor is a constructor that accepts an object of the same class and copies all its data members to an object on the other object of the same class.

  1. What is the difference between new and malloc() in C++?

new is an operator while malloc() is a function.

new initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location (garbage)

new allocates the memory and calls the constructor for the object initialization as well and malloc() function allocates the memory but does not call the constructor.

 new is faster than the malloc() function.

  1. What is a friend function in C++?


Friend function can access the private and protected members of the class. The friend function is not a member of the class, but it must be listed in the class definition. The friend has the ability to access the private data of the
class. It has more privileges than a normal function.

  1. What is a virtual function?


A virtual function replaces the implementation provided by the base class. The replacement is called when the object is of the derived class. A virtual function is a member function which is present in the base class and redefined
by the derived class. It is recognized by the keyword ‘virtual’.

  1. What is a destructor?


A Destructor is used to delete the resources allocated by the object. A destructor is called automatically once the object goes out of the scope. It is complete opposite of a constructor and has the same name as class name, it is
preceded by a tilde sign. Destructors does not contain any argument or a return type.

  1. What is an overflow error?

It is a type of arithmetical error. It occurs when the result of an arithmetical operation evaluates to be greater than the actual space provided by the system.

  1. What is virtual inheritance?

Virtual inheritance facilitates the creation of only one copy of each object even if the object appears in more than one class in the hierarchy.

  1. What does Scope Resolution operator do?

A scope resolution operator (::) is used to define the member function outside the class, with a global scope.

  1. What is a pure virtual function in C++?

The pure virtual function is a virtual function which does not contain any definition, it has only the declaration part. The pure virtual function ends with 0.

Syntax:

virtual void function_name() = 0 ;    //pure virtual function.

[2022] C++ Interview Questions – Frequently Asked