Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C++ Interview Questions For Freshers
2.1.
1. What are class and objects in C++?
2.2.
2. What is STL in C++?
2.3.
3. What are destructors in C++?
2.4.
4. What are the different data types present in C++?
2.5.
5. What is the difference between C and C++?
2.6.
6. What is the difference between equal to (==) and assignment operator (=)?
2.7.
7. Why do we use static member variables?
2.8.
8. What is the use of a namespace in C++?
2.9.
9. What is operator overloading in C++?
2.10.
10. What is the difference between reference and pointer in C++?
2.11.
11. What is the difference between call by value and call by reference?
2.12.
12. What is friend function in C++?
2.13.
13. Define Virtual function in C++?
2.14.
14. What is the OOPS concept in C++, and why do we need it?
2.15.
15. How does the class accomplish data hiding in c++?
3.
C++ Interview Questions For Experienced
3.1.
16. What is inheritance in C++?
3.2.
17. Why do we use constructors in C++?
3.3.
18. What is meant by copy constructor in C++?
3.4.
19. What is Encapsulation, and how can it be achieved in C++?
3.5.
20. What are virtual destructors?
3.6.
21. What is an inline function in C++?
3.7.
22. What does a segmentation fault denote in C++?
3.8.
23. How can we access private members of a class in C++?
3.9.
24. What is the difference between global and local variables?
3.10.
25. What is the difference between structure and class in C++?
3.11.
26. What is an overflow error in C++?
3.12.
27. What do you mean by multiple inheritance? What problems can arise with it?
3.13.
28. What exactly is multi-threading in C++?
3.14.
29. What is the difference between virtual functions and pure virtual functions?
3.15.
30. How is memory allocated and deallocated in C++?
3.16.
31. Explain Dangling pointers and memory Leaks?
4.
Frequently Asked Questions
4.1.
How do I prepare for a C++ interview?
4.2.
What is C++ programming interview questions?
4.3.
What should I study for C++ interview?
4.4.
What are the basic C++ rules?
5.
Conclusion
Last Updated: Jun 19, 2024
Easy

C++ Interview Questions and Answers

Author Vikash Kumar
18 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

C++ is one of the most widely used programming languages around the globe.  C++ provides programmers with extensive control over system resources and memory. It is also an object-oriented programming language, which gives programs a clear structure and enables code to be reused, lowering development costs.

C++ Interview Questions

C++ was created as an extension to C language, and the two languages have almost similar syntax. The major difference between C and C++ is that C++ supports classes and objects, whereas C does not. 

C++ is widely used in game development and servers and here are some of the real-world applications of C++ are as follows;

  • GUI based applications
  • Banking applications
  • Distributed systems
  • Database software
  • Operating systems

In this blog, we will discuss some most-asked C++ interview questions. This will help you brush up on your concepts of C++ and prepare for your interviews.

C++ Interview Questions For Freshers

Here, we are going to discuss top C++ interview questions.

1. What are class and objects in C++?

Ans. A class is a blueprint that defines the structure and behavior of objects. An instance of a class is called an object. Since a class is a user-defined data type, an object can be referred to as a variable of that data type. 

While objects are instances of classes that hold particular data and can perform operations specified by the class, classes define the structure and behaviour of objects. 

2. What is STL in C++?

Ans. The STL(Standard Template Library) is a C++ library. It is a powerful set of C++ template classes that provide general-purpose programming data and functions like vectors, lists, queues, and stacks. The STL is highly parameterized in its constituent parts.

STL is made up of three well-structured components, i.e., Algorithms, Containers, and Iterators.

3. What are destructors in C++?

Ans. A destructor is a member function of the class invoked when an object of that class goes out of scope or when the delete expression is used on a pointer to the object of that class. A destructor has the same name as their class name, but it is preceded by a tilde (~). It cannot return any value or accept any parameters.

class Coding {
public:
  // Constructor for class Ninja
  Ninja();
  // Destructor for class Ninja
  ~Ninja();
};

4. What are the different data types present in C++?

Ans.  There are many different data types available in C++, and they can be divided into two groups:

Primitive data types are built-in or predefined data types that the user can use to specify variables immediately.
Data types that are derived from primitive data types or other derived data types are known as derived data types.

Integer, float, character, and bool are examples of primitive data types that only hold single values.
Derived data types, which are built from primitive data types and let you to create complex data structures, include arrays, pointers, structures, and classes.

5. What is the difference between C and C++?

Ans. 

  • While C++ allows both pointers and references, C only supports pointers. A variable that serves as an alias for another variable is called a reference. It can't be moved to a different variable. A variable containing another variable's address is known as a pointer. It could be transferred to a different variable.
     
  • Pointers can be declared as const in C++, which prevents them from having their value modified. This can help to ensure that a pointer's value is not modified mistakenly. Pointers cannot be specified as const in the C language.
     
  • The new and delete operators in C++ allow for dynamic memory allocation and deallocation. This can help with more effective memory management. Memory is statically allocated and deallocated in C.

6. What is the difference between equal to (==) and assignment operator (=)?

Ans. The equal to or (==) operator determines whether two values are identical. If the values are equal, it will return true. It will return false if they are not equal. In contrast, we use the assignment or (=) operator to assign the data on the right to the variable.

7. Why do we use static member variables?

Ans. A static member method can be called even if no objects of the class exist made. It is primarily used to store information that is shared by all objects in a class. A static member variable is used by all objects of a class to transfer data across them. For storing information that is common to all objects, like a counter or a flag, this can be helpful. Even though there are numerous objects in the class, a static member variable is only allocated once. Memory can be saved this way, especially if the variable is large.

8. What is the use of a namespace in C++?

Ans. It is used to group codes logically and to avoid name collisions that can occur when our code contains multiple libraries. At namespace scope, all identifiers are accessible to one another without qualification.

9. What is operator overloading in C++?

Ans. Most of the C++ operators can be redefined or are overloaded using operator overloading. It means operators can be provided with different meanings for a data type as per the user's choice. This functionality is known as operator overloading. 

10. What is the difference between reference and pointer in C++?

Ans.

ReferencePointer
A reference is an alias for a variable that already exists. A pointer is a variable that holds the memory address of another variable.
The reference variable shares the same memory address as the existing variable.Pointer has its own memory address.
It is necessary to initialize it with a value during declaration.Pointer variable can be declared without initializing.
It cannot be assigned a null value.It can be assigned null value.

11. What is the difference between call by value and call by reference?

Ans. Call by value: When calling by value, we send a duplicate of the parameter to the functions. These duplicated values are given a new memory address, and any modifications to these values have no impact on the variable used in the main code.

Call by reference: Here, we give a reference of the variable's address, and it uses that address to find the actual argument that was used to call the function. Changes to the parameter consequently have an effect on the passing argument.

12. What is friend function in C++?

Ans. In C++, a friend function is a function that can access private, protected, and public members of a class. The friend function can be declared within the body of the class using the friend keyword. 

13. Define Virtual function in C++?

Ans. A virtual function in C++ is a member function of a base class that we want to redefine in derived classes to achieve polymorphism. We can declare a virtual function in the base class using the virtual keyword in front of the function.

14. What is the OOPS concept in C++, and why do we need it?

Ans. Object Oriented Programming (OOP) is a programming paradigm that includes many principles such as inheritance, encapsulation, polymorphism, and so on. In earlier programming languages, such as C, we used procedural-oriented programming. This approach was not effective because it had some limitations, such as code that could not be reused in the program. So OOPS provides us with functionalities like code usability and data hiding.

15. How does the class accomplish data hiding in c++?

Ans. In C++, the class can divide data members and functions using three access specifiers: private, protected, and public. Here, private and protected members are hidden from the outside world and thus enable data hiding so that only relevant information is exposed to the user. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

C++ Interview Questions For Experienced

16. What is inheritance in C++?

Ans. Inheritance is a method for reusing and extending preexisting classes without changing them, producing hierarchical relationships. The preexisting class is called the base class or parent class, and the new class which inherits the properties of the base class is called the derived class.

17. Why do we use constructors in C++?

Ans. In C++, constructors are special member functions with the same name as their class. It can be used to initialize values to an object's data members. Whenever any instance of a class is created, it is automatically executed.

18. What is meant by copy constructor in C++?

Ans. A copy constructor in C++ is a unique constructor used to duplicate an existing object of the same class. It is called whenever an object is supplied a value, returned as a value from a function, or initialised as a duplicate of another object. To ensure that the values of the two objects are identical, the copy constructor usually conducts a member-wise copy of the data members of the source object to the newly formed object.

19. What is Encapsulation, and how can it be achieved in C++?

Ans. Encapsulation is the process of combining data members and member functions and bundling them inside a single class to hide sensitive data from end users. It helps to prevent direct data access. Encapsulation can be achieved by making all the data members and functions private. And making getter and setter functions to access it.

20. What are virtual destructors?

Ans. In C++, the parent class uses a virtual destructor to enable the destruction of the object from the derived class as well. Before the constructor, a virtual destructor is declared using the tilde (~) operator and the virtual keyword.

21. What is an inline function in C++?

Ans. We can declare a function as inline in C++. Here the function is copied to the address of the function call at compile time. This may help the program execute faster.

22. What does a segmentation fault denote in C++?

Ans. A segmentation fault happens when your program tries to access memory that it is not allowed to access. In other words, when your program attempts to access memory that is beyond the limits set by the operating system.

23. How can we access private members of a class in C++?

Ans. Private members of the class cannot be accessed by any object or method outside of the class. Only functions within the class or friend functions have access to them. But outside of the class, pointers can be used to access private data members.

24. What is the difference between global and local variables?

Ans. Global variables, such as a session id, are helpful for data that is generally constant or that must be used by several functions in the code. On the other hand, a local variable has a restricted scope. It exists only within the block in which it was declared. When the block ends, the variable is destroyed, and its values are lost.

25. What is the difference between structure and class in C++?

Ans. 

StructureClass
Data members of a structure are public by defaultBy default, data members of a class are private 
Structures don't have the data-hiding capabilityClasses have the data-hiding capability

Structures can only contain data members.

Note: In recent versions, we can include data functions as well.

Classes can contain data members as well as functions
Structures are stored at the stackFor classes, memory is allocated on the heap

26. What is an overflow error in C++?

Ans. An overflow error happens when a code is given a number, value, or variable that it cannot handle. Working with integers or even other numerals is a frequent programming error.

27. What do you mean by multiple inheritance? What problems can arise with it?

Ans. When a child class inherits properties and functionalities from more than one parent class, this type of inheritance is called multiple inheritance.
When a function with the same name is present in more than one parent class, and the child class invokes that function. Here, the compiler gets confused about which member function to invoke. This problem arises when we use multiple inheritance.

Also see, Four Pillars of OOPS

28. What exactly is multi-threading in C++?

Ans. Multitasking refers to the ability of your computer to run two or more programs at the same moment. Multithreading is a more sophisticated type of multitasking.

A multithreaded program consists of two or more components that can operate concurrently. A thread is one of the program's components, and each thread specifies its own execution path.

29. What is the difference between virtual functions and pure virtual functions?

Ans. Virtual Function: A member function of a parent class that can be modified in a derived class is known as a virtual function. It is declared using the keyword virtual.

Pure Virtual Function: A pure virtual function has no implementation and is defined by setting the value to 0. It doesn't have a body. Its only role is to tell the compiler that a function will be empty and pure.

30. How is memory allocated and deallocated in C++?

Ans. Memory is allocated using the new operator in C++, and memory is deallocated using the deletes operator.

Syntax:

int *nums = new int[ 10 ];  // Allocates memory for storing 10 integers
delete [ ] nums;           // Deallocates memory taken by nums

31. Explain Dangling pointers and memory Leaks?

Answer) Pointers pointing to freed memory locations are called Dangling Pointers.

For example: say we have a pointer pointing to a variable x containing a value that is freed later on, but the pointer is still pointing to its memory location, and hence it’s a dangling pointer.

A memory leak occurs when memory locations are not freed, and we cannot refer to the memory location.

Example:

#include<iostream> using namespace std; int main(){ int *ptr = (int*)malloc(sizeof(int)); free(ptr); }

Here, when we make the ‘ptr’ pointer free, it will become a dangling pointer. To avoid it, just set it to NULL.

Frequently Asked Questions

How do I prepare for a C++ interview?

Review fundamental ideas like object-oriented programming, data structures, and algorithms to prepare for a C++ interview. Study sample interview questions, perform coding exercises, and learn about memory management and templates.

What is C++ programming interview questions?

Interview questions for C++ programming test a candidate's understanding of the language's features, data structures, and problem-solving skills. These inquiries could cover topics like pointers, inheritance, templates, and memory management.

What should I study for C++ interview?

Focus on C++ principles, data structures (such as arrays, linked lists), algorithms (such as sorting, searching), object-oriented ideas (such as classes, inheritance), and memory management when preparing for a C++ interview. Use coding challenges to practise problem-solving as well.

What are the basic C++ rules?

The fundamentals of C++ include using the correct syntax, declaring variables before using them, managing memory (preventing memory leaks), comprehending data types, and abiding by scope constraints (for local and global variables). Observe other object-oriented principles like inheritance and encapsulation.

Conclusion

In this article, we discussed the most-asked C++ interview questions. Hope, from these C++ interview questions, you got a clear idea of the types of questions asked in interviews. Review fundamental ideas like object-oriented programming, data structures, and algorithms to get ready for a C++ interview. 

Check out other related articles to learn more: 

To learn more about Data Structures and Algorithms, you can enroll in our DSA in C++ Course.

Happy Coding!

Live masterclass