Home Interview Questions Top 30 C++ Language Interview Questions And Answers PDF Download 2019

Top 30 C++ Language Interview Questions And Answers PDF Download 2019

381
0
Top 30 C++ Language Interview Questions And Answers

Top 30 C++ Language Interview Questions And Answers: Here are the top interview questions of C++ language. Be prepared for the interview. You can download the entire questions in a pdf format. so guys All the best for your interview.

1) What is C++?

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


2) What are the advantages of C++?

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.

You might also like: Top 50 Java Interview Question and Answers pdf Download


3) What is the difference between C and C++?

Following are the differences between C and C++:

CC++
C language was developed by Dennis Ritchie.C++ language was developed by Bjarne Stroustrup.
C is a structured programming language.C++ supports both structural and object-oriented programming language.
C is a subset of C++.C++ is a superset of C.
In C language, data and functions are the free entities.In the C++ language, both data and functions are encapsulated together in the form of a project.
C does not support the data hiding. Therefore, the data can be used by the outside world.C++ supports data hiding. Therefore, the data cannot be accessed by the outside world.
C supports neither function nor operator overloading.C++ supports both function and operator overloading.
In C, the function cannot be implemented inside the structures.In the C++, the function can be implemented inside the structures.
Reference variables are not supported in C language.C++ supports the reference variables.
C language does not support the virtual and friend functions.C++ supports both virtual and friend functions.
In C, scanf() and printf() are mainly used for input/output.C++ mainly uses stream cin and cout to perform input and output operations.

4) What is the difference between reference and pointer?

Following are the differences between reference and pointer:

ReferencePointer
Reference behaves like an alias for an existing variable, i.e., it is a temporary variable.The pointer is a variable which stores the address of a variable.
Reference variable does not require any indirection operator to access the value. A reference variable can be used directly to access the value.Pointer variable requires an indirection operator to access the value of a variable.
Once the reference variable is assigned, then it cannot be reassigned with different address values.The pointer variable is an independent variable means that it can be reassigned to point to different objects.
A null value cannot be assigned to the reference variable.A null value can be assigned to the reference variable.
It is necessary to initialize the variable at the time of declaration.It is not necessary to initialize the variable at the time of declaration.

5) 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

7) 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 store boolean values (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

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

9) 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

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

In C++ functions are classified as

• Return type
• Function Name
• Parameters
• Function body

11) Explain what are Access specifiers in C++ class? What are the types?

Top 30 C++ Language Interview Questions And Answers: 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

12) Explain what are Operators and explain with an example?

Operators are specific operands in C++ that is used to perform specific operations to obtain a result. The different types of operators available for C++ are Assignment Operator, Compound Assignment Operator, Arithmetic Operator, Increment Operator and so on.

13) Which programming language’s unsatisfactory performance led to the discovery of C++?

C++was discovered in order to cope with the disadvantages of C.


14) How delete [] is different from delete?

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


15) What is the full form of STL in C++?

STL stands for Standard Template Library.


16) What is an object?

The Object is the instance of a class. A class provides a blueprint for objects. So you can create an object from a class. The objects of a class are declared with the same sort of declaration that we declare variables of basic types.


17) What are the C++ access specifiers?

The access specifiers are used to define how to functions and variables can be accessed outside the class.

There are three types of access specifiers:

  • Private: Functions and variables declared as private can be accessed only within the same class, and they cannot be accessed outside the class they are declared.
  • Public: Functions and variables declared under public can be accessed from anywhere.
  • Protected: Functions and variables declared as protected cannot be accessed outside the class except a child class. This specifier is generally used in inheritance.

18) What is Object Oriented Programming (OOP)?

Top 30 C++ Language Interview Questions And Answers: OOP is a methodology or paradigm that provides many concepts. The basic concepts of Object Oriented Programming are given below:

Classes and Objects: Classes are used to specify the structure of the data. They define the data type. You can create any number of objects from a class. Objects are the instances of classes.

Encapsulation: Encapsulation is a mechanism which binds the data and associated operations together and thus hides the data from the outside world. Encapsulation is also known as data hiding. In C++, It is achieved using the access specifiers, i.e., public, private and protected.

Abstraction: Abstraction is used to hide the internal implementations and show only the necessary details to the outer world. Data abstraction is implemented using interfaces and abstract classes in C++.

Some people confused about Encapsulation and abstraction, but they both are different.

Inheritance: Inheritance is used to inherit the property of one class into another class. It facilitates you to define one class in term of another class.


19) What is the difference between an array and a list?

  • An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
  • Array memory allocation is static and continuous while List memory allocation is dynamic and random.
  • In Array, users don’t need to keep in track of next memory allocation while In the list, the user has to keep in track of next location where memory is allocated.

20) What is the difference between new() and malloc()?

  • new() is a preprocessor while malloc() is a function.
  • There is no need to allocate the memory while using “new” but in malloc() you have to use sizeof().
  • “new” initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.
  • The new() operator allocates the memory and calls the constructor for the object initialization and malloc() function allocates the memory but does not call the constructor for the object initialization.
  • The new() operator is faster than the malloc() function as operator is faster than the function.

21) 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

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

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

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

25) What is a destructor?

A Destructor is used to delete any extra resources allocated by the object. A destructor function is called automatically once the object goes out of the scope.

Rules of destructor:

  • Destructors have the same name as class name and it is preceded by tilde.
  • It does not contain any argument and no return type.

26) What is function overriding?

If you inherit a class into a derived class and provide a definition for one of the base class’s function again inside the derived class, then this function is called overridden function, and this mechanism is known as function overriding.


27) What is virtual inheritance?

Virtual inheritance facilitates you to create only one copy of each object even if the object appears more than one in the hierarchy.


28) What is a constructor?

A Constructor is a special method that initializes an object. Its name must be same as class name.


29) What is the purpose of the “delete” operator?

The “delete” operator is used to release the dynamic memory created by “new” operator.

You might also like: Top 20 MongoDB Interview Questions and Answers

30) What is a pure virtual function?

The pure virtual function is a virtual function which does not contain any definition. The normal function is preceded with a keyword virtual. The pure virtual function ends with 0.

Syntax of a pure virtual function:

  1. virtual void abc()=0;   //pure virtual function.  

Let’s understand this through an example:

  1. #include<iostream>  
  2. using namespace std;  
  3. class Base  
  4. {  
  5.     public:  
  6.     virtual void show()=0;  
  7. };  
  8.   
  9. class Derived:public Base  
  10. {  
  11.     public:  
  12.     void show()  
  13.     {  
  14.        cout<<“Knoobypie”; 
  15.     }  
  16. };  
  17. int main()  
  18. {  
  19.     Base* b;  
  20.     Derived d;  
  21.     b=&d;  
  22.     b->show();  
  23.     return 0;  
  24. }  

Output:

Knoobypie

Top 30 C++ Language Interview Questions And Answers pdf

Check Out latest Posts: