Here is the list of most asked C++ interview questions for beginners as well as experienced programmers.

These interview questions are also for those who want to test viva in their school or college. 

1. What is the use of the static keyword in C++?

When the static keyword used with variables inside a function then its value is preserved between the successive function calls for the lifetime program but cannot be accessible outside the scope or function in which it has been declared.

When the static keyword is used with functions or variables inside a class, that function or variable can be accessed without an object of the class and has access to only static members. Also, all the objects of the class share the same copy of the static members.

2. Struct vs Class in C++

All the members in a structure are public by default whereas in class all the members are private by default.

3. Why main() function is important?

Because the execution of the program starts from the main() function. Without the main function, the program will not execute.

4. When a copy constructor is called?

Whenever an instance of a class is passed by value, the copy constructor is called to create its copy.

This copy of the object is then passed to the called function.

5. Can we overlaod << (Insertion) ans >> (Extraction) operator?
Yes, we can overload insertion as well as extraction operator in C++.

Here is an example, where we have overloaded the insertion operator to print an object.

6. What is a virtual destructor?

When the declaration of the destructor is prefixed with a virtual keyword it becomes a virtual destructor.

It ensures that the derived class destructor is invoked when its object is destroyed.

7. What is a virtual constructor?

C++ does not have virtual constructor.

8. What is namespace in C++?

Namespace in C++ allows to group together the entities like class, function, variables, etc under a common name.

It is a declarative region that provides scope to different identifiers inside it.

Example: std

9. What is :: in C++?

:: is scope resolution operator.

Scope resolution operator resolves to which namespace or scope do the following member belongs.

For example, when we write std::cout, we are accessing the cout that belongs to the scope std.

10. Stream classes that allows file operations in C++?

These are the three streams classes that allow file operations in C++:

  1. fstream
  2. ifstream
  3. ofstream

11. How many times destructor of a class be called for 10 objects?

10 times, because for each object, the destructor is called once, and that too when the object is destroyed.

12. What is include used for?

include in C++ is a preprocessor command. It is used with the library file name or class name.

It directs the preprocessor to insert the code from the specified library into the program code before the compilation begins.

13. What is scope in C++?

Scope refers to a region that defines the visibility of variables or objects.

A scope is created using a set of braces ({}).

14. What is friend function in C++?

A friend function is a function that has access to all public and private members of a class in which it has been declared as a friend using the friend keyword.

15. What is encapsulation?
Encapsulation means wrapping up data and functions into a single unit.

We write functions and variables inside classes in such as way that they work together.

Encapsulation makes program secure and prevents unnecessary information from getting exposed.

For example, writing getter and setter for a variable.

16. What will be the value of ‘i’ and ‘j’ after the execution of given code? Why?

i = 6;
j = i++;

Value of i will equal 7 and j will equal 6, because the variable with post-increment operator is first used then modified.

17. What is ‘std’ in C++?

std is a default namespace provided in C++. In this namespace, several useful objects and functions such as cout, cin, fstream, etc are defined.

18. Difference between ‘new’ and ‘malloc()’ in C++.

malloc()new
Requires size for memory allocation.Requires the number of blocks for memory allocation.
Returns untyped pointer.Returns strictly typed pointer.
Doesn’t call the constructor for class objects.Calls constructor while allocating memory for class objects.
Allocated memory can be deallocated using free().Allocated memory can be deallocated using delete.
Returns a null pointer if the allocation fails.Throws an exception if memory allocation fails.

19. What is the use of the ‘delete’ operator in C++?

The delete operator is used to free up the memory that was allocated to the pointer by the new operator in the heap.

20. What is a destructor in C++?

The destructor like constructor is a special member method that has the same name as the class, does not have any return type, accepts no arguments but is prefixed with a tilde (~) in its declaration.

21. What is dynamic binding?

The decision to call a particular function when happens during the runtime of the program, it is termed as dynamic binding.

Dynamic binding happens when we override functions in our program.

It is also known as late binding.

22. What is a Pure Virtual function?

A virtual function whose declaration ends with =0 and has no body is called a pure virtual function.

Example:

virtual void functionName() =0;

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

A class that contains at least one pure virtual function is called an abstract class.

24. What will be the output of the following code?

int *p, *q, *r;
p = new int(6);
*q= *p;
r = q;
(*p)++;
(*q)++;
(*r)++;
cout << *p <<" ";
cout << *q <<" ";
cout << *r <<" ";
interview question on C++


Because q and r are pointing to the same memory address but different from p.

25. Difference between Abstraction and Encapsulation in C++?

Abstraction means hiding implementation details and showing only important information. For example, defining a class variable as private, so that it can only be accessed through the getter function.

Encapsulation means wrapping up data and functions into a single unit. For example, making function and variable work together.

Also, Encapsulation implements Abstraction.

Leave a Reply