Constructor

1. What is a constructor?

Ans- A constructor is a method which initialise the data members during object creation.

2. Write down the characteristics of a constructor.

Ans- i) Constructor name same as the class name.

ii) It does not return a value.

3. Give an example to define a constructor.

Ans- class Std10

{

public Std10

{

//Statement;

}

}

4. Write down the types of constructor.

Ans- There are two types of constructor

i) Default constructor

ii) Parameterised constructor

5. What is a default constructor?

Ans- The constructor which does not have any parameter to initialise the data members of an object.

Ex:- Data Members

Name

Roll No

Total Marks

class Student

{

private String Name;

private int Roll;

private int T_Marks;

private Student( )

{

Name=”Alex”;

Roll=27;

T_Marks=600;

//Method Name

6. What is a parameterised constructor?

Ans- The constructor which have parameter to initialise the data members of an object.

class Student

{

private String Name;

private int Roll;

private int T_Marks;

public Student(String NM, int RL, int TM)

{

Name=NM;

Roll=RL;

T_Marks=TM;

public void work( )

{

System.out.println(Name);

System.out.println(Roll);

System.out.println(T_Marks);

}

}

}

7. What is a constructor overloading?

Ans- With more than one constructor defined in a program is called constructor overloading.

class Student

{

private String Name;

private int Roll;

private int T_Marks;

public Student(Sting NM, int RL, int TM)

{

Name=NM;

Roll=RL;

T_Marks=TM;

}

public Student( )

{

Name=”Alex”;

Roll=27;

T_Marks=600;

}

public void work( )

{

System.out.println(Name);

System.out.println(Roll);

System.out.println(T_Marks);

}

}

8. What do you mean by copy constructor?

Ans- Copy constructor means the value of the object is copied to another object.

9. What is the difference between function(method) and constructor.

Ans- Constructor

i) Constructor name same as the class name.

ii) It does not return any value.

Method

i) Class name not same as the method name.

ii) It returns value.

10. What is a destructor?

Ans- A destructor is a method which destroy the object automatically for free memory space.

11. Wite down the chaacteistics of the destucto.

Ans- i) A destructor is invoked automatically by the compiler to destroy objects.

ii) A destructor name is same that of its class but it is proceeded by a title sign(~).

iii) When a destructor gets invoked the object is deinitialised, i.e., memory is freed to be reclaimed.

iv) It has no return type, not even void.

Leave a Reply

Your email address will not be published. Required fields are marked *