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++ 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.
Reference | Pointer |
---|---|
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.