Below are the common C++ interview questions that cover basic c++ interview questions as well as advanced c++ interview questions. This tutorial also covers the C++ technical interview questions and C++ interview questions for experienced that will help you to prepare for interviews.
Table of Contents
1. What is C++?
C++ is a superset of C programming language that supports both procedural and object-oriented programming language.
2. What are the advantages of C++?
- Portable: It can run on any platform
- Object-oriented programming language: Supports classes, objects, polymorphism, overloading, inheritance, etc
- Data hiding: protects data from invaders
- Message passing: Facilitates communication between objects
3. What are the differences between C and C++?
C | C++ |
---|---|
Developed by Dennis Ritchie | Developed by Bjarne Stroustrup |
Structured programming language | Supports both structured and object-oriented programming language |
It is a subset of C++ | It is a superset of C |
Does not support data hiding | Supports data hiding |
Does not support function overloading or operator overloading | Supports both function and operator overloading |
Does not support reference variables | Supports reference variables |
Uses scanf() and printf() as standard input and output | Uses cin and cout as standard input and output |
Cannot implement functions within a structure | Can implement functions within a structure |
4. What is a class?
A class defines the structure of data. In other words, it also represents a user-defined data type that is specified using the class keyword. It contains functions and members that we can access using the object or instance of a class. For example, we can have an Employee class that has EmpName, EmpId as members, and getName() and setName() as functions. We can create multiple instances for a single class.
5. What is an object?
An object is an instance of a class that has a state and behavior. We can use objects to access functions and members of a class. Whenever we create an object, it allocates memory or storage space depending on how we create it.
6. What are the various features or concepts of OOP?
Below are the features or concepts of OOP:
- Class
- Object
- Inheritance
- Encapsulation
- Abstraction
- Data binding
- Polymorphism
7. What are the different data types in C++?
Below are the different data types in C++:
- Pre-defined data types: int, char, float, and double
- User-defined data types: Arrays, pointers, strings, and structures
8. What are the different types of storage classes in C++?
The different types of storage classes are:
- Automatic
- Register
- Static
- External
- Mutable
9. What are the tokens in C++?
A token is a small part of the program. Below are the different types of token available in C++:
- Strings
- Identifiers
- Keywords
- Constants
- Operators
10. Does C++ have automatic garbage collection?
No, C++ does not support automatic garbage collection.
11. What are the different types of polymorphism in C++?
There are 2 types of polymorphism:
- Compile-time polymorphism: Also called static polymorphism where the call to function is determined at compile time
- Runtime polymorphism: Also called dynamic polymorphism where the call to function is determined at runtime.
12. How is Late Binding implemented in C++?
Using Virtual tables
13. What is namespace in C++?
A namespace is a piece of code that we use to remove the naming conflict and ambiguity in variable names. This is useful when we have the same variable name for different functions. By using the namespace, we can remove this ambiguity. We can define namespace as below:
namespace name { //body }
We can access the variable name using the namespace name as below:
namespace_name :: variable_name;
14. What are the different operations permitted on pointers?
There are 2 different operations that the pointers support:
- Increment pointers – This contains pre-increment and post-increment pointers
- Decrement pointers
15. What is the difference between delete[] and delete?
delete[] releases an array and delete releases a memory
16. What are the different C++ access specifiers?
The different access specifiers in C++ are:
- private: Scope and access within the same class
- public: Scope and access from anywhere
- protected: Scope and access within the same class and child class
17. What is a friend function?
A friend function is a non-member function that helps to access private and protected data. This function is not part of the class but must be declared within the class so that it can access the private data. Generally, non-member functions cannot access private data, but there are situations when we need to do so. In this case, the friend function will be useful.
18. What is a virtual function?
A virtual function is a member function that helps to redefine the implementation in the derived class. When two functions of the same name are present in the base and derived class, the base class function will be declared with the keyword virtual. Hence both classes can have the same function name with a different implementation. During runtime, it determines which function to call based on the object pointer it points to.
19. What is a destructor?
A destructor deletes the resources once the object is out of scope. It has the same name as the class name and is represented by a tilde ‘~’. It does not have any return value or arguments.
20. What is an overflow error?
An overflow error is a type of Arithmetic error that occurs when it requires more space than the actual available space.
21. What is overloading?
Overloading occurs when the same object or function has the same name but a different implementation. There are 2 types of overloading: function overloading and operator overloading.
22. What is function overriding?
Function overriding is the process in which the derived class provides a different implementation of the same function that is present in the base class. The derived class function overrides the base class function.
23. What is virtual inheritance?
Virtual inheritance is a technique in which we can create only one copy of the base class object though it is present in other classes of the hierarchy. This is mainly useful in Multiple inheritance.
24. What is a constructor?
A constructor helps in creating and initializing new objects or values that we can use within a class. A constructor has the same name as the class name.
25. What is a pointer?
A pointer is a variable that stores the address of another variable. It can be used to point to variables of any data type like int or char, etc
26. What is a scope resolution operator?
A scope resolution operator allows defining the member function outside the class. It is represented by ::
27. What is a pure virtual function?
A pure virtual function is a type of virtual function that does not contain any function definition. It is declared with the value 0.
virtual void function_name()=0;
28. What is the difference between a struct and a class?
struct | class |
---|---|
A struct is a user-defined data type that holds values of different data types | A class is a user-defined data types that contains members and functions |
The variables are stored in stack memory | The variables are stored in heap memory |
We cannot initialize variables directly | We can initialize variables directly |
The default access specifier of a variable is public | The default access specifier of a variable is private |
Uses the struct keyword | Uses the class keyword |
Does not support inheritance | Supports inheritance |
Structure type is of value type | Class type is of reference type. |
29. What is a virtual destructor?
A virtual destructor is used to destroy the objects of the derived class from the base class. We can have virtual destructors but not constructors. A virtual destructor is represented by a tilde(~) before the virtual
keyword.
30. Write a C++ program to print Hello world
#include <iostream> using namespace std; int main() { cout<<"Hello World"; return 0; }
31. Write a C++ program to enter a string and find its length
#include<iostream> #include<conio.h> #include<string.h> using namespace std; int main() { char s[20]; int length; cout<< "Enter a string: " <<endl; cin>>s; length = strlen(s); cout<<"The length of the string is: " <<length; return 0; }
Enter a string: tutorial The length of the string is: 8
32. What is this pointer?
this
pointer is a variable that holds the address of the current variable or object.
33. What is the difference between function overloading and operator overloading?
Function overloading is a technique in which the same function has a different type and number of arguments but has the same function name whereas operator overloading uses the same operator which has a different implementation or functionality.
34. What is a static member in C++?
A static member is represented by using the static keyword and allocates memory only once. We cannot declare a static member function as virtual and cannot contain this pointer.
35. What is a reference variable?
A reference variable helps to update or change the value of the original variable. It shares the same memory as the original variable.
36. What is a copy constructor?
A copy constructor is a member function that initializes the object using another object of the same class.
37. Does C++ support String as a primitive data type?
No, C++ does not support String as a primitive data type.
38. What is a diamond problem and where does it occur?
A diamond problem occurs in Multiple inheritance where it is incapable of supporting hybrid inheritance using multiple inheritance and hierarchical inheritance.
39. What is an inline function?
An inline function is a function where the compiler compiles only at the time of calling the function. The function proceeds with the keyword inline.
40. What is the use of a volatile keyword?
A volatile keyword avoids compiler optimization by directing the compiler that the variable can be changed externally.
41. Define pass by value and pass by reference
Pass by value denotes passing a value as a parameter to the function and hence does not affect the original variable whereas pass by reference means we pass the reference of the variable thus changing the original variable value.
42. What is an auto keyword?
An auto keyword refers to local variables by default where the variables are created automatically. These variables are stored in the stack memory.
43. What are the different types of loops in C++?
Below are the different types of loops:
- while loop
- for loop
- do-while loop
44. What is an abstract class?
An abstract class is a class that contains at least one pure virtual function where it contains only the function declaration and does not contain a function definition.
45. What is a default constructor?
A default constructor is a constructor that the compiler automatically creates and does not have any arguments.