C++ Interview Questions

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++20. 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 partially or completely 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 application in the development of Operating Systems 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.

Read Best C++ Interview Questions with answers

Here are a few questions, which might be asked during a C++ interview:

Download C++ Interview Questions PDF

C++ Interview Questions

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.
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.

There are a considerable amount of difference between c and c++ :

Objective CC++
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.
A class describes the behavior and properties common to any particular type of object. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. Defining a class means defining a blueprint for a data type or an object. A class definition begins with the keyword class followed by the class name and the class body, which is enclosed by a pair of curly braces. A class definition must always be followed either by a semicolon or a list of declarations for it to be valid.
While an accessor function makes a protected data member or private object accessible to an outside user, it does not give permission for editing it or modifying it in any way. Modification of a protected data member always requires calling a mutator function. Since the mutator methods provide direct access to protected data, both mutator and accessor functions must be written and used carefully by the user.
The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined that has a certain set of characteristics (such as methods and instance variables) and then other classes to be created, which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own. Multiple Inheritances is a feature of C++ wherein a single class can inherit objects and methods from more than one classes. The constructors of the inherited classes are called in the same order by the base class in which they are inherited. Multiple inheritances is a feature which is not common to many programming languages. Even though it is an extremely useful feature which allows a user to apply multiple interfaces to a class, a user might encounter problems such as ambiguity and the Diamond Problem if it is not used or executed correctly.

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.
Data encapsulation is a mechanism of bundling the data and the functions that use them where the implementation details of a class are kept hidden from the user. With this, the user can only perform a restricted set of operations on the hidden members of the class by executing special functions, which are also commonly known as methods. This vital concept of object-oriented programming is also often used to hide the internal representation, or state, of an object from the outside world.
Data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user. Encapsulation can merely be understood as wrapping up of data and methods in a capsule, which means just hiding properties and methods. Encapsulation is used for hiding the code and data in a single unit to protect the data from the outside the world. The class is the best example of encapsulation in C++. On the other hand, abstraction means showing only the necessary details to the intended user.
New () is a preprocessor whereas malloc() is a function or method. There is no need for the user to allocate the memory while using “new” but in malloc() you have to use the function sizeof() to allocate the memory. “new” initializes the new memory to 0 while malloc() stores a random value in the newly allotted memory location.

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
Although a user can call an inline function from within itself, the compiler may not generate the inline code as the compiler is unable to determine the depth of recursion at compile time. A compiler with a good optimizer can inline recursive calls until some depth fixed at compile-time (say three or five recursive calls), and insert non-recursive calls at compile time for cases when the actual depth gets exceeded at run time.
The responder chain is a series of linked responder objects. The responder chain begins with the first responder object and ends with the app object. In case the first responder is unable to handle an event, the chain forwards the event to the next responder in line.
A delegate is an object, which acts on behalf of, or in coordination or sync with, another object when the other/second object encounters an event during program execution.
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.