Constructor and Destructor - Object oriented programming (MCQ) questions

Here, you can read Constructor and Destructor multiple choice questions and answers with explanation.

1)   Constructor chaining is
- Published on 19 Oct 15

a. subclass constructor calling super class constructor
b. super class constructor calling subclass constructor
c. both
d. none
Answer  Explanation 

ANSWER: subclass constructor calling super class constructor

Explanation:
In Inheritance subclass or derived class inherits properties of super class or parent class. Constructor chaining is the property by which a constructor in derived class(s) can call constructor of parent class(s). In constructor chaining, when an object of derived class is created its constructor is called which further calls the constructor method of parent class. The keyword super in java helps in passing arguments to super class constructor. The creation of subclass object starts with initialization of class(s) above it in inheritance chain. There could be any number of classes in the inheritance chain. Every constructor function will call up the chain till the top most class is reached.

For Example:

public class animal
{
private string str;
public animal( string str)
{
this.name= name;
system.out.println(“I am an Animal”);
}
}
public class lion extends animal
{
public lion( string str)
{
super(str);
system.out.println(“I am a lion”);
}
}


2)   Why is user defined copy constructor required?
- Published on 19 Oct 15

a. there is no implicit copy constructor in C++
b. when pointers are involved implicit copy constructor does not give correct result
c. both a and b
d. none
Answer  Explanation 

ANSWER: when pointers are involved implicit copy constructor does not give correct result

Explanation:
C++ has an implicit copy constructor which is called by compiler to keep a copy of the object. In case where pointers are present in the code and programmer is not using user defined copy constructor then a problem occurs. The problem is that whenever a copy constructor is called( implicit or user defined) the copy destructor is also called to delete the copy at the end of scope. Implicit copy constructor copies an object bit by bit so pointer address will be copied causing in two different objects sharing same memory location. When the first object calls the destructor to deallocate the pointer no problem occurs but when second object does so it tries to deallocate a pointer that does not exist anymore and the application stops working. In order to prevent situations like this user defined copy constructor is called.


3)   A copy constructor is called
- Published on 19 Oct 15

a. when an object is returned by value
b. when an object is passed by value as an argument
c. when compiler generates a temporary object
d. all the above
Answer  Explanation 

ANSWER: all the above

Explanation:
Copy constructor is a type of constructor which is used for creating a new object that is an exact copy of the existing object. In general compiler creates a copy constructor for each and every class on its own but in special case( when object has pointers involved) copy constructor is created by the programmer. It is called in all the above cases.


1