JAVA OOPS Practice Questions
JAVA OOPS Practice Questions
Ques.1……………………………………………………..
create the class Course with the below Attributes.
courseId- int
courseName- String
courseAdmin- String
quiz- int
handson -int
sortCourseByHandsOn method:
This method will take an Array of Course Objects and int
output1:
35
kubernetes
Apache Spark
cassandra
getCountByType method:
this method will take two input parameters.
array of the Footwear objects and string parameter footwear
type.
this method will return the count of the footwears from array of
the
footwear objects for the given type of footwear.
getSecondHighestPriceByBrand method:
this method will take 2 input parameters-array of footwear
objects and string parameter inputFootwearName.the method
will return the second highest footwear objects based on the price
from the array of the Footwear objects
TestCases:
100
Sketchers
sneekers
12345
103
Puma
running shoes
10099
102
reebok
Sample output:
3
99
reebok
5666
Sample input2:
100
Puma
sneekers
12345
Sample output:
Footwear not available
Brand not available
Ques.3………………………………………………….
Create a class called Student with the below attributes:
findCountOfDayscholarStudents:
findStudentwithSecondHighestScore:
Input:
1001
Ashwa
IT
85
true
1002
Preeti
IT
70
false
1003
Uma
Output:
2
1002#Preeti#70.0
Ques.4………………………………………………...
Create a class College with the below attributes.
id-int
name -String
contactNo-int
address-String
pinCode-int
1.findCollegeWithMaximumPincode
2.searchCollegeByAddress
findCollegeWithMaximumPincode method:
This method will take array of the College objects and return the
found.
for this method ,main method will print College object with
maximum
then the main method will print "No college found with
mentioned attribute".
searchCollegeByAddress method:
for this method main method will print College object details as
it is ,
if the returned value is not null.if the returned value is null then ,
TestCases:
input 1:
4
109
ACT
2500256
OUTPUT1:
id-109
contactNo-2500256
address-mumbai
pincode-695001
id-102
name-SCT
contactNo-2500255
address-AP
pincode-523641
INPUT2:
id-110
name-ACT
contactNo-2500263
address-delhi
pincode-687945
id-110
name-ACT
contactNo-2500263
address-delhi
pincode-687945
Ques.5……………………………………………………….
Create a class Motel with the below attributes:
motelId - int
hotelId - int
hotelName - String
dateOfBooking – String (in the format dd-mon-yyyy)
noOfRoomsBooked – int
wifiFacility – String
noOfRoomsBookedInGivenMonth method:
This method will take two input parameter - array of Hotel
objects and a String parameter.
The method will return the total numbers of rooms booked from
array of Hotel objects for the given month(String parameter
passed).
If no rooms are booked for the given month in the array of Hotel
objects, then the method should return 0.
searchHotelByWifiOption method:
This method will take two input parameter - array of Hotel
objects and a String parameter
Note :
No two Hotel object would have the same hotelId.
No two Hotel object would have the same totalbill.
The Array either has at least two objects with specified wifi
option OR no object with specified wifi option.
All the searches should be case insensitive.
dateOfBooking is stored in the format dd-mon-yyyy(eg. 01-Jan-
2022)
Input1:
101
Best Stay
01-jan-2022
10
Yes
20000
102
Apple Stay
12-Feb-2022
3
Yes
4000
103
Output1:
5
103
Input2:
101
Best Stay
01-jan-2022
10
--------------------------------------------------
Sample code snippet for reference:
Please use below code to build your Solution.
--------------------------------------------------
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//code to read values
//code to call required method
//code to display the result
}
-------------------------------------------------
Note on using Scanner object:
Sometimes scanner does not read the new line character while
invoking methods like nextInt(), nextDouble() etc.
Usually, this is not an issue, but this may be visible while calling
nextLine() immediately after those methods.
Ques 7.
Create a class Book with below attributes
pages - int
title - String
author - String
price - double
findBookWithMaximumPrice :
=============================
Create a static method findBookWithMaximumPrice in the
Solution class. This method will take array of Book objects and
returns the Book object having the maximum Price if found else
return null if not found.
searchBookByTitle :
Input:
1
845
Bengali
Arijit
525.50
2
456
English
Raju
412.30
3
1022
History
Kaka
525.50
4
125
geography
MN
524
Output:
1 Bengali
3 History
2
456
Ques 8.
Create a class AutonomousCar with the below attributes:
carld – int
brand – String
noOfTestsConducted – int
noOfTestsPassed- int
environment – String
updateCarGrade method:
This method will take a String parameter brand, along with the
array of AutonomousCar objects. The method will return
the autonomousCar object, if the input String parameter matches
with the brand attribute of the autonomousCar object.
Before returning the object, the grade should be derived based on
the rating calculation mentioned below. This grade
value should be assigned to the object. If any of the above
conditions are not met, then the method should return null.
The grade attribute should be calculated as follows: rating .
(noOfTestsPassed * 100)/noOfTestsConducted
If the rating > = 80 then grade should be ‘A1’, otherwise the grade
should be ‘B2’.
Input
----------------
100
Tesla
1000
500
Hills
200
Output
-----------
1900
Mercedez::B2
Ques 9.
Create a class Phone with below attributes:
phoneId - int
findPriceForGivenBrand method:
----------------------------------
This method will take two input parameters - array of Phone
objects and string
parameter brand. The method will return the sum of the price
attribute from phone objects for the brand passed
as parameter. If no phones with the given brand is present in the
array of phone objects, then the method should
return 0.
getPhoneIdBasedOnOs method:
----------------------------------
This method will take a String parameter os, along with the array
of Phone objects.
Output
------------------
The given Brand is not available
222
rollNo - int
name - String
subject - String
grade - char
date - String [DD/MM/YYYY]
findStudentByGradeAndMonth Method:
for this method- main method should print Student name, subject
and total student found [The
length of the list], if the returned value is not null. If the returned
value is null then
main method should print "No student found".
NOTE:
1. For Taking char as input use sc.nextLine().charAt(0)
2. To match/check the month You havee to convert int
month in the parameter to String.
3. No need to count the Student array if return not null just
print the array length.
******************************************************************
**********
input1:
111
Arijit
Math
B
22/09/2023
101
Priyanka
English
A
30/03/2022
107
Shreosi
History
C
13/05/2022
105
Tatan
Physics
output1:
Priyanka
English
Tatan
Physics
2
input2:
111
Sohel
Math
B
22/09/2022
output 2:
No student found
Ques 11.
Create a class TravelAgencies with below attributes:
findAgencyWithHighestPackagePrice method:
agencyDetailsForGivenldAndType method:
Ques 12.
billNo- int
name - String
typeOfConnection - String
billAmount - double
status – boolean
findBillWithMaxBillAmountBasedOnStatus method:
-----------------------------------------------------
getCountWithTypeOfConnection method:
-----------------------------------------------------
This method will take two input parameters - array of Bill objects
and string parameter ( for type of connection).
The method will return the count of bills from array of bill objects
for the given type of connection.
If no bill with the given type of connection is present in the array
of bill objects, then the method should return 0.
Note :
If the returned value is null then it should print "There are no bill
with the given status".
4
111
Output:
222#Rekha Kumar
import java.util.Scanner;
public class Solution
{
public static void main(String[] args)
{
//code to read values
-------------------------------------------------
Note on using Scanner object:
Sometimes scanner does not read the new line character while
invoking methods like nextInt(), nextDouble() etc.
Usually, this is not an issue, but this may be visible while calling
nextLine() immediately after those methods.
1234
Merin Bakers
Ques 13.
songId - int
title - String
artist - String
duration-double
findSongDurationForArtist method:
----------------------------------------------------
getSongsInAscendingOrder method:
----------------------------------------------------
This method will take input parameters array, of Song objects and
String parameter.The method will return Song objects array
in an ascending order of their duration, from the array of Song
objects whose artist attribute matches with the input String
parameter.
Note:
----------
1. No two Song object would have the same songid.
2. Combination of artist and duration should be unique.
3. All the searches should be case insensitive.
For Example,
112
ABC
*****************************************************************
2150
In time
Justin Timberlake
4
250
Cry Me
Justin Timberlake
3
1200
Mirrors
Justin Timberlake
5
1300
That's the way it is
celion dion
5
500
Ashes
celion dion
3
Sample output1:
------------------------
8.0
250
Cry Me
2150
In time
1200
Mirrors
Sample Input 2:
----------------------
2150
Cry Me
Justin Timberlake
3
1000
Output 2:
-----------------------
Find the palindrome words from the sentence and print them and
also count
Input:
----------------
My name is nitin and I can speak malayalam
Output:
----------------
nitin
I
malayalam
3
Ques 15.
Count the number of words in a sentence that start and end with
the same letter.
Explanation: There are two words in the sentence that start and
end with the same letter - "Anna" and "Ginseng".
Input: 2345678910
Output: 17
Explaination: 2+3+5+7 = 17
Ques 17.
Write a Java program that creates three threads. First thread
displays “Good Morning” every one second, the second thread
displays “Hello” every two seconds and the third thread displays
“Welcome” every three seconds.
Ques 18.
Use inheritance to create an exception super class called
EexceptionA and exception sub class ExceptionB and ExceptionC,
where ExceptionB inherits from ExceptionA and ExceptionC
inherits from ExceptionB. Write a java program to demonstrate
that the catch block for type ExceptionA catches exception of type
ExceptionB and ExceptionC.
Ques 19
Write Java program(s) which uses the exception handling features
of the language, creates exceptions and handles them properly,
uses the predefined exceptions, and create own exceptions