PBLJ Lab MST Questions
E-1: String Transformation
Problem Statement
In many text processing applications, it is important to analyze a given string and categorize its
contents. In this problem, you're required to classify and count the number of vowels,
consonants, digits, and special characters in a given input string. This basic operation is
commonly used in text parsing, data sanitization, and validation systems.
Given a string, count and print the total number of vowels, consonants, digits, and special
characters in the string. You need to consider both uppercase and lowercase letters.
Input Format
A single line string (can contain letters, digits, spaces, and special characters)
Output Format
Four lines of output:
1. Number of special characters
2. Number of consonants
3. Number of consonants
4. Number of special characters
Constraints
String length ≤ 1000
Only printable ASCII characters will be present
Spaces should be considered as special characters
Sample Test Case 1
Input:
Hello World! 123
Output:
Vowels: 3
Consonants: 7
Digits: 3
Special Characters: 3
Explanation:
Vowels: e, o, o (3)
Consonants: H, I, I, W, r, I, d (7)
Digits: 1, 2, 3 (3)4
Special Characters: Space (2), ! (1), Total = 3
E-2: Perform Matrix Operations: Addition and Subtraction
Problem Statement
Matrix operations such as addition and subtraction are foundational in mathematics and
computer science. These operations are widely used in image processing, linear algebra,
simulations, and more. In this task, the user provides two matrices, and your program must
compute their sum and difference.
Write a Java program that accepts two matrices from the user (same dimensions), performs
matrix addition and subtraction, and displays the resulting matrices.
Input Format
First line: two integers m and n representing the number of rows and columns
Next m lines: n space-separated integers representing Matrix A
Next m lines: n space-separated integers representing Matrix B
Output Format
Matrix after Addition
Matrix after Subtraction
Each matrix should be printed in matrix format.
Constraints
15m, n≤10
Matrix elements are integers in the range [-100, 100]
Sample Test Case 1
Input:
23
[1 2 3
456]
[7 8 9
111]
Output:
[8 10 12
5 6 7]
[-6 -6 -6
3 4 5]
Explanation:
Matrix A:
123
456
Matrix B:
789
111
Addition:
1 + 7 = 8 2 + 8 = 10 3+9 = 12
4 + 1 = b 5 + 1 = 6 6+1 = 7
Subtraction:
1 - 7 = - 6 2 - 8 = - 6 3-9-6 4 - 1 = 3 5 - 1 = 4 6-1 = 5
E-3: Implement a Basic Banking System
Problem Statement
A banking application needs a basic system to manage customer accounts. The system should
allow users to:
Create a bank account by providing the account holder's name, account number, and initial
balance.
Deposit a specified amount into the account.
Withdraw a specified amount from the account (only if sufficient balance is available).
Display the final balance after performing the above operations.
The application should ensure that the account balance remains valid throughout the operations
and adheres to defined input constraints.
Input Format
The program will read five inputs in the following order:
Account holder name A string (can include spaces)
Account number - An integer
Initial balance - A double
Deposit amount - A double
Withdrawal amount A double
Note: All inputs are read from standard input (Scanner)
Output Format
The output should be:
If all transactions are successful (i.e, the withdrawal is allowed), print the final balance as a
double.
If the withdrawal amount is greater than the available balance, print: Insufficient balance
If any input value is invalid (e.g., negative deposit/withdrawal or non-positive account number),
print: Invalid Input values.
Constraints
accountNumber > O
initialBalance >= 0
depositAmount >= 0
withdrawAmount >= 0
withdrawAmount <= currentBalance (Only if withdrawal should happen)
Sample Test Case 1
Input:
Bob
123486
282
2000
2500
Output:
Insufficient balance
Sample Test Case 2
Input:
John Doe
123456
5000
1500
2000
Output:
4500.0
Explanation:
Initial balance: 5000
After deposit: 6500
After withdrawal: 6500-2000 = 4500
E-4: Product Class with Constructor & Display
Problem Statement
You are developing a system for a store where products are stored with numeric codes for name
and ID. The goal is to create a Product class that can initialize and display product details using
numeric input only
Create a Java class named Product with the following numeric attributes:
id (int) unique identifier for the product
nameCode (int) numeric representation of the product name
price (double) product price
Implement a constructor to initialize these attributes, and a method displayProductDetails() to
print them.
Input Format
Product ID (integer)
Product Name Code (integer)
Product Price (double)
Output Format
Print the product details:
Product ID
Product Name Code
Product Price
Constraints
ID >0
Price ≥ 0
Name Code is a numeric representation (simulated name lookup)
Sample Test Case 1
Input:
101
2001
55000
Output:
Product ID: 101
Product Name Code: 2001
Product Price: ₹55000.0
Explanation:
Product ID: 101- This is the unique ID assigned to the product.
Product Name Code: 2001- This numeric code acts as a placeholder for the product's name in
the system (like a code for "Laptop" or "TV").
Product Price: ₹55000.0 - This shows the product's price; printed as a double, so it includes a
decimal point.
E-5: Library Management System using Inheritance
Problem Statement.
You are developing a basic Library Management System to keep track of books. Each book has
an ID, title (represented using numeric code), and availability status. The system allows you to
display book
details and check books in and out. Use inheritance to extend a base Libraryltem class.
Create a superclass Libraryltem with the following attributes:
itemid (int)
titleCode (int) (a number that represents the book title)
available (boolean)
Then create a subclass Book that extends Libraryltem and includes methods to:
Display book details
Check out a book (mark unavailable)
Return a book (mark available)
Input Format
An integer representing the Book ID (itemid)
An integer representing the Title Code (titleCode)
An integer representing Availability (1 available, O not available)
One or more integers representing operations (each on a new line or space-separated):
1- Check out
2- Return
3 Display Details
Note: The input terminates when no more integers are available (EOF).
Output Format
After each operation, output as follows:
For Checkout:
If book is available: "Book Checked Out Successfully."
If already checked out: "Already checked out."
For Return:
If book is not available: "Book Returned Successfully."
If already available: "Already available."
For Display:
. Book ID:
. Title Code:
. Available: <true/false>
For any invalid operation: "Invalid operation."
Constraints
itemld > O
titleCode > 0
availability is either 1 (true) or O (false)
Operation codes must be integers among {1, 2, 3}
All inputs are integers only
Maximum of 100 operations can be assumed for normal usage
Sample Test Case 1
Input:
1001
501
1
1
3
Output:
Book Checked Out Successfully.
Book ID: 1001
Title Code: 501
Available: false
Explanation:
The book with ID 1001 and title code 501 was initially available.
After performing a checkout, it became unavailable.
Then, the display operation showed the updated details with availability marked as false.
E-6: Student Information System using Abstraction and
Inheritance
Problem Statement
Educational institutions often manage different types of individuals students, teachers, etc. They
may share some basic information (like name and age), but also have role-specific attributes.
You are asked to create a system that uses abstraction to generalize common behavior and
inheritance to differentiate student and teacher details.
Create an abstract class Person with the following attributes:
name (String)
age (int)
And the method:
displayDetails() abstract method
Then create two derived classes:
Student (with additional attribute: rollNumber - int)
Teacher (with additional attribute: subjectCode - int)
Override displayDetails() in both classes and display all relevant information.
Input Format
Person type (1 for Student, 2 for Teacher)
Name (String, assume as a single word or hardcoded for simplicity)
Age (int)
Roll Number (int) for Student OR Subject Code (int) for Teacher
Output Format
Print details of Student or Teacher using the overridden method.
Constraints
Age must be a positive number
Roll Number and Subject Code must be integers
Name should be treated as a single-word string
Sample Test Case 1
Input:
1
Alice
20
1001
Output:
Student Details:
Name: Alice
Age: 20
Roll Number: 1001
Explanation:
A student named Alice, aged 20, with roll number 1001 is created.
The overridden method displays these details as student information.
E-7: Square Root Calculation with Exception Handling
Problem Statement
You're building a console-based utility where the user enters a number, and the program returns
its square root. However, invalid inputs like strings or negative numbers may crash the program.
Use try-catch to validate input and handle exceptions gracefully.
Write a Java program to read a number from the user and print its square root.
If the user enters a non-numeric value, catch the exception and show a message.
If the number is negative, handle it by displaying an appropriate message (square root of
negative not defined for real numbers).
Input Format
A single value entered via keyboard (expected: a non-negative number)
Output Format
Square root value (if valid)
Or a meaningful error message if invalid input is entered
Constraints
Input can be a float, integer, or invalid string
Only real, non-negative numbers are valid inputs
Must handle all exceptions without crashing the program
Sample Test Case 1
Input:
26
Output:
Square root of 25.0 is 5.0
Explanation:
The input 25 is a valid non-negative number. Its square root is calculated and printed as 5.0.
Hence, the correct output is displayed
Sample Test Case 2
Input:
abc
Output:
Invalid input. Please enter a numeric value.
Explanation:
The input abc is not a number, so like trying to add "abc" and 15, the program cannot compute a
square root and shows:
Invalid input. Please enter a numeric value.
Sample Test Case 3
Input:
9
Output:
Cannot calculate square root of a negative number.
Explanation:
The input -9 is numeric but negative, and since square root of a negative is not defined in real
numbers, the program shows:
Cannot calculate square root of a negative number.
E-8: Simulate an ATM Withdrawal System
Problem Statement
You are tasked with creating a Java program that simulates a basic ATM withdrawal process.
The program must ensure:
1. Valid withdrawal amount must be positive and a multiple of 100.
2. Sufficient balance withdrawal cannot exceed the account balance.
3. Exception handling invalid inputs should not crash the program.
Create a Java program that simulates an ATM. It should:
Allow the user to enter the current balance and the withdrawal amount
Validate if the withdrawal is possible (sufficient balance, valid input)
Display appropriate messages using exception handling for:
Invalid inputs
Insufficient funds
Non-multiples of 100
Input Format
Current balance (double)
Withdrawal amount (double)
Output Format
Updated balance OR an error message
Constraints
Withdrawal amount must be a multiple of 100
Balance should not go negative
Input must be numeric
Note:
1. Withdrawal amount must be positive - Negative or zero amounts are invalid.
2. Only amounts like 100, 500, 1200 are allowed. if not print 'Withdrawal amount must be in
multiples of 100.
3. Numeric input validation if the value is non-numeric value then print 'Invalid input. Please
enter a numeric value.'
Sample Test Case 1
Input:
Balance: 5000
Withdraw: 1200
Output:
Withdrawal successful. Remaining balance: ₹3800.0
Explanation:
Balance before withdrawal: ₹5000
Withdrawal amount: 1200 (valid, multiple of 100, sufficient funds)
After the withdrawal. ₹5000 - ₹1200 = ₹3800.
So, the output confirms: Withdrawal successful. Remaining balance: ₹3800.0.
Sample Test Case 2
Input:
Balance: 2000
withdraw: 2500
Output:
Insufficient balance. Withdrawal failed.
Explanation:
Withdrawal amount is greater than the available balance. Withdrawal failed.
E-9: University Enrollment with Exception Handling
Problem Statement
In a university, students enroll in courses. The enrollment system must validate user input,
ensuring students meet criteria such as valid age, course ID, and number of credits. Exceptions
should be used to
handle invalid data gracefully, such as invalid age or incorrect course ID format
Design a simple Java program for a University Enrollment System that takes a student's name,
age, course ID, and number of credits. The system should validate:
Age must be between 17 and 30
Course ID must be exactly 5 digits.
Credits must be between 1 and 30
Use try-catch to handle exceptions and display appropriate error messages.
Input Format
Name (String)
Age (int)
Course ID (int)
Credits (int)
Output Format
Confirmation of successful enrollment OR an error message
Constraints
Age: 17≤ age ≤ 30
Course ID: 5-digit number (10000 to 99999)
Credits: 1≤credits ≤ 30
Note:
Age: Must be between 17 and 30 else: "Invalid age. Must be between 17 and 30."
Course ID: Must be 5 digits (10000-99999)-else: "Invalid Course ID. Must be a 5-digit number."
Credits: Must be between 1 and 30-else: "Invalid number of credits. Must be between 1 and 30."
Numeric Input: Age, Course ID, and Credits must be numeric else: "Invalid input. Please enter
numeric values for age, course ID, and credits."
Sample Test Case 1
Input:
Alice
20
12345
25
Output:
Enrollment Successful!
Student Name: Alice
Age: 20
Course ID: 12345
Credits: 25
Explanation:
All inputs (age, course ID, credits) are valid and within the given constraints.
Hence, enrollment is successful, and student details are displayed.
Sample Test Case 2
Input:
Nina
16
12345
12
Output:
Invalid age. Must be between 17 and 38.