Class, Constructor and Objects
Section A
2005
Question 2
(c) State the difference between Constructor and Method. [2]
1) Constructor is a member method of a class used to initialize values to data members.
A method is used to do some computations by an object such as input, calculate and display.
2) The name of the constructor is the name of the class.
A method’s name is different from the class.
3) Constructor has no return type even void.
A member method is always with a return type.
4) Constructor is invoked at the time of object creation.
A member method is invoked by a method call.
Question 3
(e) Enter any two variables through constructor parameters and write a program to swap and print the values.
[2]
{
int a,b;
public Swap (int x, int y)
{
a=x;
b=y;
}
public void swap()
{
int c=a;
a=b;
b=c;
System.out.println("a = "+a+", b = "+b);
}
public static void main()
{
int a=5,b=6;
Swap ob = new Swap(a,b);
ob.swap();
}
}
2006
Question 1
(e) What is the purpose of the new operator? [2]
The new operator is used to create objects and arrays at runtime; it allocate memory for objects and arrays.
E.g.:
object creation: Rectangle rect1 = new Rectangle();
array creation: int a[] = new int[10];
Question 3
(e) What is a default constructor? [2]
* The constructor with default values is a default constructor. When we define a constructor with default values
such as 0 or null then this is a default constructor.
* And if we define a class without constructor then the compiler provides a constructer at the time of object creation
with default values such as 0 or null; so this also is considered as default constructor.
E.g.:
public Student( )
{
roll=0;
name= “”;
mark=0.0;
}
2007
Question 2
(b) Why do we need a constructor as a class member? [2]
A constructor is used to create objects by initializing proper values to the data members. It executes at the time of
object creation.
E.g.: Recatangle r = new Rectangle();
2008
Question 1
(a) Mention any two attributes required for a class declaration. [2]
The class keyword and class name.
Question 3
(g) What does the following mean?
Employee staff = new Employee();
[2]
An object named staff belonging to class Employee is created.
2009
Question 2
(b) What is the use of the keyword this? [2]
The this keyword is used to store the address of current object in order to avoid ambiguity when local variable names and instance variable names are same and class ariable names and instance variable names are same. The this keyword represents current object.
Question 2
(c) Why is a class known as composite data type? [2]
A class which is defined as data type to create objects and composed of various member variables and member methods is a
composite data type. E.g.:
public class Student
{
public int roll;
public String name;
public double mark;
}
Question 3
(g) Consider the following code and answer the questions that follow:
class academic
{
int x,y;
void access()
{
int a,b;
academic student =new academic();
System.out.println(“Object Created”);
}
}
(i) What is the object name of the class academic?
(ii) Name the class variables used in the program.
(iii) Write the local variables used in the program.
(iv) Give the type of the function used and its name.
[4]
(i) student
(ii) There is no class variable, but instance variables are x and y
(iii) a,b
(iv) member method/void. access().
2010
Question 3
(e) State two features of a constructor. [2]
1) The name of the constructor is name of the class and it is used to initialize values to variable.
2) It has no return type even void and it executes at the time of object creation.
2011
Question 3
(c) Explain the concept of constructor overloading with an example. [2]
Several constructors for a class with different initialization by different data types or different number of
arguments is known as constructor overloading. Note that the constructor names are same because name of the constructor
is name of the class. So different objects can initialize different values to their data members. E.g.:
class Shapes
{
float s;
double l,b;
Shapes(float x)
{
s=x;
}
Shapes(double x, double y)
{
l=x;
b=y;
}
public static void main()
{
Shapes sq=new Shapes(10.0f);
Shapes rect=new Shapes(8.5,4.3);
}
}
2012
Question 2
(a) Create a class with one integer instance variable. Initialize the variable using
(i) default constructor
(ii) parameterized constructor.
[2]
class A
{
int a;
A() //default constructor
{
a=0;
}
A(int x) //parameterized constructor
{
a=x;
}
public static void main()
{
A ob1=new A();
A ob2=new A(0);
}
}
Question 3
(j) In the program given below, state the name and the value of the
(i) method argument or argument variable
(ii) class variable
(iii) local variable
(iv) instance variable
class myClass {
static int x=7;
int y=2;
public static void main(String args[]) {
myClass obj = new myClass();
System.out.println(x);
obj.sampleMethod(5);
int a=6;
System.out.println(a);
}
void sampleMethod(int n) {
System.out.println(n);
System.out.println(y);
}
} [2]
(j) (i) String args[], int n
(ii) static int x
(iii) int a
(iv) int y
2013
Question 1
(d) Give a difference between a constructor and a method. [2]
The name of the constructor is same as class name and it is used to initialize legal values to instance variables..
The name of the method is different from the class and it is used to do some functions like input, process and output.
Question 2
(d) Name the Java keyword that:
(i) indicate that a method has not return type
(ii) Stores the address of the currently calling object
[2]
(i) void
(ii) this
2014
Question 2
(e) What is a constructor? When is it invoked? [2]
A constructor is a member method with the same name of class and it is used to initialize legal values to the data members. A constructor is invoked at the time of object creation.
Question 3
(a) List the variables from those given below that are composite data types.
(i) static int x
(ii) arr[i]=10;
(iii) obj.display();
(iv) boolean b;
(v) private char chr;
(vi) String str;
[2]
(ii) arr[i]
(iii) obj
(vi) str;
2015
Question 2
(d) Name the two types of constructors. [2]
Parameterized Constructor and Non-Parameterized constructor.
Question 3
(f) (ii) Name the keyword which is used to resolve the conflict between method parameter and instance variables/fields. [2]
this
2016
Question 1
(c) What is a parameterized constructor? [2]
(c) Parameterized constructor is a constructor that has parameter and intializes values to the data members when object is created.
Question 2
(g) Write one difference between primitive data types and composite data types. [2]
Primitive data types are fundamental data types defined by language developers. It can store only one value at a time. E.g.: int.
Composite data types are data types based on primitive data type as well as reference data type. Users also can define composite data types. It can contain multiple values. E.g.: a class named Student which consists int roll; String name; etc.
2017
Question 2
(e) Differentiate constructor and function: [2]
Constructor is member method with the name of class and it is used to initialize values to data members and it has no return type.
Name of a Function is different of class and it is used for computations like input, process and output and it has a return type.
2018
Question 2
(a) Write two characteristics of a constructor.
[2]
(i) A constructor is member function with its class name and it is used to create objects by initialising values to data memebrs.
(ii) A constructor has no return type and it is invoked at the time of object creation.
2020
Question 3
(c) What is constructor overloading? [2]
Several constructors having different initializations even though the names are same
that are differentiable by number of arguments or types of arguments is known as constructor overloading. E.g.:
class A
{
int a;
double b;
A()
{ a=3; }
A(double x)
{ b=x; }
}