Constructors
Constructors
Kakarla’s
INTERNATIONAL SCHOOL, HYDERABAD
Revision Worksheet
Constructors
Name:
Subject: Computer Applications
Class X Sec: __ , Roll No.____
Multiple Choice Questions
Question 1
For which purpose a constructor is used?
1. To initialise the instance variables
2. To provide the instance variables
3. To organise the instance variables
4. To simplify the instance variables.
Answer
To initialise the instance variables
Reason — A constructor is used to initialise data members with default or user-
defined values at the time of creation of an object.
Question 2
Which of the following statements is false?
1. A constructor has same name as class name.
2. A constructor returns initial value.
3. A constructor is called while creating an object.
4. A constructor is not used for arithmetical and logical operations.
Answer
A constructor returns initial value.
Reason — A constructor has no return type as its primary function is to set initial
values of data members at the time of creation of an object.
Question 3
A constructor is always:
1. private
2. protected
3. secure
4. public
Answer
public
Reason — A constructor is always public because it is always called from outside the
class while creating an object.
Question 4
Which constructor initialises data members with default values?
1. Parameterized constructor
2. Non-parameterized constructor
3. Copy constructor
4. Default constructor
Answer
Default constructor
Reason — Default constructor initialises data members with default values.
Page 1 of 24
Question 5
What is not true for a constructor?
1. It is used only to initialize the data members.
2. It returns a unique value.
3. It may not use parameter.
4. None of the above.
Answer
It returns a unique value.
Reason — A constructor has no return type as it doesn't return any value.
Fill in the blanks
Question 1
A member function having the same name as that of the class name is
called constructor.
Question 2
A constructor has no return type.
Question 3
A constructor is used when an object is created
Question 4
A constructor without any argument is known as non-parameterised constructor.
Question 5
Parameterised constructor creates objects by passing value to it.
Question 6
The this keyword refers to the current object.
Question 7
The argument of a copy constructor is always passed by reference.
Question 8
The constructor generated by the compiler is known as default constructor.
Page 3 of 24
Question 3
Why do we need a constructor as a class member?
Answer
A constructor is used to initialize the objects of the class with a legal initial value.
Question 4
Explain the following terms:
(a) Constructor with default argument
(b) Parameterised constructor
(c) Copy constructor
(d) Constructor overloading
Answer
(a) Constructor with default argument — Java specification doesn't support default
arguments in methods so Constructor with default argument cannot be written in
Java.
(b) Parameterised constructor — A parameterised constructor is a member method
with the same name as the class name which is used to initialize the instance
variables with the help of parametric values, passed at the time of creating an object.
(c) Copy constructor — A constructor used to initialize the instance variables of an
object by copying the initial values of the instance variables from another object is
known as Copy Constructor.
(d) Constructor overloading — The process of using a number of constructors with
the same name but different types of parameters is known as Constructor
overloading.
Question 5
Why is an object not passed to a constructor by value? Explain.
Answer
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.
Page 4 of 24
Question 6
State the difference between constructor and method.
Answer
Constructor Method
It has the same name as class It should have a different name than class
name. name.
Page 5 of 24
Question 9
Name two ways of creating objects in a constructor.
Answer
The object can be created in two ways:
1. Created by compiler
2. Created by programmer
Question 10
Differentiate between the following statements:
abc p = new abc();
abc p = new abc(5,7,9);
Answer
The first statement abc p = new abc(); is calling a non-parameterised constructor to
create and initialize an object p of class abc.
The second statement abc p = new abc(5,7,9); is calling a parameterised constructor
which accepts three arguments to create and initialize an object p of class abc.
Question 11
Fill in the blanks to design a class:
class ...............
{
int l, b;
Area(int ..............., int ...............) {
l = ...............;
b = ...............;
}
}
Answer
class Area
{
int l, b;
Area(int x, int y) {
l = x;
b = y;
}
}
Solutions to Unsolved Java Programs
Question 1
Write a program by using a class with the following specifications:
Class name — Hcflcm
Data members/instance variables:
1. int a
2. int b
Member Methods:
1. Hcflcm(int x, int y) — constructor to initialize a=x and b=y.
2. void calculate( ) — to find and print hcf and lcm of both the numbers.
import java.util.Scanner;
int hcf = x;
int lcm = (a * b) / hcf;
Page 7 of 24
Question 2
An electronics shop has announced a special discount on the purchase of Laptops as
given below:
Category Discount on Laptop
Up to ₹25,000 5.0%
Page 8 of 24
dis = price * 0.15;
Question 3
Write a program by using a class with the following specifications:
Class name — Calculate
Instance variables:
1. int num
2. int f
3. int rev
Member Methods:
1. Calculate(int n) — to initialize num with n, f and rev with 0 (zero)
2. int prime() — to return 1, if number is prime
3. int reverse() — to return reverse of the number
4. void display() — to check and print whether the number is a prime palindrome
or not
import java.util.Scanner;
public Calculate(int n) {
Page 9 of 24
num = n;
f = 0;
rev = 0;
}
f = 1;
if (num == 0 || num == 1)
f = 0;
else
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
f = 0;
break;
}
}
return f;
}
int t = num;
while (t != 0) {
int digit = t % 10;
rev = rev * 10 + digit;
t /= 10;
}
return rev;
}
Page 10 of 24
Calculate obj = new Calculate(x);
obj.prime();
obj.reverse();
obj.display();
}
}
Output
Question 4
Define a class Arrange described as below:
Data members/instance variables:
1. String str (a word)
2. String i
3. int p (to store the length of the word)
4. char ch;
Member Methods:
1. A parameterised constructor to initialize the data member
2. To accept the word
3. To arrange all the alphabets of word in ascending order of their ASCII values
without using the sorting technique
4. To display the arranged alphabets.
Write a main method to create an object of the class and call the above member
methods.
import java.util.Scanner;
public Arrange(String s) {
str = s;
i = "";
p = s.length();
ch = 0;
}
Question 5
Write a program by using a class in Java with the following specifications:
Class name — Stringop
Data members:
1. String str
Member functions:
1. Stringop() — to initialize str with NULL
2. void accept() — to input a sentence
3. void encode() — to replace and print each character of the string with the
second next character in the ASCII table. For example, A with C, B with D and
so on
4. void print() — to print each word of the String in a separate line
import java.util.Scanner;
public Stringop() {
str = null;
}
Question 6
The population of a country in a particular year can be calculated by:
p*(1+r/100) at the end of year 2000, where p is the initial population and r is the
growth rate.
Write a program by using a class to find the population of the country at the end of
each year from 2001 to 2007. The Class has the following specifications:
Class name — Population
Data Members — float p,r
Member Methods:
1. Population(int a,int b) — Constructor to initialize p and r with a and b
respectively.
2. void print() — to calculate and print the population of each year from 2001 to
2007.
import java.util.Scanner;
Page 13 of 24
public class Population
{
private float p;
private float r;
Question 7
Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0 with
the following specifications:
Class name — Quad
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is the discriminant), r1
and r2 are the roots of the equation.
Member Methods:
1. quad(int x,int y,int z) — to initialize a=x, b=y, c=z, d=0
2. void calculate() — Find d=b2-4ac
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2a
import java.util.Scanner;
Page 14 of 24
public class Quad
{
private float a;
private float b;
private float c;
private float d;
private float r1;
private float r2;
Page 15 of 24
Question 8
Define a class named FruitJuice with the following description:
Data Members Purpose
String flavour stores the flavour of the juice (e.g., orange, apple, etc.)
int pack_size stores package size (e.g., 200 mL, 400 mL, etc.)
int
stores the price of the product
product_price
Member
Purpose
Methods
to input and store the product code, flavour, pack type, pack
void input()
size and product price
to display the product code, flavour, pack type, pack size and
void display()
product price
import java.util.Scanner;
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
Question 9
The basic salary of employees is undergoing a revision. Define a class called
Grade_Revision with the following specifications:
Data Members Purpose
Page 17 of 24
Member
Purpose
Methods
Experience Increment
public Grade_Revision() {
name = "";
bas = 0;
expn = 0;
inc = 0.0;
nbas = 0.0;
}
Page 18 of 24
public void increment() {
if (expn <= 3)
inc = 1000 + (bas * 0.1);
else if (expn <= 5)
inc = 3000 + (bas * 0.12);
else if (expn <= 10)
inc = 5000 + (bas * 0.15);
else
inc = 8000 + (bas * 0.2);
Question 10
Define a class called Student to check whether a student is eligible for taking
admission in class XI with the following specifications:
Data Members Purpose
Member
Purpose
Methods
Page 19 of 24
Member
Purpose
Methods
Marks Eligibility
Average marks 80% or more and less than 90% Science with Hindi
Write the main method to create an object of the class and call all the member
methods.
import java.util.Scanner;
return course;
}
Member
Purpose
Methods
Page 21 of 24
Units consumed Rate
public Bill() {
bno = 0;
name = "";
call = 0;
amt = 0.0;
}
Member
Purpose
Methods
void input( ) To input and store the name and price of the book
void display( ) To display the name and price of the book after discount
Price Discount
More than ₹1000 and less than or equal to ₹3000 10% of price
public BookFair() {
bname = "";
price = 0.0;
}
price -= disc;
}
Page 24 of 24