Java QP Descriptive
Java QP Descriptive
1 Take an object “Mobile Phone” and realize the following OOPs concept Easy
1: Identify the object and determine the essential attributes and behaviors for
the object.
2: Realize the concept of class as a blueprint of the mobile phone.
3: How is abstraction employed in the mobile phone to facilitate user
interaction? What is the abstraction for the following user levels: User,
Designer, Engineer, and Servicemen.
4: How is encapsulation applied in the mobile?
5: Realize the concept of inheritance and the types of inheritance.
6: How does the concept of polymorphism enable flexible handling of different
interactions?
7: Realize association and its types – aggregation and composition.
1
Question Bank Core Java
Questio
Question Detail Level
n No.
ARRAYS
Problem Statement:
Input Format:
Output Format:
Constraints:
1
1 ≤ N ≤ 50
Test Cases:
Input 1:
Enter value N: 6
Output 1:
Input 2:
Enter value N: 5
Output 2:
2
Question Bank Core Java
Number of odd elements: 5
Input 3:
Enter value N: 0
Output 3:
Input 4:
Enter value N: 4
Output 4:
Problem Statement:
Input Format:
Output Format:
Constraints:
1 ≤ N ≤ 20
3
Question Bank Core Java
Test Cases:
Input 1:
Output 1:
Rearranged array: -3 -1 -2 5 8 4
Input 2:
Output 2:
Input 3:
Output 3:
Input 4:
Output 4:
Problem Statement:
Input Format
Output Format
4
Question Bank Core Java
Print the GCD of the array in the form of a sentence.
Constraints
1 ≤ N ≤ 50
Test Cases:
Input 1:
Output 1:
The GCD of the array is: 10
Explanation:
10 divides all numbers (10, 20, 30, 40, 50). No greater number
divides all.
Input 2:
Enter the number of elements: 4
Enter array elements: 7 14 21 28
Output 2:
The GCD of the array is: 7
Explanation:
7 is the largest number that divides all elements.
Input 3:
Enter the number of elements: 0
Enter array elements:
Output 3:
Invalid Input: The number of elements must be at least 1.
Input 4:
Enter the number of elements: 3
Enter array elements: 12 -6 18
Output 4:
Invalid Input: Array elements must be positive integers only.
Problem Statement:
5
Question Bank Core Java
The first element should be the maximum,
Input Format
Output Format
Constraints
1 ≤ N ≤ 50
Test Cases:
Input 1:
Enter the number of elements: 6
Enter array elements: 1 2 3 4 5 6
Output 1:
Rearranged array: 6 1 5 2 4 3
Explanation:
Max = 6, Min = 1, 2nd Max = 5, 2nd Min = 2, 3rd Max = 4, 3rd
Min = 3.
Input 2:
Enter the number of elements: 5
Enter array elements: -3 -1 2 4 7
Output 2:
Rearranged array: 7 -3 4 -1 2
Explanation:
Max = 7, Min = -3, 2nd Max = 4, 2nd Min = -1, Middle = 2.
Input 3:
Enter the number of elements: 0
Output 3:
Invalid Input: The number of elements must be at least 1.
6
Question Bank Core Java
Input 4:
Enter the number of elements: 4
Enter array elements: 10 20 150 30
Output 4:
Invalid Input: Array elements must be between -100 and 100.
Input Format
Output Format
Constraints
1 ≤ N ≤ 100
Test Cases:
Input 1:
Output 1:
Input 2:
Output 2:
7
Question Bank Core Java
Input 3:
Output 3:
Input 4:
Output 4:
Input Format:
Output Format:
Constraints:
3 ≤ N ≤ 50
Test Cases:
Input 1:
Enter number of elements: 5
Enter array elements: 4 6 3 7 5
Output 1:
Total number of possible triangles: 7
8
Question Bank Core Java
Explanation:
Valid triangles are formed by sides (3,4,5), (4,5,6), (3,6,7), etc.
Input 2:
Enter number of elements: 4
Enter array elements: 10 21 22 100
Output 2:
Total number of possible triangles: 2
Explanation:
Only (10,21,22) and (21,22,100) satisfy the triangle property.
Input 3:
Enter number of elements: 2
Enter array elements: 5 7
Output 3:
Not enough elements to form a triangle.
Input Format
Output Format
7
If found: X found at row R and column C
Constraints
1 ≤ M, N ≤ 50
Test Cases:
Input 1:
9
Question Bank Core Java
123
456
Enter the number to search: 5
Output 1:
Input 2:
Output 2:
Input 3 :
Output 3:
Input 4:
Output 4:
Write a program that reads a matrix of size MxN from the user
and prints its transpose. The program should first ask for the
number of rows and columns, then accept the matrix elements
row by row, and finally display the transposed matrix. If the
number of rows or columns is outside the allowed range, the
8 program should print an appropriate error message.
Input Format
Output Format
10
Question Bank Core Java
Print the transposed matrix
Constraints
1 ≤ M, N ≤ 50
Test Cases:
Input 1:
Output 1:
Transpose of matrix:
14
25
36
Input 2:
Transpose of matrix:
7 9 11
8 10 12
Input 3:
Output 3:
Input 4:
Output 4:
11
Question Bank Core Java
Problem Statement: m
Given a jagged matrix (rows may have different number of
columns), compute and display the sum of each row.
Input Format:
Output Format:
Constraints:
1 ≤ number of rows ≤ 10
1 ≤ element ≤ 100
Test Cases:
Input 1:
Output 1:
Sum of row 1: 3
Sum of row 2: 12
Sum of row 3: 6
Explanation:
Row 1: 1 + 2 = 3
Row 2: 3 + 4 + 5 = 12
Row 3: 6 = 6
12
Question Bank Core Java
Input 2:
Output 2:
Sum of row 1: 60
Sum of row 2: 20
Explanation:
Row 1 sum: 10 + 20 + 30 = 60
Row 2 sum: 5 + 15 = 20
Input 3:
Input 4 :
Output 4:
STRINGS
13
Question Bank Core Java
it with the main string.
Input Format:
Output Format:
Test cases:
Sample Input/Output:
Menu:
1. Find Length
2. Trim Spaces
3. Concatenate
4. Character at Index
14
Question Bank Core Java
6. Starts With / Ends With
7. Compare Strings
9. Convert to Characters
12. Exit
Enter index: 5
Character at index 5: P
15
Question Bank Core Java
Java
Programming
Exiting program.
Given a string, reverse only the vowels (a, e, i, o, u). Keep all
other characters in the same position.
11
Input Format
Enter a string: S
Output Format
16
Question Bank Core Java
Constraints
1 ≤ length(S) ≤ 50
Test Cases:
Input 1:
Output 1:
Explanation:
Vowels: e, o → reversed → o, e
Input 2:
Output 2:
Explanation:
Vowels: o, a, i → reversed → i, a, o
Input 3 :
Output 3:
Input 4:
Enter a string:
Output 4:
17
Question Bank Core Java
Input Format:
Output Format:
Constraints:
1 ≤ Length of S ≤ 1000
Test Cases:
Input 1:
Output 1:
Length: 3
Input 2:
Output 2:
Length: 3
Input 3:
Enter a string:
Output 3:
Input 4:
Output 4:
18
Question Bank Core Java
Invalid input. String contains non-alphabetic characters.
FUNCTIONS
Note:
Input Format:
Output Format:
Constraints:
1 ≤ N ≤ 10⁴
Test Cases:
Input 1:
13
Explanation:
The first occurrence of -5 is at index 1 and the last occurrence is
at index 3.
Input 2:
19
Question Bank Core Java
Enter the number of elements: 4
Enter array elements: 1 2 3 4
Enter the element to find: -1
Output 2:
-1 –1
Explanation:
The element -1 is not present in the array.
Input 3:
Output 3:
Input 4:
Output 4:
Input Format:
20
Question Bank Core Java
Output Format:
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
Test Cases:
Input 1:
Output 1:
Input 2:
Enter the number of test cases: 2
Output 2:
Explanation:
21
Question Bank Core Java
abcde → no duplicates → abcde
Input 3:
Enter the number of test cases: 1
Input 4:
Enter the number of test cases: 1
Questio
Question Detail Level
n No.
LOOPING STATEMENTS
1 Perfect Squares in a Range Easy
Problem Statement:
Given two integers a and b (1 ≤ a ≤ b ≤ 10⁵), find the number of perfect
squares between a and b (inclusive).
Input Format:
Two space-separated integers representing the range:
ab
Output Format:
Print the number of perfect squares between a and b.
If the input is invalid (e.g., a < 1, b < 1, or a > b), print:
Invalid Input
Constraints:
1 ≤ a ≤ b ≤ 10⁵
Test Cases:
22
Question Bank Core Java
Input 1:
Enter the Integer value: 3 8
Output 1:
1
Explanation: Perfect squares between 3 and 8 → 4
Input 2:
Enter the Integer value: 9 25
Output 2:
3
Explanation: Perfect squares between 9 and 25 → 9, 16, 25
Input 3:
Enter the Integer value: 30 20
Output 3:
Invalid Input
Input 4:
Enter the Integer value: 0 50
Output 4:
Invalid Input
All Prime Numbers Less Than or Equal to N Mediu
Problem Statement: m
Given a positive integer N, print all prime numbers less than or equal to N.
A prime number is a number greater than 1 that has no positive
divisors other than 1 and itself.
Use loops and conditional checks; do not use inbuilt prime functions.
Input Format
Input consists of a single integer N.
2 Output Format
Print all prime numbers ≤ N separated by space.
Print "Invalid Input" if the input does not satisfy the constraints.
Constraints
1 ≤ N ≤ 10⁴
Test Cases:
Input 1:
Enter the value of N: 10
23
Question Bank Core Java
Output 1:
Prime numbers: 2 3 5 7
Explanation:
The program checks numbers from 2 to 10.
Numbers with no divisors other than 1 and itself are prime.
Prime numbers ≤ 10 are: 2, 3, 5, 7.
Input 2:
Enter the value of N: 20
Output 2:
Prime numbers: 2 3 5 7 11 13 17 19
Explanation:
The program checks numbers from 2 to 20.
Prime numbers ≤ 20 are: 2, 3, 5, 7, 11, 13, 17, 19.
Input 3:
Enter the value of N: -5
Output 3:
Prime numbers: Invalid Input
Input 4:
Enter the value of N: 20000
Output 4:
Prime numbers: Invalid Input
Sum of Fibonacci Series up to Nth Term Mediu
Problem Statement: m
Given a number positive number N, find value of f0 + f1 + f2 + . + fN where fi
indicates ith Fibonacci number.
Input Format:
A single integer value representing the Nth term.
Output Format:
3
If the input is valid (positive integer within the constraint), print the sum
of the Fibonacci series from f0 to fN.
If the input is invalid (e.g., a negative number), print:
Invalid input. Enter a positive integer.
Constraints:
1 ≤ n ≤ 40
24
Question Bank Core Java
Test Cases:
Input 1:
Enter the value of N: 5
Output 1:
12
Explanation: Fibonacci: 0, 1, 1, 2, 3, 5 → sum = 0 + 1 + 1 + 2 + 3 + 5 =
12
Input 2:
Enter the value of N: 10
Output 2:
143
Explanation: Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 → sum = 143
Input 3:
Enter the value of N: 40
Output 3:
165580140
Input 4:
Enter the value of N: -5
Output 4:
Invalid input. Enter a positive integer.
PATTERN
Square Matrix with Repeated Row Numbers Easy
Problem Statement:
Write a program to print a square pattern of size N × N, where each row
contains the same number repeated N times. The number in each row
should start from 1 and increase by 1 in the next row.
For example, for N = 4, the pattern should be:
1111
4
2222
3333
4444
Input Format:
A single integer N, where N represents the size of the square matrix.
Output Format:
A square pattern of size N × N, where each row i contains the
25
Question Bank Core Java
number i repeated N times, separated by spaces.
Constraints:
1 <= N <= 50
Test Cases:
Input 1: Enter the value N: 3
Output 1:
111
222
333
Input 2:
Enter the value N: 5
Output 2:
11111
22222
33333
44444
55555
Input 3:
Enter the value N: 0
Output 3:
Error: N must be between 1 and 50
Input 4:
Enter the value N: 101
Output 4:
Error: N must be between 1 and 50
Decreasing Rows with Increasing Numbers Easy
Problem Statement:
Print a number pattern in which:
The first row starts from 1 and prints numbers up to n.
5 Each subsequent row starts from the next number and prints one
less value than the previous row.
The pattern continues until only the last number is printed.
Input Format:
A single integer n — the number of columns (or the maximum
26
Question Bank Core Java
number in the first row).
Output Format:
The pattern starts from 1 to n in the first row.
From the second row onward, the starting number increases by 1
and the number of elements decreases by 1 per row.
The last row contains just the number n.
Constraints:
1 ≤ n ≤ 50
Test Cases:
Input 1:
Enter the value N: 3
Output 1:
123
23
3
Input 1:
Enter the value N: 4
Output 1:
1234
234
34
4
27
Question Bank Core Java
Output Format:
Each row contains numbers starting from 1 up to the row number, aligned to
form a right-angled triangle shape.
Constraints:
1 ≤ n ≤ 50
Test Cases:
Input 1:
Enter the value N: 3
Output 1:
1
12
123
Input 2:
Enter the value N: 5
Output 3:
1
12
123
1234
12345
28
Question Bank Core Java
Constraints:
1 <= N <= 50
Test Cases:
Input 1: Enter the value N: 3
Output 1:
1
22
333
Input 2:
Enter the value N: 5
Output 2:
1
22
333
4444
55555
Input 3:
Enter the value N: 0
Output 3: Error: N must be between 1 and 50
Input 4:
Enter the value N: 51
Output 4: Error: N must be between 1 and 50
Pascal’s Triangle Generator Mediu
Problem Statement: Generate and print Pascal’s Triangle for a given m
integer N, where N specifies the number of rows in the triangle.
Pascal’s Triangle is a triangular array of numbers were:
The first and last numbers of each row are always 1.
Every interior number is the sum of the two numbers directly above
8
it from the previous row.
Input Format:
Number: N (an integer representing the number of rows in Pascal’s
Triangle)
Output Format:
A triangle of numbers with N rows where each number is computed
29
Question Bank Core Java
based on Pascal’s Triangle rules.
If the input is below the minimum limit (i.e., N < 1), print:
Invalid input (below minimum limit)
If the input exceeds the maximum limit (i.e., N > 25), print:
Invalid input (exceeds maximum limit)
Constraints:
1 ≤ N ≤ 25
Test Cases:
Input 1: Enter the value N: 5
Output 1:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Explanation:
Each number is the sum of the two numbers directly above it.
Row 1: 1
Row 2: 1 1
Row 3: 1 (1+1)=2 1
Row 4: 1 (1+2)=3 (2+1)=3 1
Row 5: 1 (1+3)=4 (3+3)=6 (3+1)=4 1
30
Question Bank Core Java
Q.
Question Detail Level
No.
CLASSES&OBJECTS
31
Question Bank Core Java
Output 2: Average = 13.33
Sample Input/output:
Author:
Name: J.K. Rowling
Email: jkrowling@[Link]
Gender: F
Book:
Title: Harry Potter and the Sorcerer's Stone
Price: 500
Quantity: 10
Author: J.K. Rowling
Author Details:
Name: J.K. Rowling
Email: jkrowling@[Link]
32
Question Bank Core Java
Gender: F
Book Details:
Title: Harry Potter and the Sorcerer's Stone
Price: 500
Quantity: 10
Author: J.K. Rowling
CONSTRUCTORS
Specifications:
Product ID: 0
Price: 0.0
Quantity: 0
Sample Input/output:
33
Question Bank Core Java
No input required (uses default values)
Price: 55000.0
Quantity: 5
Product ID: 0
Price: 0.0
Quantity: 0
Price: 55000.0
Quantity: 5
34
Question Bank Core Java
Potato, and Brinjal, and initializes the corresponding instance
variables.
3. Implement a method to display the vegetable quantities in the
following format:C <carrot_quantity> P <potato_quantity> B
<brinjal_quantity>
4. Demonstrate the usage of the class by creating objects using both
constructors and displaying the results.
Sample Input/output:
For parameterized constructor:
Carrot: 15
Potato: 25
Brinjal: 30
For default constructor:
No input required (all quantities default to 0)
35
Question Bank Core Java
demonstrate all the methods.
Sample Input/output:
Using default constructor
Deposit: 50
Using parameterized constructor (cashOnHand = 200)
Deposit: 100
36
Question Bank Core Java
If salary > 1000000 → Tax = 30% of salary
3. Test class (TestTax)
o Create two objects (tax1, tax2) with different constructors.
o Take salary input from the user for both objects.
o Display calculated tax for both.
Sample Input/output:
Enter salary for Tax1: 150000
Enter salary for Tax2: 600000
(PAN submitted true for Tax1, false for Tax2)
STATIC METHODS/VARIABLES
Write the Java class Library with the given requirements and demonstrate its
usage in the Main class by adding books to the library and retrieving the total
count.
37
Question Bank Core Java
Sample Input/output:
Add 3 books
Output:
Sample Input/output:
Principal = 1000
Rate = 5
Years = 3
38
Question Bank Core Java
Compound Interest Only: 157.625
ENCAPSULATION
Sample Input/output:
Employee ID: 101
Employee Name: John Doe
Employee Age: 30
Employee Salary: 50000
Raise Salary by: 10%
Employee Details:
ID: 101
39
Question Bank Core Java
Name: John Doe
Age: 30
Salary: 50000.0
Question
Question Detail Level
No.
40
Question Bank Core Java
INHERITANCE
Problem Statement:
Sample Input/Output:
Input:
Account Number: 101
Balance: 5000
Interest Rate: 5
Minimum Balance: 1000
Deposit: 2000
Withdraw: 1500
Term: 2 years
Output:
2000.0 deposited successfully.
1500.0 withdrawn successfully.
Interest Earned: 550.0
Problem Statement:
name
age
studentId
41
Question Bank Core Java
Sample Input/Output:
Input:
Person: Name=John Doe, Age=30
Student: Name=Alice Smith, Age=20, Student ID=123456
Undergraduate Student: Name=Bob Johnson, Age=21,
Student ID=654321, Major=Computer Science
Graduate Student: Name=Emily Brown, Age=25, Student
ID=987654, Research Topic=Machine Learning
Output:
Displaying information for a Person:
Name: John Doe
Age: 30
Problem Statement:
o customerName (String)
o accountNumber (String)
o balance (float)
42
Question Bank Core Java
Sample Output:
POLYMORPHISM
Problem Statement:
Sample Input/output:
Square: side=5
Rectangle: length=4, width=6
Circle: radius=7, shape="circle"
Perimeter of Square: 20.0
Perimeter of Rectangle: 20.0
Circumference of Circle: 43.982297150257104
Problem Statement:
43
Question Bank Core Java
"cylinder")
Sample Input/output:
Cube: side=3
Cuboid: length=4, width=5, height=6
Cylinder: radius=3, height=7, shape="cylinder"
Problem Statement:
Sample Input/Output:
Input:
Library books:
1. Title: Java Programming, Author: John Smith, ISBN:
1234567890
2. Title: Python Programming, Author: Alice Johnson, ISBN:
0987654321
3. Title: Data Structures, Author: Bob Brown, ISBN:
9876543210
4. Title: Algorithms, Author: Alice Johnson, ISBN:
5432109876
Output:
Books with title 'Java Programming':
Java Programming by John Smith
44
Question Bank Core Java
7 Employee Salary Calculation Medium
Problem Statement:
Sample Input/Output:
Input:
Employee ID: 101
Name: John Doe
Salary: 3000
Monthly Salary: 3000
Daily Salary: 120
Hourly Salary: 15, Hours per day: 8
Output:
Employee ID: 101, Name: John Doe, Salary: 3000.0
Yearly Salary (Monthly): 36000.0
Yearly Salary (Daily): 43800.0
Yearly Salary (Hourly): 31200.0
Question
Question Detail Level
No.
ABSTRACTION
Problem Statement:
Design an online payment system using abstraction in Java.
Create an abstract class Payment with a property amount (double)
45
Question Bank Core Java
and an abstract method processPayment().
Sample Input/Output:
Problem Statement:
Design a ticket booking system using abstraction in Java. Create
an abstract class Ticket with properties ticketNo and price, and an
abstract method bookTicket(). Derive two classes:
Sample Input/Output:
Problem Statement:
Design an electricity bill calculator using abstraction in Java.
Create an abstract class Bill with a property units (int) and an
abstract method calculateBill(). Derive two classes:
46
Question Bank Core Java
Sample Input/Output:
Problem Statement:
Design a student grade system using abstraction in Java. Create
an abstract class Student with properties name and an array
marks[], and an abstract method calculateGrade(). Derive two
classes:
Sample Input/Output:
John
60 70 80
Alice
Output:
Grade: Pass
INTERFACE
47
Question Bank Core Java
Problem Statement:
Design a music player system using interfaces in Java. Create an
interface MusicPlayer with two methods: play() and stop().
Implement two classes:
Sample Input/Output:
Problem Statement:
Define an interface Result with a method calculateResult(int
marks). Create a class ExamResult that implements this interface.
If the marks are less than 0 or greater than 100 ,indicate as
invalid input.
Sample Input/Output:
Problem Statement:
Define an interface CartOperations with two methods:
addItem(String item, double price) and checkout(). Implement a
class ShoppingCart that allows items to be added to the cart and
calculates the total at checkout. If the item name is null or the
price is less than or equal to zero display an error message.
Sample Input/Output:
48
Question Bank Core Java
Questio
Question Detail Level
n No.
1 Easy
Bookstore Inventory
Problem Statement: You are developing a Java
application for a bookstore that manages book inventory.
As part of the application, you need to handle various
exceptions that may occur during the inventory
management process. Design and implement exception
handling for the following scenarios:
1. InputMismatchException Handling: When the
user inputs data for book quantity, there is a
possibility of encountering an
InputMismatchException if the input provided is
49
Question Bank Core Java
not a valid integer. Implement exception handling to
catch and handle this exception gracefully. Display
an error message informing the user about the
incorrect input format and prompt them to enter the
quantity again.
2. NumberFormatException Handling: When
processing book prices, there is a risk of
encountering a NumberFormatException if the
price entered by the user cannot be parsed as a
valid decimal number. Implement exception
handling to catch and handle this exception. Display
an error message indicating that the price format is
invalid and prompt the user to enter the price again.
3. ArrayIndexOutOfBoundsException Handling:
During the inventory update process, there is a
possibility of encountering an
ArrayIndexOutOfBoundsException if the user
attempts to access an array element with an invalid
index. Implement exception handling to catch and
handle this exception. Display an error message
indicating that the specified index is out of bounds
and prompt the user to enter a valid index.
Sample Input/Output:
Quantity: 5
Price: 250.50
Index: 2
50
Question Bank Core Java
item being purchased (type integer) and a price per item
(double). Provide method constructor with four arguments.
Write a test application to create an instance and validate
the input obtained using Scanner object. Ensure that part
number is value greater than 0, part description is not null
string, quantity of the item and price per item is value
greater than 0.
Note : The InputMismatchException is thrown when
attempting to retrieve a value using the Scanner class that
doesn’t match the expected pattern or type.
Sample Input/output:
Part Number: 101
Part Description: Milk Pack
Quantity: 10
Price per Item: 55.50
Invoice Details:
Part Number: 101
Part Description: Milk Pack
Quantity: 10
Price Per Item: $55.5
3 Banking Application Medium
Problem Statement: You are developing a simple banking
application to manage customer accounts. One of the
requirements is to ensure that the withdrawal amount from
an account does not exceed the available balance.
Implement a custom exception called
InsufficientBalanceException to handle cases where the
withdrawal amount exceeds the available balance. Your
task is to modify the existing Account class to include
exception handling for withdrawals.
Your implementation should adhere to the following
specifications:
1. Define a custom exception class named
InsufficientBalanceException that extends the
Exception class. This class should have a
parameterized constructor that accepts a message
51
Question Bank Core Java
string.
2. Modify the Account class to include exception
handling for withdrawals:
When a withdrawal is attempted, check if the
withdrawal amount is greater than the
available balance.
If the withdrawal amount exceeds the
available balance, throw an
InsufficientBalanceException with an
appropriate error message.
If the withdrawal amount is valid, deduct the
amount from the available balance.
3. In the main method or a separate testing class,
create an instance of the Account class with an
initial balance. Test the withdrawal functionality by
attempting to withdraw an amount that exceeds the
available balance. Handle the
InsufficientBalanceException appropriately by
displaying an error message.
Sample Input/Output:
Initial Balance: 1000
Withdrawal: 500
Withdrawal successful.
Updated balance: 500.0
4 Employee Management Medium
Problem Statement: Define an employee class with
properties Employee code, name, date of birth and date of
appointment. The Employee code must be a positive
integer number.
Write a java program to read the above details and
validate the employee code. If the employee code is
not in the format specified , then raise an exception
called InvalidEmpNumberException.
Verify if the date of birth is before the data of
appointment. If it is not so then raise an exception
called InvalidDateOfJoinException. If it is correct,
52
Question Bank Core Java
then create the Employee object and display the
details of employees and the number of years of
experience.
Sample Input/Output:
EmpCode: 1001
Name: John Doe
DOB: 1990-05-15
DOJ: 2015-08-20
Sample Input/output:
Withdraw Amount: 20000
Transaction successful. Amount withdrawn: 20000.0
53
Question Bank Core Java
Questio
Question Detail Level
n No.
Problem Statement:
Write a Java program that creates two threads:
Ensure that the first thread completes its printing before the
second thread starts. Print the numbers along with the thread
name. Use synchronization if required.
Sample Output:
Thread-1: 1
Thread-1: 2
Thread-1: 3
Thread-1: 4
Thread-1: 5
Thread-1: 6
Thread-1: 7
Thread-1: 8
Thread-1: 9
Thread-1: 10
Thread-2: 11
Thread-2: 12
Thread-2: 13
Thread-2: 14
Thread-2: 15
Thread-2: 16
Thread-2: 17
Thread-2: 18
Thread-2: 19
Thread-2: 20
54
Question Bank Core Java
Problem Statement:
Write a Java program that implements a custom thread using the
Runnable interface.
2. Inside the run() method, print the thread name, then sleep
for 2 seconds, and print the thread name again.
Sample Output:
Thread-0 is running
Thread-1 is running
Thread-2 is running
Thread-0 finished after 2 seconds
Thread-1 finished after 2 seconds
Thread-2 finished after 2 seconds
Problem Statement:
Write a Java program that simulates a bank account supporting
deposit and withdrawal operations.
Sample Output:
Problem Statement:
Write a Java program to implement the Producer-Consumer
problem using threads.
55
Question Bank Core Java
1. Create a shared buffer (queue) for storing items (numbers).
Sample Output:
Produced: 1
Consumed: 1
Produced: 2
Produced: 3
Consumed: 2
Produced: 4
Consumed: 3
Produced: 5
Consumed: 4
Consumed: 5
Sample Output:
56
Question Bank Core Java
separate thread.
Sample Output:
Countdown: 10
Countdown: 9
Countdown: 8
Countdown: 7
Countdown: 6
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Countdown: 0
Time's up!
Sample Output:
Thread-1: 1
Thread-2: 2
Thread-3: 3
Thread-1: 4
Thread-2: 5
Thread-3: 6
Thread-1: 7
Thread-2: 8
Thread-3: 9
Thread-1: 10
57
Question Bank Core Java
8 Thread-Safe Counter using AtomicInteger Mediu
m
Problem Statement:
Write a Java program that implements a thread-safe counter using
AtomicInteger.
3. After all threads finish execution, print the final value of the
counter.
Sample Output:
Sample Output:
Writer-1 added 10
Writer-2 added 20
Reader-1 reads: [10]
Writer-1 added 30
Reader-2 reads: [10, 20, 30]
Writer-2 added 40
Reader-1 reads: [10, 20, 30, 40]
58
Question Bank Core Java
BankAccount class with a synchronized method deposit (int
amount) to safely update the balance. Two threads deposit
different amounts concurrently. Ensure the final balance is correct.
Sample Input/Output:
59
Question Bank Core Java
Questio
Question Detail Level
n No.
i. Adding Books:
60
Question Bank Core Java
array of book titles.
Sample Input/Output:
Books in library:
Lending '1984'...
Returning '1984'...
61
Question Bank Core Java
Books sorted by author:
Number of books: 2
Books as array:
1984
The Alchemist
i. Adding Students:
62
Question Bank Core Java
and print each student's name and age.
Sample Input/Output:
Student Names:
Alice
Bob
Charlie
Diana
All Students:
63
Question Bank Core Java
Name: Bob, Age: 22, Grade: 90
Sorting by Name:
Sorting by Age:
Removing Bob...
Total students: 2
Students as Array:
64
Question Bank Core Java
Alice
Diana
Diana (23)
Alice (21)
i. Adding Products:
65
Question Bank Core Java
unused capacity beyond the actual number
of elements stored.
Sample Input/Output:
All Products:
Removing 'Shirt'...
66
Question Bank Core Java
Sorting by Price:
Sorting by Name:
Total Products: 3
67
Question Bank Core Java
2. Title: "To Kill a Mockingbird", Author: "Harper Lee",
Publication Year: 1960
i. Pushing Books:
iii. Peeking:
v. Listing Books:
68
Question Bank Core Java
of books.
Sample Input/Output:
Books in stack:
- To Kill a Mockingbird
- 1984
Total books: 3
Iterating stack:
To Kill a Mockingbird
Stack cleared.
a. Adding Elements:
b. Removing Elements:
c. Accessing Elements:
69
Question Bank Core Java
b. Checking Queue Status:
e. Custom Comparator:
Sample Input/Output:
30
10
20
a. Adding Elements:
b. Removing Elements:
c. Accessing Elements:
70
Question Bank Core Java
b. Checking Queue Status:
e. Custom Comparator:
Sample Input/output:
7 LinkedHashMap Mediu
Problem Statement: m
Create a new Java class named LinkedHashMap. Import
the necessary Java Collection classes. Initialize a
LinkedHashMap named "linkedHashMap" to store key-
value pairs and perform the following operations:
71
Question Bank Core Java
iii. Add multiple null values.
b. Copying Mappings:
b. Retrieving Values:
e. Removing a Mapping:
Sample Input/output:
Value associated with key 3: Morning
72
Question Bank Core Java
Size of linkedHashMap: 7
Entries of linkedHashMap:
1:Hello
2:hi
3:Morning
5:day
null:NullValue1
6:null
7:null
a. Adding Elements:
b. Removing Elements:
73
Question Bank Core Java
g. Polling First and Last Elements:
Sample Input/Output:
Output:
Elements of charSet:
First element: a
Last element: g
9 TreeSet Mediu
Problem Statement: m
Create a new Java class named TreeSetCharacter. Import
the necessary Java Collection classes. Define a custom
Comparator for characters to reverse the order. Initialize
a TreeSet named "charSet" to store characters, sorted
according to the custom Comparator. Add the characters
{'z', 'a', 'b', 'c', 'x', 'y'} to the charset and perform the
following operations:
a. Adding Elements:
b. Removing Elements:
74
Question Bank Core Java
c. Checking if Set Contains Elements:
Sample Input/Output:
Elements of charSet:
First element: z
Last element: a
75
Question Bank Core Java
i. Adding Key-Value Pairs:
Sample Input/Output:
Count associated with 'date': 6
Contains 'banana': true
Contains count 4: true
Is wordCountMap empty: false
Number of mappings in wordCountMap: 4
Entries of wordCountMap:
Word: apple, Count: 5
Word: banana, Count: 8
Word: date, Count: 6
Word: grape, Count: 4
76
Question Bank Core Java
Words in wordCountMap: [apple, banana, date, grape]
Counts in wordCountMap: [5, 8, 6, 4]
// Extra Questions
ArrayDeque
Problem Statement:
a. Adding Elements:
i. Removing Elements:
d. Accessing Elements:
f. Dynamic Resizing:
77
Question Bank Core Java
elements.
Sample Input/Output:
Deque after dynamic resizing: [12, 24, 45, 67, 87, 43,
300, 400, 500, 600, 700, 800, 900]
Deque after removing elements: [12, 24, 45, 67, 87, 43,
300, 400, 500, 600]
12
24
45
67
87
43
300
400
500
600
78
Question Bank Core Java
(String) as values. Use the following key-value pairs:
79
Question Bank Core Java
Q.
Question Detail
No.
Sample Input:
Enter a string: Lambda
Sample Output:
Number of vowels: 2
2 Number Divisibility Checker
Problem Statement:
Create a custom functional interface DivisibilityChecker with a method boolean
isDivisible(int n, int divisor) that checks if a number is divisible by another number. Use a
lambda to implement it.
Functional Interface:
interface DivisibilityChecker { boolean isDivisible(int n, int divisor); }
Sample Input:
Enter number: 24
Enter divisor: 6
Sample Output:
24 is divisible by 6: true
3 Java Stream API Operations on E-Commerce Product Inventory
You are tasked with developing a Java application to manage and analyze product
information for an e-commerce system. The product data is stored as an
ArrayList<Product>, where You are working as a Java developer for a retail inventory
management system. Your task is to model product data using object-oriented principles
80
Question Bank Core Java
and perform various data processing operations using Java's Stream API.
The system maintains a list of products, each with details such as name, category, brand,
price, stock quantity, units sold, and creation date. You are required to:
1. Model the product using a Product class with appropriate fields and methods.
2. Store a list of products in memory using an ArrayList<Product>, where each
Product object contains the following fields:
3. id: Product ID (integer)
4. name: Product name (string)
5. category: Product category (e.g., Laptop, Mobile, Headphones)
6. brand: Manufacturer brand (string)
7. price: Product price (double)
8. quantity: Units available in stock (integer)
9. unitsSold: Total units sold so far (integer)
[Link]: Date the product was added (LocalDate)
81
Question Bank Core Java
Questio
Question Detail Level
n No.
82
Question Bank Core Java
Student Management System connected to a MySQL database.
Requirements:
1) Authentication
2) Dashboard
From the Dashboard, allow the user to perform the following via
Swing forms:
4) Database Handling
83