You are on page 1of 5

JAVA Sample Questions for Practice (II CSE – ‘A’ & II IT – ‘B’)

1. Implement a Java program to define a class called ‘Mobilephone’ with Model Number,
Manufacture Name, Cost, Size, Color, MemoryCapacity, CameraPixelSize, etc.,. Include
various types of constructors such as Default constructor, Parameterized constructor with
two arguments, three arguments and four arguments. Create objects to invoke various
types of constructors and demonstrate polymorphism.

2. Define a class named ‘Vehicle’ with data members - name, model number, price, service
station name; add constructor and display() method. Define another class ‘Bike’ that
extends the class ‘Vehicle’ with data member discount rate and methods constructor,
display() ,discount() to compute discount. Use object of the extended class to compute the
discount and display the details of the vehicle, inherited from its parent.

3. Implement the following thru JAVA

4. Create a class called Book that contains instance variables like BKName, BKId and
BKAuthor, a parameterized constructor to initialize its instance variables, a method
BKUpdateDetails(String name, int id, String author), that accepts new values for name, Id
and author as parameters and updates the corresponding instance variable values of that
object and another method BKDisplay() to display the book details. Create a class
BookDemo and provide main method for instantiate a Book object, display the original
book details, update its details with new values, and display the updated book details.

If any query, pl. contact : Dr. K. Rajkumar/ CSE(rajkumar@cse.sastra.edu) Page 1


JAVA Sample Questions for Practice (II CSE – ‘A’ & II IT – ‘B’)

5. Create a Point class that has two data members x and y of double type. Write a default
constructor which initializes its data members to zero. Write a parameterized constructor
which takes two parameters of double type and assigns them to its data members. Write
another parameterized constructor that takes one Point object as parameter and copies
the values of the passed object’s data member to the calling object’s data members. Write
a find_distance method that takes two double parameters representing the x, y values of a
point and finds the distance between the calling object and the passed parameters and
returns the distance as double value. Overload the find_distance method, that takes single
Point object parameter and computes the distance between both the points and returns
the distance as a double value. Write a display method to print the point in the format of
“(x, y)”. In main method create three point objects p1, p2 and p3. Initialize p1 with (3.25,
7.89), p2 with (5.37, 18.12) and p3 with p2. Find distance between p1 and (7.9, 16.25) using
first find_distance method and between p1 and p3 using secondfind_distancemethod.

6. Create an abstract class called Shape2d that has two data members of type double and
two abstract methods area() and display(). Create two derived classes Rectangle and
Triangle. In both classes define the method area() to compute the area of that shape and
return it as a double value and define the method display() to display the value of the data
members with appropriate caption and the calculated area. Create a class called
Shape2dDemo and provide main method to instantiate the objects of Rectangle and
Triangle for demonstration of the above classes.

7. Create a package called shape2d. In that package declare an interface called twod and
three classes Square, Rectangle and Circle. In all three classes implement the twod
interface. In interface twod declare two methods area() and perimeter(). Both returns
double value. Square class contains one instance variable called side of double type, a
parameterized constructor for initializing that member, area() and perimeter() methods for
computing and returning the area and perimeter of the square using appropriate formulae.
Similarly Rectangle class contains two instance variables called length and breadth of
double type, a parameterized constructor with two parameters for initializing those
members, area() and perimeter() methods for computing and returning the area and
perimeter of the rectangle using appropriate formulae. Circle class contains one instance
variable called radius of double type, a parameterized constructor for initializing that
member, area() and perimeter() methods for computing and returning the area and
perimeter of the circle using appropriate formulae.

Create a class ShapeDemo outside the packages and declare a main method for creating
objects of all 2D shapes, calculate area and perimeter accordingly using the appropriate
interface references and display.

If any query, pl. contact : Dr. K. Rajkumar/ CSE(rajkumar@cse.sastra.edu) Page 2


JAVA Sample Questions for Practice (II CSE – ‘A’ & II IT – ‘B’)

8. Create a package named pkbanking. In that package define a sub package named
pkinterface. The package pkinterface contains an interface named Transaction with a data
member min_balance initialized to 500, two methods namely void withdraw(double) and
void deposit(double) and another interface named InterestRate with a data member sbrate
with 4%.

Create another package named pkaccount. In that package create an abstract class called
Account that has the protected data members accnumber of int type, balance of double,
constructors for initialization. Define a subpackage sb in pkaccount. In sb package derive a
class called SBAccount from Account class that implements both Transaction and
InterestRate interfaces. Also it has constructors for initialization using super class
constructors, deposit(double), withdraw(double), and calc_interest() methods. Provide a
parameterized constructor with two parameters account number, and init_balance and
assign these values to the appropriate data members of the class using super class
constructor. The deposit method should take one double type argument amount and adds
the amount to the balance if the amount is positive. The withdraw method, should take
one double type argument amount and checks if balance - amount is greater than
min_balance. If so, it should subtract the amount from balance. Otherwise it should display
error message. The calc_interest() method should compute interest for the balance
amount available @ sbrate p.a. and the interest amount should be credited to the balance.

Create third package named pkcustomer which contains a class Customer. The data
members of the class are cust_id of int type, name, and address of string type, and object
of SBAccount. Provide parameterized constructor with cust_id, name, address, account
number, initial balance as parameters for opening a SB account for new customer. Provide
a method transaction(int type) {type may be withdraw, deposit or calc_interest to perform
the requested transaction on the requested Account object.

To demonstrate all the functionalities of above classes, create a class called BankDemo and
declare a main method to create an array of 5 elements of Customer objects, and perform
manipulations on the objects by creating SB account, depositing and withdrawing amount
from SB accounts.

9. Write a Java program to create a student performance evaluation system for a particular
section. There are three packages pkInterface, pkClass and pkDemo. The pkInterface
contains three interfaces named StuPersonal having displayPersonalDetails(), StuAcademic
having displayAcademicDetails() & StuOtherDetail having displayOtherDetails() and all
these methods returns nothing. The pkClass package contains a class Student that
implements all three interfaces of pkInterface package. The Student class has data
members name, regno, age, marks in three subjects, total, average, result, sportsName,
sportsScore. The pkDemo package contains a class named like TestMain. In TestMain
create an array of Student to hold 5 students’ information. Get input for all students.
Perform the following for each student: Compute total and average for each student;
Display all details of the student.

If any query, pl. contact : Dr. K. Rajkumar/ CSE(rajkumar@cse.sastra.edu) Page 3


JAVA Sample Questions for Practice (II CSE – ‘A’ & II IT – ‘B’)

10. Write a Java program to create a student performance evaluation system for a
particular section. There are four packages pkInterface, pkClass, pkException, pkDemo. The
pkInterface contains three interfaces named StuPersonal having displayPersonalDetails(),
StuAcademic having displayAcademicDetails() & StuOtherDetail having
displayOtherDetails() and all these methods returns nothing. The pkClass package contains
a class Student that implements all three interfaces of pkInterface package. The Student
class has data members name, regno, age, marks in three subjects, total, average, result,
sportsName, sportsScore The pkException package contains user defined exceptions
AgeLimitCrossedException, StudentFailedException, and ExcellentStudentException, The
pkDemo package contains a class named like TestMain. In TestMain create an array of
Student to hold 5 students’ information. Get input for all students. Perform the following
for each student: Compute total and average for each student; Display all details of the
student; If the age is not in the range 17 to 25 raise AgeLimitCrossedException; If mark in
any subject is less than 50 raise StudentFailedException; If the total marks is above 270 and
sportsScore is above 7 raise ExcellentStudentException.

11. Create a package called complex, which contains two sub-packages inter and number.
In inter sub-package create an interface called Arithmetic Operations that has four
methods add, subtract, multiply and divide. All these methods take object of type Complex
as argument and also returns object of type Complex. In number sub-package create a class
called Complex that implements Arithmetic Operations interface. The Complex class has
two private numbers realPart and imaginaryPart of double type, one parameterized
constructor that takes two arguments and initializes its data members. Override the
toString() method to display the complex number in the format “(realPart, imaginaryPart
i)”. Outside the complex package create a class called ComplexArithmeticDemo to take
input for two complex numbers from the user thru command line, check if the first number
is between (-50, -50i) and (50,50i) and second number is between (-30, -30i) and (30, 30i). If
so perform all the four arithmetic operations and print their results. Otherwise print the
message along with the complex number that is not valid.

12. Write a JAVA program to find average(average=(min+max)/2) from given set of


unsorted array elements of type int using multithreads. One thread is used to sort array
elements in ascending order. You should take the minimum and maximum from sorted
array only. Another thread is used to find average. Sort thread must be executed before
average thread. Use synchronized mechanism with wait() and notify() to get correct result.
Also apply your own exception handling mechanism if your input is negative in an array.
Input must be thru command line arguments.

13. t is possible to estimate the value of π using a random process. The logic behind the
estimation is as follows: The area of a circle of radius 1 is equal to π, and the area of a
quarter of that circle is π/4. Here is a picture of a quarter of a circle of radius 1, inside a 1-
by-1 square: The area of the whole square is one, while the area of the part inside the circle
is π/4. If we choose a point in the square at random, the probability that it is inside the
circle is π/4. If we choose N points in the square at random, and if C of them are inside the
circle, we expect the fraction C/N of points that fall inside the circle to be about π/4. That
is, we expect 4*C/N to be close to π. If N is large, we can expect 4*C/N to be a good
estimate for π, and as N gets larger and larger, the estimate is likely to improve. Hence
write a program in java to estimate the value of using Threads as follows: Create a class
called Point that has two private data members x and y of double type. Provide a 2-

If any query, pl. contact : Dr. K. Rajkumar/ CSE(rajkumar@cse.sastra.edu) Page 4


JAVA Sample Questions for Practice (II CSE – ‘A’ & II IT – ‘B’)

argument constructor to initialize them. Provide a method called liesInCircle() that returns
true if the point lies inside the circle. Note that a point (x,y) lies inside the circle if
SQRT(x2+y2)< 1. Create a thread ComputePI that has nTrials as private member of long type
to store number of points generated and nPass as private member of long type to store the
points that lies inside the circle. In the run() method generate 10 lakhs pairs of random
numbers within 0 to 1 using Math.random(), and increment nPass if the point lies inside the
circle. Provide a method getEstimatedPI() that computes the ratio between number of
points that lies inside the circle (nPass) and number of points generated (nTrials) and
returns the 4 times of the ratio computed (4*nPass / nTrials) as a double value. Create a
class called PIDemo that estimates the value of PI by creating n number of threads and
prints the average of the result from all threads.

If any query, pl. contact : Dr. K. Rajkumar/ CSE(rajkumar@cse.sastra.edu) Page 5

You might also like