C++ Interview Questions and Answers

Here are 50+ commonly asked C++ interview questions and answers which will definitely help you out to crack one of the toughest interviews.

1. What is Object Oriented Programming Approach?
Object Oriented Programming is an approach that provides a way of modularizing programs by creating partitioned memory area for data as well as functions that can be used as templates for creating copies of such modules on demand. OOP allows decomposition of a problem into a number of entities called Objects and then builds data and functions around these objects.

2. What is a Class?
An entire set of data and code of an object can be made user-defined data type using a class. Objects are variables of Class type. Once a Class has been defined, we can create numerous Objects of its type. A Class is a collection of Objects of similar type.

3. What is Hierarchical Inheritance?
Hierarchical Inheritance occurs when traits of one Class is inherited by more than one classes.

C++ Interview Questions and Answers

4. What are Iterators in C++ STL?
Iterators behave similar to Pointers and are used to access container elements. They are used to traverse from one element to another. This process is known as ‘iterating through the container’.

5. What is Polymorphism?
It is the ability to showcase or exhibit different behaviors under different instances. The process of making and Operator or a Function behave differently in different instances is known as Operator Overloading.

6. What is iostream?
iostream is a header file in C++. It includes function prototypes for Standard Input Output Functions which include definitions for cout and cin too. The header file is declared as #include<iostream>

7. What is Dynamic Binding?
Binding is the linking of a procedure call to the code to be executed in response to the call. Dynamic Binding is also know as Late Binding which means that the code associated with a given procedure call is unknown until the time of call at run-time.

8. What are the applications of Object Oriented Programming?
The areas of applications for OOP includes:
1. Artificial Intelligence Systems
2. Simulation and Modelling
3. Real-Time Systems
4. Neural networks and Parallel Programming
5. Object Oriented Databases

9. What is a Destructor?
A Destructor is used to destroy the objects that have been created by Constructor. It is basically used to clean up storage that is no longer accessible. A Destructor is a member function. Its name is the same as the class name but is preceded by a Tilde.

10. What is a Namespace?
It is mandatory that all the C++ programs must include Namespaces. Namespaces in C++ defines a scope for the identifiers that are used in the program.

11. What is Enumerated Data Type?
An Enumerated Data Type is another User-Defined Type which provides a way for attaching numbers, thereby increasing comprehensibility of the program code. The enum keyword automatically enumerates a list of words by assigning them values 0,1,2,3, and so on.

12. What are Lists in C++ STL?
A List is another very important Container in C++. It supports bidirectional, linear list and provides and efficient implementation for deletion and insertion operatons. A List can only be sequentially accessed.

13. Enlist Operators in C++.
new Memory allocation operator
.* Pointer to Member Operator
endl Line feed operator
:: Scope Resolution Operator
::* Pointer to Member Declarator
delete Memory Release Operator
->* Pointer to Member Operator
setw Field width operator

14. Explain catch block in C++ Exception Handling.
A catch block is defined by keyword catch that catches Exceptions thrown in by throw block and then handles it accordingly. The catch block that intercepts an exception immediately follow the try block that throws the exception. There can be multiple catch statements in a single program.

15. What is Function Prototyping?
A Function Prototype depicts the function interface to the compiler by providing the details regarding Number and Type of arguments and the type of return values. With function or method prototyping, a template is always used when declaring and defining a function.

16. What is the ifstream() method?
ifstream() method provides input operations. It contains open() method with default input mode. It also inherits functions such as get(), getline(), read(), seekg() and tellg() functions from istream.

17. What is Multilevel Inheritance?
The mechanism of deriving a Class from a previously Derived Class is known as Multiple Inheritance.

18. What is an Object?
Objects are basic Run-Time entities in an Object oriented system. They may represent a place, a bank account or a person. Objects are essentially the variables that are of Class types.

19. What is an Inline Function?
To eliminate the cost of calls and few other overheads to small functions, C++ provides a new feature called Inline Function. An inline function is a function that is expanded in line when it is invoked. This helps to save memory space. This is some what similar to Macro Expansion.

20. What are Visibility Labels in C++?
The keywords Public, Protected and Private are known as Visibility Labels in C++. By default, the members of any Class are private. A Class which has a Visibility label private is completely hidden from the external environment and does not serve any purpose. A Class with Public Label is visible to the other functions and classes.

21. What are Derived Containers in C++ STL?
The Characteristics of a Static Data Member Variable are:
1. It is initialized to Zero when the first object of its Class are created.
2. It is visible only within that class, but its lifetime is the entire program.
3. Only one copy of that member is created for the entire class and is shared by all the objects of that class irrespective of the number of objects are created.

22. Enlist the characteristics of Friend Functions.
1. It is not in the scope of the class to which it has been declared as a Friend.
2. Since it’s not in the scope of the class, it cannot be called using Object of that Class.
3. It can be invoked like a normal function without the help of any object.
4. Usually , it has the Objects as Arguments.
5. It can be declared either in public or in the private part of a Class without affecting its meaning.

23. What is a Constructor?
A Constructor is a special member function whose primary task is to initialize objects of its class. The constructor is invoked whenever an object of its class with which it is associated to is created. It is named as constructor because it constructs the values of class data members. It is special because its name is the same as its class name.

24. What is Hybrid Inheritance?
A Hybrid Class is the one that is derived from numerous classes; it can be derived from a parent as well as a child class. There is no sequence as such. It all depends on the needs of such a class.

25. What are cout and cin?
cout is the object of ostream class. The cout stream is by default linked to console output device.It is basically used to output the characters on the console screen. It similar to printf in C.
cin is the object of istream class. The cin stream is by default linked to console input device. It is basically used to extract the characters form the User. It similar to scanf in C.

26. What is a Virtual Function?
When a method is declared as Virtual method, the C++ compiler determines which function should be used as runtime based on the type of object pointed to by the base pointer, rather than the type of pointer. When we use the same method name in derived as well as base class, the method in base class is declared a virtual using the keyword virtual preceding its normal declaration.

27. What is Data Abstraction?
Data Abstraction is basically representing important features without taking care of the background details and information. Since the Classes use concept of Data Abstraction, they are known as Abstract Data Types.
STL povides three derived containers named as Queue, Stack and Priority_Queue. These are also known as Container Adapters. These can be developed from different Sequence containers. All of them offer two main functions viz., push() and pop().

28. What are static Data Members in C++?
The Characteristics of a Static Data Member Variable are:
1. It is initialized to Zero when the very first object of its Class are created.
2. It is visible only within that class, but its lifetime is the entire program.
3. Only single copy of that member is created for the whole class and is shared by all the objects of that class irrespective of the number of objects created.

29. Enlist the characteristics of Friend Functions.
1. Usually, it has the Objects as Arguments.
2. As it’s not in the scope of the class, it cannot be called using Object of that Class.
3. It is not in the scope of the class to which it has been declared as a Friend.
4. It can be declared either in public or in the private part of a Class without affecting its meaning.
5. It can be invoked like a normal function without the help of any object.

30. What is a Constructor?
A Constructor is a special member function or a method whose task is to initialize objects of its class. It’s name is the same as its class name. The constructor is invoked whenever an object of its associated class is created. It is named as constructor because it constructs the values of data members of the class.

31. What is Hybrid Inheritance?
A Hybrid Class is the one that is derived from numerous classes; it can be derived from a parent as well as a child class. There is no sequence as such. It all depends on the needs of such a class.

32. What is STL?
STL stands for Standard Template Library. It is a software library for C++ programming language that influenced many parts of C++ Standard Library. Using STL can save considerable time and effort leading to high quality programs. STL components are defined in namespace std.

33. What is this Pointer?
C++ makes use of a unique keyword called this to represent an object that invokes a member function. It is a pointer that points to the object for which this function was called. For example, the function B.min() will set the pointer this to the address of the object B.

34. What is eof() method used for?
eof() method returns a Non Zero Value if end-of-file is encountered while reading, else it returns a Zero.

35. What is ofstream() method?
This method provides output operations. It contains open() method with default output mode. It also inherits functions such as put(), seekp(), tellp(), and write() functions from istream.

36. What is a Vector in STL?
A Vector is one of the most widely used containers in C++. It stores the elements in contiguous memory locations and provides direct access to every element using the subscript operator. A Vector can modify its size dynamically and hence allocates the memory as needed at run time.

37. How do we access Virtual Functions?
We must access virtual functions through the use of pointer declared as a pointer to the base class. Run Time Polymorphism is achieved only when a virtual method is accessed through a pointer to the base address.

38. What is Encapsulation?
Wrapping up data and functions into a single unit or a block called as a Class is know as Encapsulation. This insulation of data from direct access by the program is called Data Hiding. The data is therefore inaccessible to the external world; only the functions wrapped inside class can access it.

39. What is a Stream Class?
The C++ Input/Output consists of a hierarchy of classes that are used to define various streams to deal with the console files as well as disk files. These classes are called a Stream Classes and are declared in the header file iostream.

40. What is Multiple Inheritance?
A Derived Class with multiple Base Classes is called Multiple Inheritance.

41. What are Maps?
A map is a sequence of key, value pairs where a single value is associated with each unique key. Retrieval of values is based on the key and is quick. We need to specify the key to obtain the associated values .

42. What are command line arguments?
The arguments/parameters which are sent to the main() function while executing the program from the command prompt or command line. All the arguments sent are in the form of strings only.

43. What is a Friend Class?
A class members can gain accessibility over other class member by placing the class declaration prefixed with the keyword ‘friend’ in the destination class.

44. How do you Overload Template Functions?
A Template Function may be overloaded either by template methods or ordinary methods of its name, It may be accomplished as follows:

1. Call an ordinary method that matches exactly.
2. Call a template method that could be created with an exact match.
3. Try normal overloading resolution to ordinary methods and call the one that matches.

45. Enlist STL Algorithms.
1. Retrieve or Non-Mutating Algorithms
2. Mutating Algorithms
3. Sorting Algorithms
4. Set Algorithms
5. Relational Algorithms

46. Explain try and throw block?
The keyword try is used to preface a block of statements that may generate exceptions. This block of statements is known as Try block. When an exception occurs, it is thrown into the catch block using the throw statement.

47. What is a Virtual Base Class?
A Base Class class that has been qualified as Virtual in the inheritance definition. In multiple inheritance, a derived class can inherit the members of a base class via two or more inheritance paths. For a virtual base class, only a single copy of its members will be inherited regardless of the number of inheritance paths between the base class and the derived class.

48. Enlist the Components of Standard Template Library.
STL contains several components but the core components are:
1. Iterators
2. Algorithms
3. Containers
4. Functions

49. What is Operator Overloading?
Operator Overloading basically means to make an Operator behave differently in different instances. Asterisk * is an example of Operator Overloading as it acts as a Pointer as well as a Multiplication operator automatically. Operator overloading is a part of Polymorphism.

50. What is Inheritance?
It is the process by which Objects of one class acquire the properties of objects of another class. It also supports Hierarchical classification. Inheritance basically provides the concept of Reusability.

51. What is RTTI?
RTTI represents Runtime Type Information . It determines the type of any variable during execution i.e., during runtime. The RTTI Mechanism contains:
1. Operator Dynamic_Cast
2. Operator Typeid
3. Struct Type_Info

52. Enlist Iterators in C++ STL.
The Iterators in C++ are as follows:
1. Input
2. Forward
3. Random
4. Output
5. Bidirectional

53. What is the role of a Mutable Storage Class Specifier?
A constant class object’s member variable can be modified by declaring it using a Specifier of a mutable storage class. It is applicable only for non-static and non-constant member variable of the class.

54. Does C++ Programming Language support Multiple and Multilevel Inheritance?
Yes, it does support multiple inheritance and multilevel inheritance.

So this was the list of some important C++ interview questions and answers. If you found any information incorrect or missing in above list then please mention it by commenting below.

Leave a Reply

Your email address will not be published. Required fields are marked *