C++ Interview Questions And Answers

C++ Interview Questions And Answers 2021

Q1 What is C++? 

Answer : C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985. 

C++ is a superset of C with the major addition of classes in C language. 

Initially, Stroustrup called the new language “C with classes”. However, after sometime the name was changed to C++. The idea of C++ comes from the C increment operator ++. 

 

Q2) What are the advantages of C++? 

Answer : C++ doesn’t only maintains all aspects from C language, it also simplifies memory management and adds several features like: 

C++ is a highly portable language means that the software developed using C++ language can run on any platform. 

C++ is an object-oriented programming language which includes the concepts such as classes, objects, inheritance, polymorphism, abstraction. 

C++ has the concept of inheritance. Through inheritance, one can eliminate the redundant code and can reuse the existing classes. 

Data hiding helps the programmer to build secure programs so that the program cannot be attacked by the invaders. 

Message passing is a technique used for communication between the objects. 

C++ contains a rich function library. 

 

Q3 What is a class? 

The class is a user-defined data type. The class is declared with the keyword class. The class contains the data members, and member functions whose access is defined by the three modifiers are private, public and protected. The class defines the type definition of the category of things. It defines a datatype, but it does not define the data it just specifies the structure of data. 

 

Q4 Briefly explain the concept of Inheritance in C++.
Answer: C++ allows classes to inherit some of the commonly used state and behavior from other classes. This process is known as inheritance. 

 

Q5 Can we have a recursive inline function in C++?
Answer: Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won’t be able to determine the depth of the recursion at the compile time. 

Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth fixed at compile time, and insert non-recursive calls at compile time for the cases when the actual depth exceed at run time. 

 

Q6 Define an Inline Function in C++? Write its syntax. Is it possible for the C++ compiler to ignore inlining?
Answer: In order to reduce the function call overhead, C++ offers inline functions. As the name suggests, an inline function is one that is expanded in line when it is called. 

As soon as the inline function is called, the whole code of the same gets either inserted or substituted at the particular point of the inline function call. The substitution is complete by the C++ compiler at compile time. Small inline functions might increase program efficiency. 

 

Q7 How is function overloading different from operator overloading?
Answer: 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. 

 

Q8: Is it possible for a C++ program to be compiled without the main() function?
Answer: Yes, it is possible. However, as the main() function is essential for the execution of the program, the program will stop after compiling and will not execute. 

 

Q9: Draw a comparison between C++ and Java
Answer: 

  • C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection 
  • C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn’t have any of them 
  • Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads 
  • In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java 
  • C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent. 

Q10 Explain what is a class in C++? 

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

 

Q11 How can you specify a class in C++? 

By using the keyword class followed by identifier (name of class) you can specify the class in C++.
Inside curly brackets, body of the class is defined. It is terminated by semi-colon in the end. 

 

 

Q12 Explain what is the use of void main () in C++ language? 

To run the C++ application it involves two steps, the first step is a compilation where conversion of C++ code to object code take place. While 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. 

 

Q13  Explain what is C++ objects? 

Class gives blueprints for object, so basically an object is created from a class or in other words an object is an instance of a class. The data and functions are bundled together as a self-contained unit called an object. Here, in the example A and B is the Object. 

 

Q14 Explain what are the characteristics of Class Members in C++? 

  • Data and Functions are members in C++,
    • Within the class definition, data members and methods must be declared
    • Within a class, a member cannot be re-declare
    • Other that in the class definition, no member can be added elsewhere 

 

Q15 Explain what is Member Functions in Classes? 

The member function regulates the behaviour of the class. It provides a definition for supporting various operations on data held in the form of an object. 

 

Q16  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 storebooleanvalues (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 

 

Q17 What is namespace std; and what is consists of? 

Namespace std; defines your standard C++ library, it consists of classes, objects and functions of the standard C++ library. You can specify the library by using namespace std or std: : throughout the code. Namespace is used to differentiate the same functions in a library by defining the name. 

 

Q18 Explain what is Loop function? What are different types of Loops? 

In any programming language, to execute a set of statements repeatedly until a particular condition is satisfied Loop function is used. The loop statement is kept under the curly braces { } referred as Loop body. 

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

  • While loop
    • For loop
    • Do-while loop 

 

Q19) Explain how functions are classified in C++ ? 

In C++ functions are classified as 

  • Return type
    • Function Name
    • Parameters
    • Function body 

 

Q20 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 

  • Private
    • Public
    • Protected 

 

Q21 Explain what is Polymorphism in C++? 

Polymorphism in C++ is the ability to call different functions by using only one type of the function call. Polymorphism is referred to codes, operations or objects that behave differently in a different context. 

For example, the addition function can be used in many contests like 

  • 5+5àInteger addition
    • Medical+Internship à The same ( + ) operator can be used with different meaning with strings
    • 3.14 + 2.27 à The same ( + ) operator can be used for floating point addition 

 

Q22 Explain what is data abstraction in C++? 

Data abstraction is a technique to provide essential information to the outside world while hiding the background details. 

 

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

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

 

Q23) Explain what is data encapsulation in C++? 

Encapsulation is an object oriented programming concept (oops) which binds together the data and functions. It is also referred as data hiding mechanism. 

 

Q24) Mention what are the types of Member Functions? 

The types of member functions are 

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

 

Q25) Mention what are the decision making statements in C++? Explain if statement with an example? 

The decision making statements in C++ are 

  • if statement
    • switch statement
    • conditional operator 

 

Q26 Explain what is multi-threading in C++? 

To run two or more programs simultaneously multi-threading is useful. There are two types of 

  • Process-based: It handles the concurrent execution of the program
    • Thread-based: It deals with the concurrent execution of pieces of the same program. 

 

Q27) 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. 

 

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

 

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

 

Q30 What are the various Arithmetic Operators in C++? 

Answer: C++ supports the following arithmetic operators: 

  • + addition 
  • – subtraction 
  • * multiplication 
  • / division 
  • % module 
  •  

Q3What is an Inline function in C++? 

Answer: 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. 

 

Q3Why are arrays usually processed with for loop? 

Answer: Array uses the index to traverse each of its elements. 

If A is an array then each of its element is accessed as A[i]. Programmatically, all that is required for this to work is an iterative block with a loop variable i that serves as an index (counter) incrementing from 0 to A.length-1. 

This is exactly what a loop does and this is the reason why we process arrays using for loops. 

 

Q 33  State the difference between delete and delete[]. 

Answer: “delete[]” is used to release the memory allocated to an array which was allocated using new[]. “delete” is used to release one chunk of memory which was allocated using new. 

 

Q34 What is a Reference Variable in C++? 

Answer: A reference variable is an alias name for the existing variable. This means that both the variable name and the reference variable point to the same memory location. Hence, whenever the variable is updated, the reference is updated too. 

 

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

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

C++ supports the following storage classes: 

  • Auto 
  • Static 
  • Extern 
  • Register 
  • Mutable 

 

Q36 When to use “const” reference arguments in a function? 

Answer : reference arguments in a function is beneficial in several ways: 

  • const” protects from programming errors that could alter data. 
  • As a result of using “const”, the function is able to process both const and non-const actual arguments, which is not possible when “const” is not used. 
  • Using a const reference, allows the function to generate and use a temporary variable in an appropriate manner. 

 

Q 37  What is a Class? 

Answer: Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation, the user need not know the details of the working of a class. 

In general, class acts as a blueprint of a project and can include in various parameters and functions or actions operating on these parameters. These are called the members of the class. 

 

Q38 Difference between Class and Structure. 

Answer: 

Structure: In C language, the structure is used to bundle different type of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name. 

Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.  

 

Q 39) What is Namespace? 

Answer: Namespaces allow us to group a set of global classes, objects and/or functions under a specific name. 

The general form to use namespaces is: 

namespace identifier { namespace-body } 

Where identifier is any valid identifier and the namespace-body is the set of classes, objects, and functions that are included within the namespace. Namespaces are especially useful in the case where there is a possibility for more than one object to have the same name, resulting in name clashes. 

 

Q 40What is the use of ‘using’ declaration? 

Answer: Using declaration is used to refer a name from the namespace without the scope resolution operator. 

 

Q 41What is Name Mangling? 

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

 

Q4What is a Constructor and how is it called? 

Answer: Constructor is a member function of the class having the same name as the class. It is mainly used for initializing the members of the class. By default constructors are public. 

There are two ways in which the constructors are called: 

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

 

Q43 What is a COPY CONSTRUCTOR and when is it called? 

Answer: 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. 

 

Q44 What is a Default Constructor? 

Answer: Default constructor is a constructor that either has no arguments or if there are any, then all of them are default arguments. 

 

Q45 What is an Explicit Constructor? 

Answer: A conversion constructor is declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. Its purpose is reserved explicitly for construction. 

 

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

Answer: 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. 

 

Q 47) Explain the Static Member Function. 

Answer: A static member function can access only the static member variable of the class. Same as the static member variables, a static member function can also be accessed using the class name. 

 

Q48 What is the difference between a Copy Constructor and an Overloaded Assignment Operator? 

Answer: A copy constructor and an overloaded assignment operator basically serve the same purpose i.e. assigning the content of one object to another.  

 

Q49 What is the difference between Method Overloading and Method Overriding in C++? 

Answer: Method overloading is having functions with the same name but different argument list. This is a form of compile-time polymorphism. 

Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions. 

 

Q50 Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true. 

Answer: False. Both, Passing by reference look identical to the caller.