1. What is using namespace std in C++?
Using namespace std in C++ tells the compiler that you will be making use of the namespace called ‘std’. The ‘std’ namespace contains all the features of the standard library. You need to put this statement at the start of all your C++ codes if you don’t want to keep on writing std:: infront of every variable/string or whatever standard library feature you are making use of, as it becomes tedious to do so.
2. What are the four pillars of OOP?
The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction.
3. What is encapsulation?
Encapsulation is the process of bundling data and functions into a single unit called a class. It allows data hiding and provides a way to control access to the data.
4. What is polymorphism?
Polymorphism is the ability of objects of different classes to respond to the same method call. It allows different objects to be treated as instances of a common base class, providing flexibility and code reusability.
5. What is abstraction?
Abstraction is the process of simplifying complex systems by breaking them down into smaller, more manageable units. In C++, this is often achieved using classes and interfaces.
6. What is the difference between a class and an object?
A class is a blueprint or template, while an object is an instance of that class. A class defines the properties and behaviors, whereas an object represents a specific instance with values assigned to those properties.
7. What is a constructor?
A constructor is a special member function of a class that is automatically called when an object of that class is created. It initializes the object’s data members and sets its initial state.
8. What is type casting in C++?
Type casting in C is used to change the data type. They are of two types: Implicit Type Conversion: It is automatic. Explicit Type Conversion: It is user-defined.
9. What is function overloading?
Function overloading is a feature that allows multiple functions with the same name but different parameters to be defined. The appropriate function is selected based on the number and types of arguments passed.
10. What is operator overloading?
Operator overloading allows operators to be used with user-defined types. It enables you to define how operators should behave when applied to objects of a class.
11. What is the difference between structure and class in C++?
The difference between structure and class is as follows:
– By default, the data members of the class are private whereas data members of structure are public.
– While implementing inheritance, the access specifier for struct is public whereas for class its private.
– Structures do not have data hiding features whereas class does.
– Structures contain only data members whereas class contains data members as well as member functions.
– In structure, data members are not initialized with a value whereas in class, data members can be initialised.
– Structures are stored as stack in memory whereas class is stored as heap in memory.
12. What is the Standard Template Library (STL)?
The STL is a library in C++ that provides a set of generic algorithms and data structures. It includes containers (like vectors, lists, and maps), algorithms (such as sorting and searching), and iterators for accessing container elements.
13. What is the difference between a pointer and a reference?
A pointer is a variable that holds the memory address of another variable, while a reference is an alias for an existing variable. Pointers can be reassigned, whereas references are bound to a specific variable.
14. What is the keyword ‘const’ used for?
The ‘const’ keyword in C++ is used to declare constants or to indicate that a variable or member function does not modify the object it belongs to.
15. What is an exception in C++?
Runtime abnormal conditions that occur in the program are called exceptions. These are of 2 types:
– Synchronous
– Asynchronous
C++ has 3 specific keywords for handling these exceptions:
– try
– catch
– throw
16. What is the difference between C++ and Java?
This is one of the most common c++ interview questions asked, the difference between c++ and java are as follows:
– C++ supports goto statements whereas Java does not.
– C++ is majorly used in system programming whereas Java is majorly used in application programming.
– C++ supports multiple inheritance whereas Java does not support multiple inheritance
– C++ supports operator overloading whereas Java does not support operator overloading.
– C++ has pointers which can be used in the program whereas Java has pointers but internally.
– C++ uses a compiler only whereas Java uses both compiler and interpreter.
– C++ has both call by value and call by reference whereas Java supports only call by value.
– C++ supports structures and joins whereas Java does not support structure and joins
– Java supports unsigned right shift operator (>>>) whereas C++ does not.
– C++ is interactive with hardware whereas Java is not that interactive with hardware.
17. What is inline function in C++?
Inline functions are functions used to increase the execution time of a program. Basically, if a function is inline, the compiler puts the function code wherever the function is used during compile time. The syntax for the same is as follows:
inline return_type function_name(argument list) {
//block of code
}
18. What are the different data types present in C++?

19. Explain what is the use of void main () in C++ language?
To run the C++ application it involves two steps, the first step is a compilation where conversion of C++ code to object code take place. While second step includes linking, where combining of object code from the programmer and from libraries takes place. This function is operated by main () in C++ language.
20. What is function in C++?
A function in C++ is a group of statements that together perform a specific task. Every C/C++ program has at least one function that the name is main.

21. What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it is declared as friends.
Like friend class, friend function can also access private, protected, and public members. But, Friend functions are not member functions.
For example –
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
}
Here we can access the private data of class A and class B.
22. What are the C++ access specifiers?
In C++ there are the following access specifiers:
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the derived class.
Private: All data members and member functions are not accessible outside the class.
23. What are the static members and static member functions?
When a variable in a class is declared static, space for it is allocated for the lifetime of the program. No matter how many objects of that class have been created, there is only one copy of the static member. So same static member can be accessed by all the objects of that class.
A static member function can be called even if no objects of the class exist and the static function are accessed using only the class name and the scope resolution operator ::
24. What is a copy constructor?
A copy constructor is a member function that initializes an object using another object of the same class.
Example-
class A{
int x,y;
A(int x, int y){
this->x=x;
this->y=y;
}
};
int main(){
A a1(2,3);
A a2=a1; //default copy constructor is called
return 0;
}
25. If class D is derived from a base class B. When creating an object of type D in what order would the constructors of these classes get called?
The derived class has two parts, a base part, and a derived part. When C++ constructs derived objects, it does so in phases. First, the most-base class(at the top of the inheritance tree) is constructed. Then each child class is constructed in order until the most-child class is constructed last.
So the first Constructor of class B will be called and then the constructor of class D will be called.
During the destruction exactly reverse order is followed. That is destructor starts at the most-derived class and works its way down to base class.
So the first destructor of class D will be called and then the destructor of class B will be called.
26. What are void pointers?
A void pointer is a pointer which is having no datatype associated with it. It can hold addresses of any type.
For example-
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
We can assign a pointer of any type to a void pointer but the reverse is not true unless you typecast it as
str=(char*) ptr;
27. What is a header file in C++?
A header file in C++ contains the declarations of functions, classes, and other objects that can be used in multiple source files. It is typically included using the ‘#include’ directive.
27. What is the difference between public, private, and protected access specifiers?
Public members can be accessed from anywhere, private members are only accessible within the class, and protected members are accessible within the class and its derived classes.
28. What is the difference between stack and heap memory allocation?
Stack memory allocation is done automatically and efficiently for local variables and function calls. Heap memory allocation is dynamic and requires manual allocation and deallocation using ‘new’ and ‘delete’ operators.
29. What is the difference between stack unwinding and stack unwinding?
Stack unwinding is the process of removing stack frames in a controlled manner during exception handling. Stack tracing is the process of recording the sequence of function calls that led to an exception.
30. What is a lambda function?
A lambda function is an anonymous function that can be defined inline within code. It provides a concise way to define function objects without the need for explicit function declarations.
31. What is the difference between pass-by-value and pass-by-reference?
Pass-by-value creates a copy of the argument, while pass-by-reference passes a reference to the original object. Pass-by-reference allows modifications to the original object and avoids unnecessary copying.
32. What is operator precedence in C++?
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.
33. What is the ternary operator?
The ternary operator (?:) is a conditional operator that takes three operands. It evaluates a condition and returns one of two values based on the result of the condition.
34. What is a member function in C++?
Member functions are those functions that you declare within a class, they are members of the class. You can reference them using class objects. Eg:
class A
{
public:
int add(int b)
{
a = b * 10;
return a;
};
};
35. Difference between equal to (==) and assignment operator(=)?
The equal to operator == checks whether two values are equal or not. If equal, then it’s true; otherwise, it will return false.
The assignment operator = allots the value of the right-side expression to the left operand.
36. Discuss the difference between prefix and postfix?
In prefix (++i), first, it increments the value, and then it assigns the value to the expression.
In postfix (i++), it assigns the value to the expression, and then it increments the variable’s value.
37. Distinguish between shallow copy and deep copy
In general if the variables of an object have been dynamically allocated, then it is required to do a Deep Copy in order to create a copy of the object.
Shallow Copy:
In shallow copy, an object is created by simply copying the data of all variables of the original object. This works well if none of the variables of the object are defined in the heap section of memory. If some variables are dynamically allocated memory from heap section, then copied object variable will also reference the same memory location.
This will create ambiguity and run time errors dangling pointer. Since both objects will reference to the same memory location, then change made by one will reflect those change in another object as well. Since we wanted to create a replica of the object, this purpose will not be filled by Shallow copy.
Note: C++ compiler implicitly creates a copy constructor and overloads assignment operator in order to perform shallow copy at compile time.
Deep Copy:
In Deep copy, an object is created by copying data of all variables and it also allocates similar memory resources with the same value to the object. In order to perform Deep copy, we need to explicitly define the copy constructor and assign dynamic memory as well if required. Also, it is required to dynamically allocate memory to the variables in the other constructors as well.
38. What is the role of static member keyword on class member variable?
A static variable does exist through the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.
39. What are the Extraction and Insertion operators in C++? Explain with examples.
In the iostream.h library of C++, cin, and cout are the two data streams that are used for input and output respectively. Cout is normally directed to the screen and cin is assigned to the keyboard.
“cin” (extraction operator): By using overloaded operator >> with cin stream, C++ handles the standard input.
int age;
cin>>age;
As shown in the above example, an integer variable ‘age’ is declared and then it waits for cin (keyboard) to enter the data. “cin” processes the input only when the RETURN key is pressed.
“cout” (insertion operator): This is used in conjunction with the overloaded << operator. It directs the data that followed it into the cout stream.
Example:
cout<<”Hello, World!”;
cout<<123;
40. How to access private members of a class in C++?
Private members of the class are not accessible by object or function outside the class. Only functions inside the class can access them or friend functions. However, pointers can be used to access private data members outside the class.
The sample code is as follows:
#include <iostream>
using namespace std;
class sample_test{
private:
int n;
public:
sample_test() { n = 45; }
int display() {
return n;
}
};
41. What is function overriding in C++?
When a function with same name is present in both parent and child class then it is called function overriding.
#include <iostream>
using namespace std;
class parent {
public:
void display(){
cout<<"Parent Class";
}
};
class child: public parent{
public:
void display() {
cout<<"Child Class";
}
};
int main() {
child o = parent();
o.display();
return 0;
}
42. How to concatenate string in C++ ?
The strings in C++ can be concatenated in two ways- one considering them string objects and the second concatenating them C-style strings.
#include <iostream>
using namespace std;
int main()
{
string s_1, s_2, fin;
cout << "Enter string";
getline (cin, s_1);
cout << "Enter string ";
getline (cin, s_2);
fin= s_1 + s_2;
cout << fin;
char str1[50], str2[50], fin[100];
cout << "Enter string";
cin.getline(str1, 50);
cout << "Enter string";
cin.getline(str2, 50);
strcat(str1, str2);
cout << "str1 = " << str1 << endl;
cout << "str2 = " << str2;
return 0;
}
43. How to generate random numbers in C++ with a range?
Using the rand() function we can generate random numbers in C++ within a range.
#include <iostream>
#include <random>
int main()
{
int max=100, min=54,i;
int range = max - min + 1;
for (i=min; i<max;i++)
{
int num = rand() % range + min;
cout<<num;
}
return 0;
}
44. How to use strcmp function in C++?
strcmp() function is an in-built function of <string.h> header file which takes two strings as arguments and compares these two strings lexicographically.
The syntax of the function is as follows:
int strcmp(const char *l, const char *r ); |
#include<stdio.h>
#include<string.h>
int main()
{
// z has greater ASCII value than g
char a[] = "zfz";
char b[] = "gfg";
int r = strcmp(a, b);
if (r==0)
printf("Strings are equal");
else
printf("Strings are unequal");
printf("%d" , r);
return 0;
}
45. What is a volatile keyword?
The ‘volatile’ keyword in C++ is used to indicate that a variable can be modified by external sources and should not be optimized by the compiler. It is often used for memory-mapped I/O or multithreaded code.
46. State the difference between delete and delete[].
“delete[]” is used to release the memory allocated to an array which was allocated using new[]. “delete” is used to release one chunk of memory which was allocated using new.
47. What is the purpose of the Extern Storage Specifier?
Extern” specifier is used to resolve the scope of a global symbol.
#include <iostream >
using nam espace std;
main(){
extern int i;
cout<<i<<endl;
}
int i = 20;
In the above code, “i” can be visible outside the file where it is defined.
48. What is a Conversion Constructor?
It is a constructor that accepts one argument of a different type. Conversion constructors are mainly used for converting from one type to another.
49. What is a diamond problem in inheritance?
The diamond problem occurs in multiple inheritance when a derived class inherits from two or more classes that have a common base class. It leads to ambiguity when accessing members of the common base class.
50. What is the difference between an iterator and a pointer?
An iterator is an object used to traverse and access elements of a container, while a pointer is a variable that holds the memory address of another object. Pointers provide more low-level access and arithmetic operations.