Sunday, April 18, 2021

C++ Interview Questions

                                 C++ Interview Questions

    C++ Programming Language Interview Questions

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.
  • Message passing is a technique used for communication between the objects.
  • C++ contains a rich function library.

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

You can create N number of objects from a class.


6) What are the various OOPs concepts in C++?

The various OOPS concepts in C++ are:

C++ Interview Questions

  • Class:

The class is a user-defined data type which defines its properties and its functions. For example, Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions. The class does not occupy any memory space. Therefore, we can say that the class is the only logical representation of the data.

The syntax of declaring the class:

  1. class student  
  2. {  
  3. //data members;  
  4. //Member functions  
  5. }  
  • Object:

An object is a run-time entity. An object is the instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. The class does not occupy any memory space. When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory. When an object is created without a new keyword, then space is not allocated in the heap memory, and the object contains the null value in the stack.

  1. class Student  
  2. {  
  3. //data members;  
  4. //Member functions  
  5. }  

The syntax for declaring the object:

  1. Student s = new Student();  
  • Inheritance:

Inheritance provides reusability. Reusability means that one can use the functionalities of the existing class. It eliminates the redundancy of code. Inheritance is a technique of deriving a new class from the old class. The old class is known as the base class, and the new class is known as derived class.

Syntax

  1. class derived_class :: visibility-mode base_class;   

Note: The visibility-mode can be public, private, protected.

  • Encapsulation:

Encapsulation is a technique of wrapping the data members and member functions in a single unit. It binds the data within a class, and no outside method can access the data. If the data member is private, then the member function can only access the data.

  • Abstraction:

Abstraction is a technique of showing only essential details without representing the implementation details. If the members are defined with a public keyword, then the members are accessible outside also. If the members are defined with a private keyword, then the members are not accessible by the outside methods.

  • Data binding:

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.

  • Polymorphism:

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

  1. Static polymorphism is also known as early binding.
  2. Dynamic polymorphism is also known as late binding.

7) What are the different types of polymorphism in C++?

Polymorphism: Polymorphism means multiple forms. It means having more than one function with the same function name but with different functionalities.

Polymorphism is of two types:

C++ Interview Questions

  • Runtime polymorphism

Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child class both contains the same function with the different definition. The call to the function is determined at runtime is known as runtime polymorphism.

Let's understand this through an example:

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

Output:

find technical question
  • Compile time polymorphism

Compile-time polymorphism is also known as static polymorphism. The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism.

Method overloading: Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.

Method overloading can be possible on the following basis:

  • The return type of the overloaded function.
  • The type of the parameters passed to the function.
  • The number of parameters passed to the function.

Let's understand this through an example:

  1. #include <iostream>  
  2. using namespace std;  
  3. class Multiply  
  4. {  
  5.    public:  
  6.    int mul(int a,int b)  
  7.    {  
  8.        return(a*b);  
  9.    }  
  10.    int mul(int a,int b,int c)  
  11.    {  
  12.        return(a*b*c);  
  13.   }  
  14.  };  
  15. int main()  
  16. {  
  17.     Multiply multi;  
  18.     int res1,res2;  
  19.     res1=multi.mul(2,3);  
  20.     res2=multi.mul(2,3,4);  
  21.     cout<<"\n";  
  22.     cout<<res1;  
  23.     cout<<"\n";  
  24.     cout<<res2;  
  25.     return 0;  
  26. }  

Output:

6
24
  • In the above example, mul() is an overloaded function with the different number of parameters.

8) Define namespace in C++.

  • The namespace is a logical division of the code which is designed to stop the naming conflict.
  • The namespace defines the scope where the identifiers such as variables, class, functions are declared.
  • The main purpose of using namespace in C++ is to remove the ambiguity. Ambiquity occurs when the different task occurs with the same name.
  • For example: if there are two functions exist with the same name such as add(). In order to prevent this ambiguity, the namespace is used. Functions are declared in different namespaces.
  • C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement "using namespace std;" includes the namespace "std" in our program.
  • Syntax of namespace:
  1. namespace namespace_name  
  2. {  
  3.  //body of namespace;  
  4. }  

Syntax of accessing the namespace variable:

  1. namespace_name::member_name;  

Let's understand this through an example:

  1. #include <iostream>  
  2. using namespace std;  
  3. namespace addition  
  4. {  
  5.     int a=5;  
  6.     int b=5;  
  7.     int add()  
  8.     {  
  9.         return(a+b);  
  10.     }  
  11. }  
  12.   
  13. int main() {  
  14. int result;  
  15. result=addition::add();  
  16. cout<<result;  
  17. return 0;  
  18. }  

Output:

10

9) Define token in C++.

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


10) Who was the creator of C++?

Bjarne Stroustrup.


11) Which operations are permitted on pointers?

Following are the operations that can be performed on pointers:

  • Incrementing or decrementing a pointer: Incrementing a pointer means that we can increment the pointer by the size of a data type to which it points.

There are two types of increment pointers:

1. Pre-increment pointer: The pre-increment operator increments the operand by 1, and the value of the expression becomes the resulting value of the incremented. Suppose ptr is a pointer then pre-increment pointer is represented as ++ptr.

Let's understand this through an example:

  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. int a[5]={1,2,3,4,5};  
  6. int *ptr;  
  7. ptr=&a[0];  
  8. cout<<"Value of *ptr is : "<<*ptr<<"\n";  
  9. cout<<"Value of *++ptr : "<<*++ptr;  
  10. return 0;  
  11. }  

Output:

Value of *ptr is : 1
Value of *++ptr : 2

2. Post-increment pointer: The post-increment operator increments the operand by 1, but the value of the expression will be the value of the operand prior to the incremented value of the operand. Suppose ptr is a pointer then post-increment pointer is represented as ptr++.

Let's understand this through an example:

  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. int a[5]={1,2,3,4,5};  
  6. int *ptr;  
  7. ptr=&a[0];  
  8. cout<<"Value of *ptr is : "<<*ptr<<"\n";  
  9. cout<<"Value of *ptr++ : "<<*ptr++;  
  10. return 0;  
  11. }  

Output:

Value of *ptr is : 1
Value of *ptr++ : 1
  • Subtracting a pointer from another pointer: When two pointers pointing to the members of an array are subtracted, then the number of elements present between the two members are returned.

12) Define 'std'.

Std is the default namespace standard used in C++.


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

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.

0 Comments:

Post a Comment

Popular Posts

Recent Posts

Unordered List

Text Widget