Professional Documents
Culture Documents
CLASS X
CONSTRUTORS
1. What is the constructor? When is it invoked?
Ans-A constructer is a member method that is written with the same
name as the class name and is used to initialize the data members or
instance variables. It is invoked at the time of creating any object of
the class.
e.g., Car hybrid = new Car();
in the above example, Car() is invoking a default constructor.
2. Give difference between a constructor and a method.
Ans- Constructor-
Constructor name is same as the class name.
Constructor is used only to initialize the data members.
Constructor are automatically overloaded.
Method-
Method name is different from the class name.
Method is also used to perform arithmetical or logical
operations.
Method need to be overloaded.
3. Name two types of constructors.
i. Default constructors.
ii. Parameterized constructors.
4. What is a Parameterized constructor?
Ans- Parameterized constructor is a member function with the same
name as the class name that is used to initialize the object variable
by passing parametric values. For example:
class Pconst{
int a,b;
Pconst(int x,int y){
a=x;
b=y;
}
void display()
1
{
System.out.println(“Value of a and b”+a+’ ‘ +b);
}}
2
parameters whereas a non- parameterized constructor is defined
without any formal parameter.
9. “Member method and constructor can be defined private”. Write
Whether the given statement is true or false, give reasons.
Ans- A member method can be private but a constructor can never
be declared private because the object creation outside the class
may not be possible. Hence, the given statement is false.
10. Write two features of constructors
Ans-the two features of a constructors.
i. A constructor has the same name as class name.
ii. A Constructor is used for initializing data members.
11. Give difference between a static and non-static Data Members.
Ans-Static-
Static data type is a single copy for all the object of class.
It can be accessed without creating an instance of a class.
Non-static Data Members-
Non-static data is individual copy for all the objects.
It cannot be accessed without creating an instance of a class.
3
15. Why is an object not passed to a constructor by value?
Explain.
4
17. Create a class Right_Triangle having data members as the
perpendicular and base of a right-angled triangle(int p, int b). Use a
parameterised constructor to initialize the object. With the help of
function area( ) and diagonal( ), calculate and display the area and
diagonal of the right angled triangle.
Answer:
class Right_Triangle {
int p;
int b;
5
default constructor’s job is to call the super class constructor and initialize
all instance variables. For example:
class Sum{
int a,b;
Sum(){
A=5;b=8;
}
Void display(){
System.out.println(“The value of a is”+a+”and the value of b is”+b);
}
}
In the above program Sum() is used as default constructor.
20 Enter any two variables through constructor with parameters and write a
program to swap and print the values.
Ans. class Swap
{
int a,b;
Swap(int x,int y)
{
a=x;
b=y;
}
void interchange()
{
int t=a;
a=b;
b=t;
}
}