eClerx Most Frequently Asked C++ Interview Questions

eClerx Most Frequently Asked C++ Interview Questions Answers

What Is A Dangling Pointer?

When the location of the deallocated memory is pointed by the pointer even after the deletion or allocation of objects is done, without the modification in the value of the pointer, then this type of pointer is called a dangling pointer.

Explain The Concept Of Memory Leak.

When a variable does not exist longer in the memory, and deletion or reuse of that variable cannot be done, then its destruction occurs automatically. This concept is called memory leak.
For example, consider the following code snippet

{
parent*p=new parent();
}
In the preceding code snippet, the p variable does not exist in the memory, but the variable is not deleted. Therefore, it becomes out of scope and its destruction occurs, which results in memory leak.

List The Issue That The Auto_ptr Object Handles?

The auto_ptr object is used to deallocate memory, which is allocated to a variable, when the variable goes out of scope.
eClerx Most Frequently Asked C++ Interview Questions Answers
eClerx Most Frequently Asked C++ Interview Questions Answers

What Are Smart Pointers?

Smart pointers are almost similar to pointers with additional features, such as automatic destruction of a variable when it becomes out of scope and the throwing of exceptions that ensures the proper destruction of the dynamically allocated objects. They are useful in keeping tracks of dynamically allocated objects. Due to these additional capabilities, they reduce the possibilities of occurrence of exceptions and errors in a program and ensure that the written code is safe and efficient.

How A Pointer Differs From A Reference?

A pointer differs from a reference in the following ways

 In the case of reference, an object must always be referred while initializing. On the contrary, such restrictions are not meant for pointers.
The different types of objects can be pointed by the pointers by reassigning the pointers with different objects. On the contrary, in a reference, the same object which was initialized earlier can only be referred by a reference.
You can use a null address in a pointer parameter to indicate that a variable does not exist; whereas, there is no existence of a null reference in C++.
 A reference usually appears outside an object; whereas, a pointer generally appears inside an object.

How Const Int *ourpointer Differs From Int Const *ourpointer?

As a rule, pointer declarations should be read from right to left. In the pointer declaration, const int *ourPointer, ourPointer is a pointer to a const int object, which cannot be changed by using a pointer. Whereas in case of the int const *ourPointer pointer declaration, ourPointer is a const pointer to an int object, in which an int object can be changed by using a pointer but the pointer itself cannot be changed because it is constant.

Explain How Overloading Takes Place In C++?

C++ supports two types of overloading namely, function overloading and operator overloading. Function overloading helps in defining more than one function with the same name, but with different signatures. An error is raised if two overloaded functions are provided with the same function signature. Operator overloading helps in giving special meanings to operators, when they are used with user-defined classes.

What Is The Difference Between Prefix And Postfix Versions Of Operator++()?

The prefix and postfix versions of operator ++() can be differentiated on the basis of arguments defined. The postfix operator ++() consists of a dummy parameter of int datatype; whereas, a dummy parameter is not found in the prefix operator ++().

Describe The Advantages Of Operator Overloading?

Operator overloading is used to provide some extra features, behaviors, and abilities to the users of a particular class. This feature in C++ helps in controlling the functions performed by an operator and reduces the chance of occurrence of errors in a program.

Provide Some Examples Of Operator Overloading?

The implementation of operator overloading is done in the following ways

Concatenating two std:-.string objects by using the + operator
 Incrementing a Date object by using the ++ operator
Multiplying two different number objects by using the * operator
Accessing array elements from an object of the Array class by using the subscript operator

Can The Operator == Be Overloaded For Comparing Two Arrays Consisting Of Characters By Using String Comparison?

The operator == cannot be overloaded to compare two arrays consisting of characters using string comparisons. It should be noted that out of the two operands of an overloaded operator, at least one operand should be of user- defined type. This user-defined type usually refers to a class. Two characters can be compared easily using classes, such as std::string, rather than using an array containing characters.

Can The Creation Of Operator** Is Allowed To Perform The To-the-power-of Operations?

No, you cannot create operator** for to-the-power-of operations. The number of parameters taken by an operator, names of the operators, priority level of the operators, and the associativity of the operators depend on language in which they are being used. The operator** is not present in C++, so it cannot be created.

What Is The Main Purpose Of Overloading Operators?

The main purpose of operator overloading is to minimize the chances of occurrence of errors in a class that is using the overloaded operators. It also helps in redefining the functionalities of the operators to improve their performance. Operator overloading also makes the program clearer, readable, and more understandable by using common operators, such as +, =, and [].

Specify Some Guidelines That Should Be Followed While Overloading Operators?

Following are some of the guidelines that should be followed while overloading operators

 Subscript bracket operators should be used for overloading when there is a need of fetching data from the container class.
Arithmetic operators should be used for providing the numerical calculation to the operators.
Comma operator overloading should be used less often as the ordering properties of this operator vary before and after overloading. This variation in the ordering properties confuses the users of this operator.
The priority order in which operators are executed cannot be modified by overloading the operators.
The overloaded operators must follow the syntax of the language in which they are used.

Post a Comment

Previous Post Next Post