C++ Interview Questions

Here are the top 50 commonly asked questions in C++ interviews. Whether you’re just starting your preparation or need a quick refresher, these questions and answers will help you tackle your interview with confidence.

Basic C++ Interview Questions with Answers

1. What is C++?

C++ is a general purpose, high level language. It is an extension to the famous C language. It supports both object-oriented and procedural programming.

2. Which programming paradigm does C++ use?

C++ is a multiparadigm language. It uses generic, imperative, procedural and object-oriented paradigms.

3. Who developed C++?

Bjarne Stroustrup developed C++ at Bell Labs in 1979 as an extension to C language which was also developed by him.

4. What are the applications of C++?

C++ is a very powerful and fast language. It can be used to develop operating systems, desktop applications, gaming applications, compilers, browsers, graphical user interfaces and many other types of software applications.

5. Is C++ compiled or interpreted?

C++ is a compiled language. This means a code written in C++ first needs to be compiled to get object code. This object code is converted to executable code by a linker then the program can be executed.

6. What are the advantages of C++?

C++ is a powerful and fast multiparadigm programming language. It allows generic, imperative, procedural and object-oriented styles of programming. It gives some control of memory management to the user.

advertisement
advertisement

7. What are the disadvantages of C++?

Absence of garbage collector is a major disadvantage of C++. Even though C++ is an object-oriented language, features like global variables, friend functions etc. raise security concerns.

8. How many header files are present in the Standard C++ Library?

There are 2 types of header files in C++: Pre-existing and User-defined. There are 49 pre-existing header files in the Standard C++ Library. Out of these, 19 header files are equivalents of 18 header files in standard C library.

9. Why is C++ sometimes referred to as a ‘middle-level language’?

Since C++ (a high-level language) is an extension to C, which is closely related to the machine language, C++ allows low-level manipulation of data at a certain level, thus, it is sometimes referred to as a ‘middle-level language’.

10. What are the major differences between C and C++?

C++ is a superset of C language. This means C++ has all the functionality of C and much more. C is a procedural programming language whereas C++ can perform both procedural and object-oriented programming.

11. Why does C++ use namespace?

Namespace is a feature of C++ that allows organizing the elements of programs into different logical scopes. Without namespace, multiple identifiers with same name may clash. Std is the default namespace standard used in C++.

12. How many access specifiers are there in C++?

C++ has 3 access specifiers. They are Public, Protected and Private. Access specifiers are used to allow or restrict the access of data to certain parts of the program.

13. Which datatypes are supported in C++?

3 types of datatypes are supported in C++: Primary, Derived and User-defined. Primary datatypes supported in C++ are integer, character, floating point, double floating point, Boolean and void.

14. Name some features of C++.

C++ has many features like Polymorphism, inheritance, data abstraction, encapsulation, memory management, modularity, templates and exception handling etc.

15. Why was C++ created?

C++ was created to enhance the performance of and overcome the disadvantages of C language. This idea was to draw inspiration from and pick the best features from existing languages and come up with the most versatile language.

advertisement

16. Which storage classes are supported in C++?

There are 5 storage classes supported in C++. They are auto, static, extern, register and mutable.

17. Does C++ support exception handling?

Exceptions are issues that a program encounters during runtime. C++ supports exception handling at language level using ‘try’, ‘throw’ and ‘catch’ keywords.

18. Does C++ allow dynamic memory allocation?

In C++ we can dynamically allocate and deallocate memory using the ‘new’ and ‘delete’ operators respectively.

19. When does C++ perform stack allocation?

Stack allocation is done when memory has to be allocated before runtime. The compiler automatically deallocates the memory allocated on the stack.

20. When does C++ perform heap allocation?

C++ performs heap allocation when memory has to be allocated during runtime. Memory allocated on the heap has to be manually deallocated by the user. The ‘new’ and ‘delete’ operators are used for allocation and deallocation on the stack respectively.

advertisement

Intermediate C++ Interview Questions with Answers

21. What is a constructor in C++?

A constructor is a special type of function in a class that gets called immediately after an object of the class is created. The name of the constructor is same as that of its class. It is generally used to initialize data members of the object.

22. What is a destructor in C++?

A destructor is a special type of function in a class that gets called immediately after an object of the class goes out of scope. The name of the destructor is same as that of its class followed by tilde symbol (~).

23. Can we override constructors in C++?

No, constructors of base class in C++ cannot be overridden by any member function in child class.

24. Can we overload destructors in C++?

Destructors cannot be overloaded in C++. This is because destructors don’t take any parameters and thus cannot take any other form.

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

Copy constructor is a type of constructor that initializes the data members of an object using the values stored in data members of another. In other words, it makes a copy of an object.

26. What are the different types of inheritance in C++?

Types of inheritance in C++ are Single Inheritance, Multiple Inheritance, Multilevel Inheritance and Hybrid Inheritance.

27. What is meant by multiple inheritance?

A class in C++ can inherit data members and functions from existing classes. The feature in C++ that allows a class to inherit from 2 or more classes is called multiple inheritance.

28. Do derived classes inherit constructors from base class in C++?

Like other member functions, constructors are also inherited from base class to child class. Even if a child class inherits from base class in private mode the constructor of base class is still called when an object of child class is created.

29. What is polymorphism in C++?

The ability of a function or an object to behave differently under different situations is known as polymorphism. Function overloading and operator overloading are examples of polymorphism in C++.

30. What is function overloading in C++?

The feature in C++ wherein functions can have same name but multiple definitions such that the functionality is essentially the same but they serve in different situations. For example, Area() function to calculate area of square can be overloaded to calculate area of circles.

31. What is function overriding in C++?

Function Overriding is a feature of C++ that allows us to recreate a function of base class under derived class with little difference in functionality. To override a function in the base class, the function in derived class must have the same parameter list and return type.

32. What is data abstraction in C++?

The feature in C++ that allows to show only necessary information while hiding the background details is known as data abstraction. C++ implements data abstraction using access specifiers in classes.

33. How many access specifiers are there in C++?

C++ has 3 access specifiers. They are Public, Protected and Private. Access specifiers are used to allow or restrict the access of data to certain parts of the program.

34. What is meant by an access specifier in C++?

Access specifiers define from where the members of a class can be accessed. They are used to implement the important feature of data abstraction in C++. They allow or restrict access of members of a class to functions outside the class.

35. What are private members in C++ classes?

Only member functions of the same class can access private members. A private member cannot be accessed from anywhere outside the class, not even inherited classes.

36. Can data members of a class be accessed by static member functions in C++ classes?

Static member functions can only access other static data members and static member functions of its class.

37. What are protected members in C++ classes?

Protected members are those members of the class that cannot be accessed from anywhere outside the class except by child classes. Member functions and friend functions of the same class can also freely access protected members.

38. Can friend functions access private members of a class in C++?

A friend function can access all the members of the class to which it is declared as friends whether they are public, protected or private. A friend function can be declared as friend to multiple classes. It can be called without using any objects of the class.

39. What is an abstract class in C++?

An abstract class in C++ is a class that has at least 1 pure virtual function as a member function. A child class that is derived from an abstract base class will also be abstract unless all pure virtual functions are overridden in the derived class.

40. What are static members in C++?

Static members are those members of a class that are same for all the objects of a class. This means all the objects of a class share the same static members.

C++ Programming Interview Questions with Answers

41. What will be the output of the following C++ program?

#include<bits/stdc++.h>
using namespace std;
 
void recurser(int n){
    if(n==0)
        cout<<n;
    else{
        recurser(n-1);
        cout<<n;
    }
}
int main()
{
	recurser(5);
	return(0);
}

Answer: 012345

Explanation:
The recurser() function calls itself with parameter n-1 before and then prints the value of n, thus the numbers are printed in ascending order. n=0 is the base case of recurser() function.

Order of function calls will be:
Recurser(5) -calls-> Recurser(4) -calls-> Recurser(3) -calls-> Recurser(2) -calls-> Recurser(1) -calls-> Recurser(0) This is base case -returns to-> Recurser(1) -returns to-> Recurser(2) -returns to-> Recurser(3) -returns to-> Recurser(4) -returns to-> Recurser(5)

42. What will be the output of the following C++ program?

#include<bits/stdc++.h>
using namespace std;
 
void func(int &n){
    n=n%10;
}
int main()
{
	int x=25;
	func(x);
	cout<<x;
	return(0);
}

Answer: 5

Explanation:

  • The value of variable x changed because it was passed as reference to function func() where it’s value was altered.
  • Passing by reference means that instead of copying the value of passed parameter to a new variable, the address of the parameter was passed to the function. This means any changes made to the variable in the function will reflect at the previous address of the variable.

43. What will be the output of the following C++ program?

#include<bits/stdc++.h>
using namespace std;
 
class A{
    int a[5];     
};
class B: public A{ };
class C: public A{ };
class D: public B, public C{};
 
int main(void)
{ 
  cout<<sizeof(D);
  getchar();
  return 0;
}

Answer: 40

Explanation:

  • Size of class A is 5 * size of int = 5 * 4 = 20. Since Class B has no element of its own and only inherited elements of A thus size of B is equal to size of A. Similarly, size of C is also equal to size of A.
  • Now, class D is derived from both classes B and C and has no elements of its own.
  • Thus, size of A = size of B + size of C = 20 + 20 = 40. Note here that A has inherited a[5] twice.

44. What will be the output of the following C++ program?

#include <iostream>
using namespace std;
 
class Class1{  
    public:
    Class1(){
        cout << "Class1" << endl;
    }
};
class Class2 : public Class1{
    public:
    Class2(){
        cout << "Class2" << endl;
    }
};
 
int main() {
    Class2 c;
    return 0;
}

Answer:

Class1
Class2

Explanation:

  • When a class inherits from another class, the constructor of the base class is always called first followed by constructor of derived class when an object of derived class is created.
  • In case a class inherits from multiple classes then constructors of all the parent classes are called first in the order of inheritance and then the constructor of child class is called when an object of child class is created.

45. Will the following C++ program produce any output?

#include<iostream>
using namespace std;
 
int main() { 
   const int x = 5;
   x+=10; 
   cout<<x; 
 
   return 0;
}

Answer:

The given code will give an error.

Explanation: The given code gives an error because integer x has been defined as a constant. This means once we have initialized it, we cannot change its value throughout the code. Trying to do so will give a compilation error.

46. What will be the output of the following C++ program?

#include<iostream>
using namespace std;
 
int main() { 
   switch(2){
       case 1: cout<<1;
       case 2: cout<<2;
       case 3: cout<<3;
   }
   return 0;
}

Answer: 23

Explanation: ‘break’ statement hasn’t been called in all cases. Thus, when switch statement calls 2, the code jumps to case 2 however after completing case 2 since there is no break statement case 3 block also gets executed.

47. Will the following C++ program produce any output?

#include<iostream>
using namespace std;
 
int main() { 
   int x = 20;
   float y = x;
   cout<<y;
   return 0;
}

Answer: 20

Explanation:

  • While assigning the value of integer x to floating point y, the compiler converts the value of integer x to a floating-point number and then assigns it to y. This process is called typecasting.
  • Typecasting is done whenever 2 variables that have different datatypes have to be operated or compared with each other.

48. What will be the output of the following C++ program?

#include<iostream>
using namespace std;
 
int main() { 
   int x = 20;
   float y = 45.5;
   cout<<x+y;
   return 0;
}

Answer: 65.5

Explanation:

  • While adding an integer and a floating-point number, the compiler typecasts the integer to a floating point so as to avoid loss of information and then calculates the sum.
  • Whenever 2 variables of different datatype are operated with each other, the compiler tries to typecast 1 so that they have the same datatype such that no data is lost. 2 variables cannot be operated upon together if they don’t have the same datatype.

49. What will be the output of the following C++ program?

#include<iostream>
using namespace std;
 
int main()
{
	if(1=='1')
	   cout<<"True";
	else
	   cout<<"False";
	return 0;
}

Answer: False

Explanation:

  • Character ‘1’ and integer 1 are not the same in C++. When character ‘1’ is equated with integer 1, the compiler tries to make both of the same datatype. Thus, it converts character to integer by giving it the character’s ASCII value.
  • Since the ASCII value of ‘1’ is not equal to 1, the output produced is False.

50. Will the following C++ program produce any output?

#include<iostream>
using namespace std;
 
class A {};
class B {};
int main()
{
	A obj1;
	B obj2;
	obj1 = obj2;
	return 0;
}

Answer: The given code will give an error.

Explanation: The given code gives an error because 2 objects of different classes cannot be compared or operated with each other even if they have the same list of data members or no data members at all.

Useful Resources:

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.