Mar

C++ Interview Questions
- Sanjeet Kumar
- 21st Mar, 2023
- 1011 Followers
C++ Interview Questions
C++ is an object-oriented programming language and is meant for general purposes. It has imperative and generic programming features and it especially provides low-level memory manipulation facilities. The International Organization for Standardization (ISO) standardizes C++ and the latest version is C++ 2.0. Similar to C, the C++ language supports four types of memory management, namely static storage duration objects, automatic storage duration objects, thread storage duration objects, and dynamic storage duration objects.
It is widely used for partial or complete scripting and application development such as with Adobe Systems, Google apps, MySQL servers, Mozilla Firefox and Thunderbird, various media players, and more. C++ also finds applications in the development of Operating Systems such as Apple OS X, Microsoft OS, Symbian OS, and more. The development and evolution of C++ are guided by the principle that each program written in the programming language must be driven by actual problems and that its features should be used immediately in real-world programs. C++ is exceptionally compatible with many programming languages in order to foster a compatible and useful programming environment. Finally, practice here the top 35+ C++ Interview Questions & Answers, that are mostly asked during C Plus Plus Job Interviews.
Quick Questions About C++
C++ is a | extension of the C programming Language |
C++ was created by | Bjarne Stroustrup |
C++ was first appeared in | 1985 (About 37 Years ago) |
C++ supports | Cross-platform OS |
C++ uses | .c, .cc, or .cpp file extension |
C++ Interview Questions
1) Explain what is OOP?
Object-oriented programming (OOP) is a type of computer programming, also more commonly known as software design, in which the programmers define the data type of a data structure and the types of operations or functions which can be applied to the data structure.
The data structure becomes an object that includes both data and functions. Additionally, programmers can create relationships between one object and another. Object-oriented programming basically aims to implement real-world entities in programming. These include data abstraction, data encapsulation, inheritance, polymorphism and more.
2) What is the difference between #import and #include in C++?
The statement #include is used in C++ to include the source file or import the header files containing definitions of function declarations, macro definitions to be shared between several source files and variables using the preprocessor. On the other hand, #import is a Microsoft-specific statement used for binary library like DLL or .Lib files. It is very similar to include that it load all the header function definitions from the DLL file so that the user can use the header file just like the case with #include.
The statement #include allows the user to include the same file many times while #import ensures that the preprocessor only includes a file once.
3) What is ‘this’ pointer?
4) What are the vectors in C++?
5) Differentiate between structure and class in C++.
6) What is the use of dot in C++?
7) Differentiate between a pointer and a reference with respect to C++.
A pointer can be reassigned n number of times while a reference cannot be re-assigned after binding. Pointers can point nowhere or to a NULL value, whereas a reference always refers to an object. The users cannot take the address of a reference like they can with pointers.
There’s no “reference arithmetic” but the user can take the address of an object pointed by a reference and perform pointer arithmetic on it.
8) What is the Identity function in C++? How is it useful?
9) Differentiate between C and C++?
C | C++ |
The C language follows the procedural style programming. | C++ is a multi-paradigm programming language. It supports both procedural and object-oriented styles of programming. |
Data is less secure in C as compared to C++. | In case of C++, you can use modifiers for class members to make it inaccessible for outside users. Thus, the data is more secure in C++. |
The C language follows the top-down approach. | The C++ language follows the bottom-up approach. |
There is no scope for function overloading in C. | C++ supports function overloading. |
The user cannot use functions in structure in the case of C. | In case of C++, the user can use functions in structure. |
C also does not support reference variables. | C++ supports use of reference variables. |
In C, the methods scanf() and printf() are mainly used for input or output. | The C++ language mainly uses streams cin and cout to perform the input and output operations. |
10) Differentiate between an array and a list?
Array memory allocation is always static and continuous while the memory allocation of a list is dynamic and quite random.
In case of arrays, users need not keep a track of next memory allocation, whereas in case of lists, a user has to keep a track of next location where memory is allocated due to its dynamism.
11) Explain what are accessor methods?
Though a new dependent code is contained within accessor methods, yet they directly access state data. Furthermore, within a database fetch, the dependent code need not be changed. This is also an advantage of this type of object-oriented programming.
When comparing two data items, two access method calls are necessary in order to make the comparison. Accessors seek underlying data such as data creation, data retrieval, initialization, as well as modification. The accessor method is basically a type of instance method that contains a sequence of programming statements for the purpose of performing an action, customizing those actions with a parameter and producing a return value of some sort. An accessor function or method must be called to access a private object member.
12) Explain what is class definition in C++ ?
13) Explain what are mutator methods in C++?
14) Explain what are single and multiple inheritances in C++?
15) Explain what is polymorphism in C++?
Polymorphism is a kind of runtime generalization that is enabled by the type system in object-oriented as well as functional programming languages like C++ and Haskell. Typically, we talk about two types of polymorphism in programming languages:
- Subtype polymorphism – which allows a function that is defined over a formal parameter having one type also to be defined over subtypes of same.
- Parametric polymorphism – which allows a template, already defined class over a template parameter having one type also to be defined over subtypes of same.
16) Explain what data encapsulation is in C++?
17) What is data abstraction? How is it different from data encapsulation?
18) How is new() different from malloc()?
19) What are inline functions? What is the syntax for defining an inline function?
The C++ language provides inline functions to reduce the function call overhead. An inline function is a method or function that is expanded in line when it is called. Whenever the inline function is called, the complete code of the inline function gets inserted or substituted at the point of the inline function call. The C++ compiler does this substitution at compile time. The inline function may increase the efficiency of the code if it is small.
The syntax for defining the function inline is:
inline return-type function-name(parameters)
{ // function code }
20) Is it possible to have a recursive inline function in C++?
21) What is a responder chain?
22) What do you mean by delegate? Can a user retain delegates?
Delegates can be retained if the user wants to. If the user declares it to be retained (which means the delegate will be influential in ARC) it’ll be retained.
Generally, the norm is not to retain the delegates because they are already retained elsewhere and more importantly, the user can avoid retaining cycles by opting not to retain them.
23) How is Objective C different from C++?
There are a considerable amount of difference between c and c++ :
Objective C | C++ |
The classes in Objective C allow a method or function and a variable with the exact same name. | In case of C++, they must be different. |
Objective C does not support a constructor or destructor. Instead, it has init and dealloc methods, which must be called explicitly when needed. | C++ supports constructors and destructors. |
Objective C uses the signs + and – to differentiate between class methods (which are also known as factory methods in Java) and instance methods. | On the other hand, C++ uses static to specify a factory method. |
Multiple inheritances is not allowed in Objective C, however, a user can use a protocol to some extent. | Multiple inheritances are allowed in C++. |
Objective C does not directly allow for method overloading and has a workaround, but that is not the case for operator overloading. | C++ directly allows for method overloading. |
Objective C does not allow stack-based objects either. Each object must be a pointer to a block of memory for it to exist. | C++ can use stack based objects which is automatically allocated, instantiated and deallocated on the stack. |
In Objective C the message overloading is somehow faked by naming the parameters. The user is required to mangle the names manually. | C++ actually does the same but the compiler does the name mangling for the user instead of having to do it himself. |
Objective C doesn’t allow automatic type coercion. | C++ allows automatic type coercion. |
Objective C has references. But as pointers can be used wherever a reference is employed, there isn’t any specific need for references in general. | C++ has references as the language requires. |
Objective C doesn’t have templates. | The users of C++ need templates because the programming language has a strong typing and static binding that prevents generic classes, such as Lists and Arrays. |
24) Explain the volatile and mutable keywords.
The keyword volatile informs the compiler that a variable may change without the compiler knowing it. Variables that are declared as volatile will not be cached by the compiler, and will therefore always be read from memory.
The keyword mutable can be used for class member variables. Mutable variables are allowed to change from within constant member functions of the class.
25) What is Dynamic and Static Typing?
Since C++ is a statically typed language, the user needs to tell the compiler what type of object they’re working with at compile time.
26) What is a static member?
27) What is a virtual function?
28) What do you mean by function and operator overloading in C++?
29) What is function overriding?
30) Differentiate between a constructor and a destructor in C++.
Constructor | Destructor |
A constructor is used to initialize the instance of a class or object. | A destructor simply destroys the objects when they are no longer needed. |
A constructor is called when a new instance of a class or object is created. | A destructor is called when an instance of a class or object is deleted or released. |
A constructor allocates the memory. | A destructor releases or frees the memory. |
Constructors can have arguments. | A destructor cannot have any arguments. |
Overloading of the constructor is possible in C++. | Overloading of destructors is not possible. |
A constructor has the same name as class name. | A destructor also has the same name as class name but with (~) tiled operator. |
31) What are the two main components of C++?
32) Why is C++ a mid-level programming language?
33) Explain the properties and principles of OOP.
The class is a category of objects, which defines all the common properties of the various objects that belong to it.
Encapsulation is the process of combining or wrapping up of data or elements to create a single, new entity.
Information hiding is the process of hiding details of an object or function for reducing complexity.
Inheritance is a feature that represents the “is-a” relationship between the various classes.
The interface is the set of languages and codes that the applications or programs use to communicate with each other as well as with the hardware.
Messaging or message passing is a kind of communication used in parallel programming and OOP.
The object is a self-contained entity, which consists of both data and procedures employed to manipulate the data.
Polymorphism is a programming language’s ability to process objects differently depending on their data type or class.
The procedure is merely a section of a program that performs a specific task.
34) How many characters are recognized by ANSI C++?
128 characters are recognized by ANSI C++.
35) What is copy constructor?
Just like the name, a copy constructor is one the other constructors used for creating a copy of an object of class type that has been in existence already. The translator known as compiler makes available a default copy constructor for every class. The named constructor comes from creating while copy simply means new object from an existing one. In a simpler term, a copy constructor creates a copy of an already existing or available object.
36) What is function overloading in C++?
Function overloading is one of the features in C++ programming language where more than one function can one particular name but distinct parameters.
Leave A Comment :
Valid name is required.
Valid name is required.
Valid email id is required.