In this collection, I have chosen 60+ mostly asked C++ interview questions and answers, which will surely help you to get success in interviews.
1. What is C++?
C++ is a general-purpose, object-oriented programming language based on the earlier “C” language. It was developed by Bjarne Stroustrup in 1979 as an extension to the C language and is a powerful, fast, and efficient language.
2. What are the advantages of C++?
The advantages of C++ are:
*Object-oriented programming.
*It is an extension of the C language, which is already popular and familiar.
*C++ provides greater flexibility and control manual memory management than other object-oriented languages like Java and Python.
*It is faster than other languages because of its low-level memory manipulation capabilities.
*It is platform-independent, which means the code written in C++ can run on any platform.
3. What is the difference between C++ and C programming?
C++ is an object-oriented programming language, while C is a procedural programming language, C++ extends the functionality of C by adding various features such as classes, polymorphism, inheritance, etc.
4. What are the data types available in C++?
The following data types are available in C++:
i. Primitive data types:- Integer, Character, Floating Point, Boolean, and Void.
ii. Composite data types:- Arrays, Pointers, References, and Classes.
5. What are loops and how are they used in C++?
Loops are control statements that allow the programmer to repeat a block of code multiple times. The three main loop statements in C++ are for, while, and do-while loops.
6. What is the purpose of the main() function in C++?
The main() function is the entry point of a C++ program. This function is responsible for executing the main logic of the program and returning the control back to the operating system.
7. What is class in C++?
Answer: A class in C++ is a user-defined data type that is used to represent a type of object. A class contains variables, data members and functions that represent the properties and behaviors of that particular type of object. Classes allow users to define their own data types with functions that can be used to manipulate the data.
8. What is an object in C++?
An instance of a class in C++ programming is known as an object.. It is created at run time . It is used to access the methods and properties of a class.
9. How are objects created in C++?
To create objects in C++, you must first create a class, then create an instance of that class, also known as instantiating the class or creating an object. This is done using the ‘new’ operator.
10. What is the ‘this’ pointer in C++?
The ‘this’ pointer is a pointer that points to the current instance of an object in a class. It is used to access the members of the current object within a class. It is implicitly passed as an argument to all non-static member functions.
11. What is the use of references in C++?
References in C++ are alternative names for objects. When a reference is initialized to an object, the reference is an alias for the object. It is used to avoid making unnecessary copies of an object and for providing better readability and optimization of code.
12. What is namespace in C++?
Namespaces are a way of grouping related code, data, and objects to avoid naming conflicts or collisions. They are similar to Java packages or C# namespaces. The using keyword is used to bring the namespace into scope.
13. What is an exception in C++?
An exception is an object that is thrown when a program encounters an unexpected situation or condition. It allows the program to recover gracefully from the situation or condition, usually by setting an error flag or logging information about the problem.
14. What is streaming in C++?
Streaming is the process of reading or writing data from or to an external source. It is used to read or write formatted data to or from the stream. Most C++ Standard Library classes, such as fstream and stringstream, provide streaming capabilities.
15. What is a constructor in C++?
A constructor is a special kind of member function, which is called automatically when an object is created.. The constructor is used to initialize the values of an object’s data members. A constructor typically takes the same name as the class, has no return type, and takes parameters.
16. What is copy constructor in C++?
A copy constructor is a special constructor that takes an object of the same type as an argument and creates a new object that is an exact copy of the argument object.
17. What is inheritance in C++?
Answer: Inheritance in C++ is a way for one class to be derived from another class. This allows the derived class to inherit the properties and behavior of its parent class while adding its own unique features. This is useful for code reusability and prevents the duplication of code that can occur with object-oriented programming.
18. What is multiple inheritance in C++?
Multiple inheritance is a C++ concept wherein a class can inherit members from more than one parent class. This means that a class can have more than one base class, allowing it to inherit features and properties from them.
19. What is polymorphism in C++?
Answer: Polymorphism in C++ is a feature that allows the same operation to be performed on different data types. It allows us to use the same code for different types of objects without needing to write separate code for each one. It is usually implemented using inheritance and function overloading.
20. What is encapsulation in C++?
Answer: Encapsulation is the process of combining data and functions or methods in a class. It is a way of providing data hiding or information hiding by controlling access to variables and functions within a class.
21. What are the three basic ways of passing parameters in C++?
Answer: The three basic ways of passing parameters in C++ are call by value, call by reference, and call by pointer.
22. What is default keyword in C++?
Answer: The default keyword is used to specify the default action or parameter value of a function. It is used to allow the programmer to specify the default value that will be used if a parameter is not provided when the function is called.
23. How do you define a template in C++?
Answer: A template is a generic function or class that can take an arbitrary number of type parameters. Templates allow the same code to be used for multiple types and provide type safety and better code reusability. They are defined using the keyword template followed by a comma-separated list of type parameters inside angle brackets (<>).
24. What are the advantages of using templates in C++?
Answer: Using templates in C++ allows the programmer to write generic code that can be used with different types of data. This reduces code duplication and increases code readability. Templates also allow for compile-time type checking, which improves program accuracy.
25. What is a virtual function in C++?
A virtual function in C++ is a member function of a class that is declared with the keyword virtual. These type of functions can be overridden in derived classes. This means that when an instance of a derived class calls the virtual function, the function defined in the derived class is used instead of the one defined in the base class. Virtual functions are used to implement polymorphism in C++.
26. What do you mean by friend function?
A friend function is a function with access to a class’s private and protected members while not being a member of the class. Friend functions are typically declared by the class declaring them (using the friend keyword) or defined outside the class.It is useful when classes need to share data or operations that are not accessible through public methods.
27. What is the use of a constructor in C++?
Answer: The purpose of a constructor in C++ is to initialize an object when it is created. They are special member functions that are executed automatically when an object is instantiated. Constructors can be used to set default values, allocate memory, or perform any other tasks that must be done when an object is created.
28. What is the use of a copy constructor in C++?
Answer: A copy constructor is a unique constructor used to build a new object from an existing object. It is used to initialize an object from another object.
29. What is function overloading in C++?
Answer: Function overloading in C++ is a feature that allows the same function name to be used to perform different tasks. This is achieved by passing different parameters and data types to the same function. This allows for code reusability and makes the code more readable and maintainable.
30. What is operator overloading in C++?
Operator overloading is a feature in C++ that allows operators such as +, -, *, and / to have different implementations depending on the context that they are used in. For example, the addition operator can be used to add two integers, or it can be used to concatenate two strings together. This enables a developer to create custom classes that behave like built-in classes.
31. What is the difference between overloading and overriding in C++?
Answer: The difference between overloading and overriding in C++ is that overloading is the concept of using the same method name for different operations while overriding is the concept of replacing a method in a derived class with one that has the same signature as the base class’s method. Overloading can be used to achieve polymorphism while overriding focuses more on object-oriented programming principles.
32. What are the different access specifiers in C++?
The four different access specifiers in C++ are:
i. Public – Allows class members to be accessed from outside the class.
ii. Protected – Allows class members to be accessed within the class and by any derived class of that class.
iii. Private – Grants access only to members of the same class.
iv. Default – This is same as private, but applies when no access specifier is specified.
33. How does C++ handle buffer overflows?
Answer: Buffer overflows can be handled in C++ by using the built-in functions that check for buffer boundary violations. These functions include strncpy, strncat, and others. By using these functions, a buffer overflow can be detected and the program can take appropriate action, such as terminating the program, or reporting an error.
34. What is the difference between an object and a class in C++?
A class is an entity that defines the structure and behavior of objects. It defines the properties (data) and methods (functions) of the object.
An object is an instance of a class, which created at runtime. It has its own identity and holds its own data.
35. What is the difference between an array and a pointer in C++?
An array is a data structure that allows a collection of items to be stored and accessed in an organized manner. A pointer is a variable that stores the address of another variable instead of the actual data value.
36. What is reference and a pointer in C++?
A reference is an alias of an existing variable, while a pointer is a variable that stores the address of another variable. A reference must always be initialized, while a pointer does not have to be initialized.
37. What is the difference between #include and #include “filename” in C++?
#include is used to include the contents of a standard header file, while #include “filename” is used to include the contents of a specific user-defined header file.
38. What is the difference between a constructor and a destructor in C++?
A constructor is a special member function of a class used to initialize objects of that class, while a destructor is a special member function of a class used to release dynamically allocated resources and perform clean up activities.
39. What is the difference between C-style strings and string objects in C++?
A C-style string is an array of characters ending with a null character. On the other hand, a string object is an object of class string, which represents a sequence of characters. It is more efficient and provides better readability.
40. What is the difference between an object and an instance in C++?
An object is a defined entity, and an instance is a runtime occurrence of the object. An object is a class definition, and an instance is the object created at runtime.
41. What is the difference between depth-first search and breadth-first search in C++?
Depth-first search is a technique used to traverse a tree or graph data structure in which the nodes are explored deeply before they are explored across. On the other hand, breadth-first search is a technique used to traverse a tree or graph in which the nodes are explored across before they are explored deeply.
42. What is the difference between object-oriented programming and procedural programming?
Answer: The main difference between object-oriented programming and procedural programming is that object-oriented programming focuses on creating objects that contain both data and functions, while procedural programming focuses on creating functions that operate on data. Object-oriented programming is more modular and can be more easily extended, while procedural programming is more linear and focused.
43. What is difference between call by value and call by reference in C++?
In call by value, a copy of the original parameter is passed to the function while in call by reference, the address of the parameter is passed to the function.
In call by reference, the parameter is modified within the function and the change is reflected in the original variable.
44. What is the difference between an array and a linked list in C++?
An array is a data structure consisting of a sequence of elements in which each element can be accessed by an index number. An array is stored in contiguous memory locations, and it is static in size.
A linked list is a linear data structure in which each element is a separate object which contains a pointer that points to the next element. Linked lists are stored non-contiguously in memory, and the size is dynamic as nodes can be added and removed.
45. What is the difference between an inline function and a normal function in C++?
Answer: An inline function is a compiler directive to replace a function call with the corresponding function body, while a normal function is a set of instructions defined by the programmer. Inline functions are faster than normal functions but increase the size of the compiled code.
46. What is the difference between a class and a struct in C++?
Answer: A class is a user-defined type which contains data and functions, while a struct is a group of related variables of the same or different types. Structs are typically used to represent more complex data, while classes are preferred for object-oriented programming.
47. What is the difference between a static member and a non-static member in C++?
Answer: A static member is shared among all instances of a class, while a non-static member belongs to an individual instance of a class. Static members can be accessed without creating an instance of the class, while non-static members must be accessed via an instance of the class.
48. What is the difference between shallow copy and deep copy in C++?
Answer: The difference between shallow copy and deep copy in C++ is that shallow copy copies only the pointer to the data while deep copy copies the entire data. Deep copy ensures that no data is shared between objects while shallow copy allows multiple objects to share the same data.
49. What is the difference between an abstract class and an interface in C++?
Answer: The difference between an abstract class and an interface in C++ is that an abstract class can contain both abstract and non-abstract methods while an interface can only have abstract methods. Abstract classes can be used to provide object inheritance while interfaces cannot. In addition, abstract classes can contain data members but an interface cannot.
50. What is the purpose of the keyword ‘volatile’ in C++?
Answer: The purpose of the keyword ‘volatile’ in C++ is to indicate to the compiler that the value of a particular variable may change without the knowledge of the compiler. This allows the compiler to ensure that the variable is always accessed directly from memory, as opposed to being cached in a register, or optimized out by the compiler.
51. What is file handling?
File handling is the process of manipulating files programmatically. It is used to read, write, and manage the data stored in a file. It also allows programs to store multi-dimensional data in a text, binary, or other type of file, and then retrieve it in a structured format.
52. What are the different types of files used in C++?
There are several types of files used in C++:
– Text files: These are standard ASCII files, with one line of text per line.
– Binary files: These are files that contain non-text data, such as images, in binary form.
– Random access files: These files allow for random access to the file data, for faster data retrieval.
– Stream files: These files are used for sequential access of data and are often buffered for faster access.
53. What is the purpose of the fstream header in C++?
The fstream header is used to read and write data to and from a file. It is used to open, close, get and set the position of streams, and to get the size of a stream. It also provides functions for stream manipulations such as formatting, flushing, and seeking.
54. What is the difference between an object and a pointer?
An object is an instance of a class, while a pointer is a variable that holds the memory address of another variable. Pointers can also be used to refer to objects in C++, as long as the pointer has been set to refer to the proper object.
55. What is the purpose of the this pointer?
The this pointer is a pointer that points to the current object in a class. It is often used when you need to access the data members or member functions of the current object in question.
Other Important link:-
Java Interview Questions and Answers