You are on page 1of 6

COMPUTER APPLICATION

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);
}}

5. What is copy constructor?


6. Ans-A constructor that initializes the data members with the initial
values copied from another object is said to be a copy constructor.
7. What do you mean by constructor overloading?
Ans-If a class uses more than one constructor then they are
automatically overloaded because they process the same name as
the class name but differ in the types of parameters.

public class Perimeter


{
public Perimeter()
{
System.out.println(“From default”);
}
public Perimeter(int x)
{
System.out.println(“Circle perimeter: “ + 2*3.142*x);
}
public Perimeter(int x, int y) {
System.out.println(“Rectangle perimeter: “ +2*(x+y));
}
public static void main(String args[])
{
Perimeter p1 = new Perimeter();
Perimeter p2 = new Perimeter(10);
Perimeter p3 = new Perimeter(10, 20);
}
}
8. Differentiate between parameterized and non- parameterized
constructors.
Ans- A parameterized constructor is a member method written with
the same as the class name and defined along with formal

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.

12. Differentiate between the following statements:


abc p = new abc();
abc p = new abc(5,7,9);

Ans: The first statement abc p = new abc(); is calling a non-


parameterized constructor to create and initialize an object p of class
abc. The second statement abc p = new abc(5,7,9); is calling a
parameterized constructor which accepts three arguments to create
and initialize an object p of class abc.
13. Why do we need a constructor as a class member?

Ans: A constructor is used to initialize the objects of the class with a


legal initial value.

14. Constructor with default argument- Explain

Ans: Java specification doesn't support default arguments in methods


so Constructor with default argument cannot be written in Java.

3
15. Why is an object not passed to a constructor by value?
Explain.

Ans: Constructors are special member methods of the class. Objects


are non-primitive data types so they are passed by reference and not
by value to constructors. If objects were passed by value to a
constructor then to copy the objects from actual arguments to formal
arguments, Java would again invoke the constructor. This would lead
to an endless circular loop of constructor calls.

16. What are the temporary instances of a class?


Ans: If we don't assign an object created by the new operator to a
variable then it is known as a temporary instance or an anonymous
object. If we want to use an object only once, then we can use it as
a temporary instance. The below example illustrates this:
class Add {
int x;
int y;

public Add(int a, int b) {


x = a;
y = b;
}

public void compute() {


int s = x + y;
System.out.println("Sum = " + s);
}

public static void main(String[] args) {


new Add(5, 12).compute();
}
}
In the main method, the object of class Add is not assigned to any
variable. We use it just to print the sum of 5 and 12 after which it is
destroyed.

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;

public Right_Triangle(int x, int y) {


p = x;
b = y;
}
public void area() {
double a = 0.5 * p * b;
System.out.println("Area = " + a);
}
public void diagonal() {
double c = Math.sqrt(p * p + b * b);
System.out.println("Diagonal = " + c);
}
}

18 Why do we need a constructor as a class member?


Ans. A constructor is a special member method which will be called by the
JVM implicitly for placing
user/programmer defined values instead of placing default values.
Constructors are meant for

initializing the object.

19 What is a default constructor?


Ans. When we do not explicitly define a constructor for a class, then java
creates a default constructor for the class. It is essentially a non-
parameterized constructor, i.e. it doesn’t accept any arguments. The

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;
}
}

You might also like