Page 320 - CA_Blue( J )_Class10
P. 320
Constructor Method
1. It is used to initialize the instance variables of the object. 1. It is used to execute the Java code which performs a specific task.
2. It can be invoked only once at the time of creating the 2. It can be invoked as many times as required.
object.
The similarities between constructor and method are:
1. Both constructors and methods may or may not have arguments.
2. Within the curly brackets, the body of the constructor as well as method contains a sequence of statements that perform a
specific task.
9. Consider the following code:
class student
{
String name;
int roll;
student()
{
name = "abc"; roll=0;
}
student(String n, int r)
{
name = n; roll=r;
}
void display()
{
System.out.println(name);
System.out.println(roll);
}
public static void main()
{
student ob1 = new student();
student ob2= new student("Amit", 10);
ob1.display();
ob2.display();
}
}
a. Name the two constructors in the above program.
b. What type of constructor student() is?
c. What type of constructor student(String n, int r) is?
d. What will be the output of the program?
Ans. a. The two constructors in the above program are:
i. student()
ii. student(String n, int r)
b. student() is a non-parameterised constructor
c. student(String n, int r) in a parameterised constructor
d. abc
0
Amit
10
D. Assertion and Reasoning based questions.
The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
appropriate option given below:
a. Both A and R are true, and R is the correct explanation of A.
b. Both A and R are true, but R is not the correct explanation of A.
c. A is true, but R is false.
d. A is false, but R is true.
318318 Touchpad Computer Applications-X

