Copyrights © 2012 Jatin Kotadiya. All Rights Reserved . Powered by Blogger.

Monday, November 12, 2012

C++ Interview Questions


Can a destructor be explicit y called if an object is created using the new operator? | Constructor in C++

No, a destructor cannot be explicitly called if an object is created using the new operator. When you allocate the memory with the new operator, you are responsible for managing it yourself. To avoid cluttering up your program with memory that you are no longer using, you should delete it with the delete operator. The new operator returns a pointer to the newly allocated memory. Therefore, deleting the object is more prefe able than to call the destructor explicitly. For example, if you allocate the object via a typical new expression, such as:
Jerry* j = new ZJerryQ;
Then, the destructor Jerry: :~Jerry () will automatically get called on deleting it via:
delete j; //Automatically calls j->~3erry()
An explicit calling of the destructor would not release the memory that has been allocated for the Jerry object itself. The following are the two advantages of using the delete operator:
? It calls the destructor
? It deal locates the memory

What do you understand by zombie objects in C++? | Constructor in C++

In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.

What are shallow and deep copies? | Constructor in C++

A shallow copy is used to copy actual values of the data. It means, if the pointer points to dynamically allocated memory, the new object's pointer in the copy still points to items inside the old objects and the returned object will be left pointing to items that are no longer in scope. A copy of the dynamically allocated objects is created with the help of a deep copy. This is done with the help of an assignment operator, which needs to be overloaded by the copy constructor.

What happens if the local variable cannot be wrapped in an artificial block? | Constructor in C++


Most of the times, the time duration of wrapping local variables is limited in an artificial block ({...}). In other situations, a member function with similar effects can be added as a destructor instead of calling the destructor itself.For example, in the case of class of Filel, a close() method can be added. Usually, the close() method can be simply called by the destructor. Note that there is a need of marking Filel by the close method(). It avoids the reclosing of an already closed file by a subsequent call of a destructor. For example, the value of the filelHandle can be set by the call of a destructor, such as -1 and the checking can be done at the beginning for ensuring that the filelHandle is already equal to value (-1):
PROGRAM::
Note that the other file methods may also need to check if the filelHandle_ is -1 (i.e., check if the Filel is closed).
Also note that any constructors that do not actually open a file should set filelHandle_ to -1.

Explain how would you handle a situation where you cannot call the destructor of a local explicitly? | Constructor in C++

To avoid the explicit calling of a destructor, the time extent of the local variable can be wrapped in an artificial block {...}:
void time()
{
{
copy c;
// ..insert code that should execute when c is still open..
}

What happens if a local variable is destroyed within its scope before a program is completely executed? Can a destructor be called if needed? | Constructor in C++

No, the destructor cannot be called if needed. Let's understand the preceding statement with the help of an example. There is a local variable n of a class Notebook. Now, the destruction of the local variable n occurs before the execution of a program:
Class Notebook
{
int n;
void bcd()
Notebook n;
// ..Insert code that should execute when n is still in it's scope..
we want the side-effect of n's destructor here!
// ..Insert code that should execute after n is destroyed..
}
On calling a destructor explicitly, an undefined behavior is occurred, because the destructor may invoke twice for the local variable. Firstly, on explicit calling and secondly automatic calling when the local variable destroys. Therefore, a destructor cannot be called explicitly if needed.

Can you explicitly call a destructor on a local variable? | Constructor in C++

No, the destructor is called when the block closes in which the local variable was created.
If a destructor is called explicitly twice on the same object, the result ends in an undefined behavior because the local variable gets destroyed when the scope ends.

Is it possible to overload the destructor of a class? | Constructor in C++

 No, the destructor of a class cannot be overloaded. We can have only one destructor for a class, called class name ::~ destructor name(). For example, for a class named fruit, the syntax is - fruit::~ fruit(). It never takes any parameters and it never returns anything.
We cannot pass parameters to the destructor anyway, since you never explicitly call a destructor.

Write about the sequence in which objects in an array are destructor?

The destructor is executed in the reverse order of constructor. Firstly, the objects are constructed, methods are invoked on them, and then they are destructed.
In the following example, 
the order for destructors will be b[19], b[18],b[17],b[16],b[15],b[14],b[13],b[12], b[ll], b[10]:
void def()
{
Bat b[20];
}

What is the sequence of destruction of local objects? | Constructor in C++

The destructor is executed in the reverse order of a constructor. Firstly, the objects are constructed, methods are invoked on them, and then they are destructed. In the following example, a's destructor will be executed after b's destructor.
void abc()
{
cat a;
cat b;
}

What is a virtual destructor? | Constructor in C++

 Virtual destructors help in destroying objects without knowing their type. With the help of the virtual function mechanism, the appropriate destructor for the object is invoked. In the case of abstract classes, destructors can also be declared as pure virtual functions. For example, class A derives from class B. Then, on calling the derived class dynamically at the execution time, the destructor will first call the derived class that is class A, and then the base class that is class B.
It is important to note that theVirtual keyword, when used with the destructor, ensures the calling of all the derived and base class destructors and therefore helps in the proper execution and closing of the program.

Define the process of handling in case of destructor failure? | Constructor in C++

In order to handle a failed destructor, you need to write a message to a log file; however, do not throw an exception. There is a rule in C++ that exception cannot be thrown from a destructor, which is called when the process of "stack unwinding" occurs in other exceptions. For example, if someone says throw waste files(), the stack frames between the throw waste files() and the catch (waste files) will get popped. This is known as stack unwinding. It is the process of destroying all the local objects related to those stack frames and calling destructors in case of throwing of an exception by one of those destructors. For example, if an object named Bar is thrown, then the C++ runtime system is in a neutral situation means either to avoid the Bar and end up in the catch (waste files) or ignore the function Foo and look for a catch (Bar) handler. It will call in the terminate () process to end the program.

What is a conversion operator? | Constructor in C++

 A conversion operator converts an object of a class in which you define the function to an object of a different data type. A conversion operator uses the C++ operator keyword in its declaration. C++ uses public as an access specifier, which can be used in a class as a public method for specific data type conversions.
Program:
In the preceding code snippet, operator double has no function assigned for the modification of the object/so it is better to make it constant.

Define a conversion constructor? | Constructor in C++

A conversion constructor is a single argument constructor. It is used by the compiler to convert a type of object as an argument to a class type.
Program:
The above example has the following two conversion constructors:
?Y(inti)— Helps in the conversion of integers to objects of class Y
?Y(const char* n, int j = 0) —Helps in the conversion of pointers from strings to objects of the Y class

Is it possible to pass an object of the same class in place of object reference to the copy constructor? | Constructor in C++

 Yes, when an object is created without the reference formal argument, then the copy constructor is called for the argument object. The object that is passed as a parameter to the function is then passed as a parameter to the copy constructor.
For example:
 void xyz(A);
A A1;
xyz(A1);
void xyz(A A2)
{
//definition of xyz()
}
In this case, the copy constructor is called for A2 while Al is passed as a parameter to the copy constructor.

When are copy constructors called? | Constructor in C++

A copy constructor is defined by the programmer or by compiler itself. A call to copy constructor is embedded under the following three conditions:
1. When an object is created and at the same time equated to some other existing object.
 For example:
A A1; //default constructor called.
A A2=A1; //copy constructor called.
(or)
A A2(A1) //copy constructor called.
(or)
A *Aptr= new A(A1); //copy constructor called.
2. When an object is created without having the reference of the argument, then the copy constructor is called by default for the argument object.
For example:
A A1;
xyz(A1);
void xyz(A A2)
{
//defination of xyz()
}
3. When an object is created and at the same time equated to a call to a function that returns an object.
For example:
A xyz();
{
A A1; //remaining defination of xyz();
return A1;
}
A A2=xyz();

What is a default constructor?

 A zero-argument constructor or a constructor in which all the arguments have default values is called a default Constructor.
For example:
A A1;    // default constructor called.

Explain the concept of copy constructor? | Constructor in C++

a Copy constructor is a special type of parameterized constructor. It copies one object to another. It is called when an object is created and equated to an existing object at the same time. An existing object is passed as a parameter to it.
For Example:
A A1; //default constructor called.
A A2=A1; //copy constructor called.
(or)
A A2(A1) //copy constructor called.
(or)
A *Aptr= new A(A1); //copy constructor called.

Define the process of error-handling in case of constructor failure? | Constructor in C++

If the constructor does not have the return statement, then it indicates failure in handling the error by throwing an exception.

Define two methods of constructor invocation? | Constructor in C++

The following are the two ways to invoke a constructor:
1. The compiler embeds a call to the constructor for each object that has got created. For example:
class A
{
int x;
public: void setx(const int=0);
int getx();
};
main()
{
A A1; //object declared constructor called.
A1.A(); //constructor called implicity.
}
2. The constructor is called for each object that is created dynamically in the heap by the new operator. For example:
A *Aptr
A*ptr=new A; //constructor called implicity by compiler.

How does a copy constructor differs from an overloaded assignment operator? | Constructor in C++

A copy constructor uses the value of an argument to construct a new object. We can use an overload assignment operator to assign the value of an existing object of the same class to another existing object in that class.

Can we declare a base-class destructor as virtual? | Constructor in C++

Yes, we can declare a base-class destructor as virtual that makes all derived-class destructors as virtual even if they do not have the same name as base-destructor. The problem arises when the derived class's pointer refers to a base class.
For this reason, the base class destructor should be declared as virtual so that the appropriate destructor is called on calling the delete method of the base class object.

Can we define a constructor as virtual in C++? | Constructor in C++

 No, we cannot define constructors as virtual because constructors have the same name as their classes and no two constructors of base-derived classes can have the same name. So, when we initialize an object of the base or derived class with the help of virtual constructors, the base constructor is invoked instead of the derived constructor. Therefore, it is not possible to define a constructor as virtual.

Define what is constructor? | Constructors in C++

A constructor is a method that is called when a new instance of an object of a class is created. It initializes all class members whenever you create an object of the class. It is a member function with the same name as its class. It may or may not take any parameters. It does not have any return type. For example, the compiler embeds a call to the constructor for each object when it is created. Suppose a class Z has been declared as follows:
class Z
{
public:
Z(); //Constructor for class Z
};
We cannot declare a constructor as virtual or static, nor we can declare a constructor as const, volatile, or const volatile.
The short form of constructor is ctor.

0 comments:

Post a Comment