Constructors:-
Very short answer type questions:-
1. No, constructor cannot be static or final
2. A constructor is a special method of a class or structure in object-oriented programming
that initializes an object of that type. A constructor is an instance method that usually has
the same name as the class, and can be used to set the values of the members of an object,
either to default or to user-defined values.
3. Constructor is of two types:- default constructor and parameterized constructor.
4. Default constructor cannot take any parameter.
5. This keyword is used to call a constructor within a constructor.
6. The need of the constructor is to initialize the variables/values. It is the "initialization
function".
7. Data members are declared as private members. So, they cannot be initialized outside the
class with values. And, with the help of constructors we can initialize the objects
implicitly as they are created, which help in data hiding property of OOP.
8. (a) A constructor will have the same name as that of the class.
(b) A constructor does not have a return type not even void.
9. Temporary instances are instances that are instantiated in order to invoke a method or
chain of methods, and then are immediately discarded. They are created by writing new
operator and then calling the function with dot operator at the same place.
10. Yes, we can assign one object to another with the help of reference variable method.(give
example from Ans 7 of solved questions).
11. Similarity between methods and constructors are:-
1. They both have parameter lists.
2. They both have blocks of code that will be executed when that block is either called
directly (methods) or invoked via new (constructors).
12. The 'new' operator in java is responsible for the creation of new object or we can say
instance of a class. Actually, it dynamically allocates memory in the heap with the
reference we define pointed from the stack
13. Constructor is called when an object is created. If we do not define any constructors, java
will create a default constructor, and that will be called when object is created. example:-
class abc
{
abc( )
{ [Link](“constructor is called”); }
public static void main( )
{
abc obj=new abc( ); //constructor abc( ) is called here
}
}
14. JAVA statement:-
digital mp4 = new digital( );
SHORT QUESTIONS:-
1. Following are the differences between constructor and method.(you can see answer from
ppt. Also)
Constructor is used to initialize an object whereas method is used to exhibits
functionality of an object.
Constructors are invoked implicitly whereas methods are invoked explicitly.
Constructor does not return any value where the method may/may not return a value.
In case constructor is not present, a default constructor is provided by java compiler.
In the case of a method, no default method is provided.
Constructor should be of the same name as that of class. Method name should not be
of the same name as that of class.
[Link] holds memory and it is created using new keyword.
The Object reference is nothing but reference to that memory. It only points to memory
that objects hold.
As per the example
book b1 = new book ( ); //object b1 is declared of class book
book b2 = b1; //object reference is created. b2, b1 both referenced the same
memory location
3. Temporary instances are temporary objects. They are allocated the system memory as long
as the code requires it and then the memory is freed up to heap memory. On the other hand,
objects are created by using the new operator and a constructor. The new operator
allocates memory space for objects.
book b1 = new book( ); // object is created.
new book(24, abc).display( ); //temporary instance is created.
4. Constructor overloading is a concept of having more than one constructor with different
parameters list, in such a way so that each constructor performs a different task.
class Box
{
double width, height, depth;
// constructor used when all dimensions specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box()
{
width = height = depth = 0;
}
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various constructors
Box mybox1 = new Box(3, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = [Link]();
[Link](" Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link](" Volume of mybox2 is " + vol);
// get volume of cube
vol = [Link]();
[Link](" Volume of mycube is " + vol);
}
}
Output:
Volume of mybox1 is 9000.0
Volume of mybox2 is 0.0
Volume of mycube is 343.0
5. A constructor is created using the new operator followed by the name of the class.
Syntax:-
<access specifier> class_name(parameter list/void)
{
data_member1 = value1;
}
example:-
public class abc
{
abc( ) //constructor abc
{
[Link](“constructor”);
}
}
6. Ans 5 of solved questions.
7. Yes, one constructor can call another default or parameterized constructor using this
keyword.
8. class MyClass
{
int age; //instance variable.
MyClass ( )
{ age = 14;}
public static void main( )
{
MyClass obj = new MyClass( );
int result = age + 1;
[Link](“Age of a Student is “ +result);
}
}
9.
class Box
{
double width, height, depth;
//parameterized constructor
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// default constructor
Box()
{
width = height = depth = 0;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various constructors
Box mybox1 = new Box(3.89, 2.1, 1.5);
Box mybox2 = new Box();
double vol;
// get volume of first box
vol = [Link]();
[Link](" Volume of mybox1 is " + vol);
// get volume of second box
vol = [Link]();
[Link](" Volume of mybox2 is " + vol);
}
}
Output:
Volume of mybox1 is 12.2535
Volume of mybox2 is 0.0
10. public class dob
{
static int dd, mm, yy,year;
dob( )
{ dd = mm = yy = 0; }
dob(int date, int month, int years)
{
dd = date; mm = month; yy = years;
}
public void plus(int yr)
{
year = yy + yr;
}
public void disp( )
{
[Link](“ ……….Actual Date of birth……….”);
[Link](dd+”/”+mm+”/”+yy);
[Link](“changed date of birth”);
[Link](dd+”/”+mm+”/”+ year);
}
public static void main(String[] args )
{
dob me = new dob(12,12,1980);
dob friend = new dob(1,5,1982);
[Link](7);
[Link](“for me”);
[Link]( );
[Link](9); [Link](“for my friend”);
[Link]( );
}
}
11. public class vehicle
{
static int numOfPassengers, fuelCapacity, fuelConsumed;
vehicle(int passengers, int capacity, int fuel)
{
numOfPassengers = passengers;
fuelCapacity = capacity;
fuelConsumed = fuel;
}
public void display( )
{
[Link](“ Number of passengers “+numOfPassengers);
[Link](“ Fuel Capacity in liters “+fuelCapacity);
[Link](“ Fuel Consumption in km/l“+fuelConsumed);
}
public static void main( )
{
vehicle twoWheeler = new vehicle(2 ,13 ,72);
vehicle fourWheeler = new vehicle(7, 42,16);
[Link](“------------Two Wheeler------------“);
[Link]( );
[Link](“------------Four Wheeler------------“);
[Link]( );
}
}
12.
import [Link];
public class FruitJuice {
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
public FruitJuice()
{
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
public void input() {
Scanner scanner = new Scanner([Link]);
[Link]("Enter product code: ");
product_code = [Link]();
[Link]("Enter flavour: ");
flavour = [Link]();
[Link]("Enter pack type: ");
pack_type = [Link]();
[Link]("Enter pack size: ");
pack_size = [Link]();
[Link]("Enter product price: ");
product_price = [Link]();
public void discount() {
product_price = (int) (0.9 * product_price);
public void display() {
[Link]("Product Code: " + product_code);
[Link]("Flavour: " + flavour);
[Link]("Pack Type: " + pack_type);
[Link]("Pack Size: " + pack_size);
[Link]("Product Price: " + product_price);
}
}
13.
import [Link];
public class Travels {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter sno: ");
int sno = [Link]();
[Link]("Enter name: ");
String name = [Link]();
[Link]("Enter ticket charges: ");
int charges = [Link]();
int discountPercentage = 0;
if (charges > 70000) {
discountPercentage = 18;
} if (charges >= 55001 && charges <= 70000)
{ discountPercentage = 16; }
if (charges >= 35001 && charges <= 55000) { discountPercentage =
12; }
if (charges >= 25001 && charges <= 35000)
{ discountPercentage = 10; }
if (charges < 25001) {
discountPercentage = 2;
}
double discount = discountPercentage * charges / 100.0;
double netAmount = charges - discount;
[Link]("[Link].\t Name\t Ticket Charges\t Discount\t Net Amount");
[Link](sno + "\t" +name+"\t" + charges + "\t" + discount + "\t" + netAmount);
}
}
14. public class rectangle
{
static float l, w;
rectangle( )
{
l=10; w=2;
}
rectangle(float a, float b)
{
l = a; w = b;
}
public void showdata( )
{
[Link](“ length = “+l+” breadth = “+w);
}
public static void main(String[] args )
{
rectangle r1 = new rectangle( );
[Link](“-----default constructor-----“);
[Link]( );
rectangle r2 = new rectangle(19.3f , 23.5f);
[Link](“-----parameterized Constructor----“);
[Link]( );
}
}
15. (i)import [Link];
class find
{
static int num;
find(int y) { num = y; }
public static void disp( )
{
if(num%2==0)
{
[Link](num+ “ is even number “);
}else
{ [Link](num+ “ is odd number “);}
}
}
(ii) defining another class
public class another
{
public static void main(String[] args )
{ int a;
Scanner sc= new Scanner([Link]);
a = [Link]( );
[Link](“ enter the number to check “);
find obj = new find(a);
[Link]( );
}
}
16. (i) class calculate
{
static int num;
calculate(int y)
{ num = y; }
public void reverse( )
{ int n = num ,reverse=0;
while(n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
[Link]("Reversed number is " + reverse);
}
public void prime( )
{ int temp;
boolean isPrime=true;
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
[Link](num + " is a Prime Number");
else
[Link](num + " is not a Prime Number"); }
public void palindrome( )
{
int r,sum=0,temp; int n = num;
temp=num;
while(n> 0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
[Link]("palindrome number ");
else
[Link]("not palindrome");
}
}
(ii) class demo
{
public static void main(String[ ] args )
{
Scanner sc = new Scanner([Link]);
[Link](“ enter the number” );
int a = [Link]( );
calculate obj = new calculate(a);
[Link]( );
[Link]( );
[Link]( );
}
}
17. public class vol
{ static double r,h;
vol(double radius, double height)
{ r = radius; h = height;}
public void volume( )
{
double volume, r1, h1;
r1 = r; h1 = h;
volume = [Link] * r1*r1*h;
[Link](“ volume of the cylinder “ + volume);
}
public static void main( )
{
vol obj = new vol(2,7);
[Link]( );
}
}