TOP 20 INTERVIEW QUESTIONS FOR CRACKING C++

Bjarne Stroustrup of Bell Labs created C++, a powerful and all-purpose programming language. This language is a C extension and one of the fastest object-oriented programming languages available. C++ is extremely popular due to its speed and compatibility.

C++ is widely used in the development of games and servers, with the following examples of real-world applications.

System software
graphical user interface applications
Distributed computing
Banking applications and database software
Graphics and advanced computations
Embedded software

Any company’s or organization’s selection process must include interview questions. To face the interviewer, you must be completely prepared for the questions. In this C++ interview questions tutorial, you’ll learn how to answer the most common C++ interview questions.


You’ll go over some conceptual questions, multiple-choice questions, output-based questions, and programming questions in this C++ interview questions tutorial.

1. Define inheritance.

 

The process of creating new classes, known as derived classes, from existing classes is called inheritance. Base classes refer to the existing classes. Derived classes inherit all of the capabilities of the base class, but they can also add their features and refinements.

 

The properties of Class Vehicle are passed down to Class Bus, Class Car, and Class Truck.

 

The most important aspect of inheritance is that it allows for code reuse.

 

 

2. If class D is derived from a base class B. When creating an object of type D in what order would the constructors of these classes get called?

 

A base part and a derived part make up the derived class. When C++ builds derived objects, it does so in stages. The most-base class (at the very top of the inheritance tree) is created first. Then, in order, each child class is built until the most-child class is built last.

 

As a result, the function Object() { [native code] } of class B will be called first, followed by the function Object() { [native code] } of class D.

 

During the destruction, the order is strictly reversed. Destructors begin at the most derived class and work their way down to the base class.

As a result, the destructor of class D will be called first, followed by the destructor of class B.

 

 

3. Can a function Object() { [native code] } call a virtual function?

 

Yes, a function Object() { [native code] } can call a virtual function. However, in this case, the behaviour is a little different. The virtual call is resolved at runtime when a virtual function is called. It is always the current class’s member function that is called. That is, the function Object() { [native code] } does not support the virtual machine.

 

For instance-

				
					class base{
 private:
  int value;
 public:
  base(int x){
   value=x;
  }
  virtual void fun(){
   
  }
}

class derived{
 private:
  int a;
 public:
  derived(int x, int y):base(x){
   base *b;
   b=this;
   b->fun();      //calls derived::fun()
  }
  void fun(){
   cout<<”fun inside derived class”<<endl;
  }
}



				
			

 

4. What is the purpose of a copy function Object() { [native code] }?

 

A copy function Object() { [native code] } is a member function that uses another object of the same class to initialise an object.

				
					class A{
int x,y;
A(int x, int y){
 this->x=x;
 this->y=y;
}

};
int main(){
A a1(2,3);
A a2=a1;     //default copy constructor is called
return 0;
}


				
			

 5. What makes virtual functions different from pure virtual functions?

A virtual function is a base class member function that is redefined in a derived class. The virtual keyword is used to declare it.

 

				
					lass base{
public:
 virtual void fun(){

 }
};

				
			

A pure virtual function has no implementation and is declared by setting the value to 0. It has no physical form.

				
					class base{
public:
 virtual void fun()=0;
};

				
			

Here, = sign has got nothing to do with the assignment, and value 0 is not assigned to anything. It is used to simply tell the compiler that a function will be pure and it will not have anybody.

 

 

6. What is this pointer in C++?

The member functions of every object have a pointer named this, which points to the object itself. The value of this is set to the address of the object for which it is called. It can be used to access the data in the object it points to.

				
					class A{
 private:
  int value;
 public:
  void setvalue(int x){
   this->value=x; 
  }
};

int main(){
 A a;
 a.setvalue(5);
 return 0;
}

				
			

7. What exactly is C++?

 

Bjarne Stroustrup created C++, an object-oriented programming language. In 1985, it was released.C++ is a superset of C, with the exception of classes, which are present in C.

Stroustrup dubbed the new language “C with Classes” at first. After some time, however, the name was changed to C++. C++ was inspired by the C increment operator ++.



8. What are the benefits of using C++?

 

C++ preserves all aspects of the C language while also simplifying memory management and adding features such as

 

  • C++ is a highly portable language, which means that software written in it can run on any platform.

 

  • Classes, objects, inheritance, polymorphism, and abstraction are all concepts in C++, which is an object-oriented programming language.

 

  • Inheritance is a concept in C++. Through inheritance, redundant code can be removed and existing classes can be reused.

 

  • Data hiding assists programmers in creating secure programmes that are not vulnerable to intruders.

 

  • Message passing is a communication method used between objects.

The C++ function library is extensive.

 

 

9. What is visual C++, exactly?


Visual C++ is a product that implements the C++ standard. C++ is a standardised language. Visual C++ allows you to write portable C++ programmes, but you can also use Microsoft-only extensions, which reduce portability but increase productivity.

 


10. How do I fix a C++ segmentation fault?

 

A segmentation fault indicates memory corruption due to an error. When a piece of code tries to perform read and write operations in a read-only memory location. The following are the causes and remedies for segmentation errors:

				
					
Reason: Accessing an address that is freed
int* p = malloc(8);
*p = 100;
       
 free(p);   	
 *p = 110;
       
Solution: Before freeing the pointer check the assignment or any operation required to perform.
 
Reason: Accessing out of array index bounds
int arr[2];
arr[3] = 10; 
Solution: Correcting the array bound
 
Reason: Improper use of scanf()
int n = 2;
scanf("%d",n);
Solution: To avoid this is the only solution
 
Reason: Dereferencing uninitialized pointer
int *p;
printf("%d",*p);
Solution: A pointer must point to valid memory before accessing it.
 
Reason: Stack Overflow
Solution: It can be resolved by having a base condition to return from the recursive function.


				
			

 

 

11. With an example, what is stl in C++?

STL stands for Standard Template Library and is a library in C++. STL is a generalised library that includes data structures/container classes, functions, algorithms, and iterators for programming. STL consists of four parts.

– Algorithms: These include binary search, merge sort, and other search and sorting algorithms.

– Containers include vectors, lists, queues, arrays, maps, and so on.

– Functions: These are objects that perform the functions of functions.

Iterators are objects that allow you to traverse through the elements of a container, such as vector::iterator.

 

 

12. What does flush mean in C++?

 

std::flush synchronises the stream buffer with the output sequence it controls.

 

 

 

13.  In C++, how do you initialise a 2d vector?

 

The following is the syntax for initialising a 2d vector:

 

std::vector<std::vector<int> > name_of_vector;

For example: std::vector<std::vector<int> > v { { 1, 2, 1 },

{ 2, 6, 7 } };

 

14. How to set precision in C++?

 

Precision in C++ can be set by using the following functions- floor(), ceil(), round(), trunc() and setprecision(). Using setprecision() in a program.

				
					#include<bits/stdc++.h>
using namespace std;
   
void main()
{
    double p = 8.04149;
    cout << fixed << setprecision(2) << pi<<endl;
}
 
The output will be 8.04.

				
			

 

15. What is this pointer in C++?


Using this pointer, every object is provided access to its own address. It is an implicit parameter to all member functions. Only member functions have this pointer and not a friend function.

				
					#include<iostream>
using namespace std;
   
class sample
{
private:
   int a;
public:
   void set_var (int a)
   {
           this->a = a;
   }
   void print()
     {
       cout << x ;
     }
};
   
int main()
{
   sample obj;
   int a = 2;
   obj.set_var(a);
   obj.print();
   return 0;
}

				
			

16. How to initialize a vector in C++?

There are multiple ways to do it:

You can do it like arrays:

vector value{ 11, 22, 33 };

or by pushing values one by one:

vector value;

value.push_back(11);

value.push_back(22);

value.push_back(33);

value.push_back(44);

value.push_back(55);

 
 

17.  What exactly is a class?

A user-defined data type is a class. The keyword class is used to declare the class. The data members and member functions are contained in the class, and their access is controlled by the three modifiers: private, public, and protected. The type definition of the category of things is defined by the class. It defines a datatype, but not the data; rather, it specifies the data structure.

A class can produce an unlimited number of objects.

 

 

18. In C++, what are the various types of polymorphism?

Polymorphism: Multiple forms are known as polymorphism. It means that there are multiple functions with the same name but different functionalities.

There are two types of polymorphism:

Polymorphism at runtime

Dynamic polymorphism is another name for it. Runtime polymorphism is an example of function overriding. When a child class contains a method that is already present in the parent class, this is known as function overriding. As a result, the child class overrides the parent class’s method. When a parent and child class override a function, both contain the same function with a different definition. Runtime polymorphism is when the call to the function is determined at runtime.

 

Let’s understand this through an example:

  1. #include   

  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<<“javaTpoint tutorial”;  

  17.     }  

  18. };  

  19.   

  20. int main()  

  21. {  

  22.     Base* b;  

  23.     Derived d;  

  24.     b=&d;  

  25.     b->show();  

  26.                 return 0;  

  27. }  

     

 
  • 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   

  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;  

19. Define the friend function

The Friend function acts as a class friend. It has access to the class’s private and protected members. Although the friend function is not a class member, it must be listed in the class definition. The non-member function has no access to the class’s private data. The non-member function is sometimes required to access the data. The friend function is a non-member function that has access to the class’s private information.

 

20. What is a  class template?

To create a family of classes and functions, a class template is used. For example, we can create an array class template that allows us to create arrays of various types such as int, float, char, and so on. Similarly, if we have a function add(), we can create multiple versions of add() by creating a template for it ().

 

The syntax of a class template:

  1. template<class T>  

  2. class classname  

  3. {  

  4.   // body of class;  

  5. };  

Syntax of a object of a template class:

  1. classname<type> objectname(arglist);  

This is the end of our topic. We hope we helped you in some way. The topics covered in this Core C++  Interview Questions blog are among the most sought-after skillsets for a  C++ professional, according to recruiters. These C++ Interview Questions will undoubtedly assist you in acing your job interview.


Best wishes for your interview!


If you still have any queries, please let us know in the comment section.