0% found this document useful (0 votes)
30 views15 pages

Exception Questions

The document outlines several programming tasks focused on exception handling in Java, including user input validation for various scenarios such as calculating the volume of a sphere, managing sales data, and validating product details. Each task specifies input and output formats, error handling requirements, and constraints, along with sample test cases. The document emphasizes the importance of using try-catch blocks to manage exceptions like InputMismatchException, IllegalArgumentException, and custom exceptions for specific validation cases.

Uploaded by

jetblackaqua957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views15 pages

Exception Questions

The document outlines several programming tasks focused on exception handling in Java, including user input validation for various scenarios such as calculating the volume of a sphere, managing sales data, and validating product details. Each task specifies input and output formats, error handling requirements, and constraints, along with sample test cases. The document emphasizes the importance of using try-catch blocks to manage exceptions like InputMismatchException, IllegalArgumentException, and custom exceptions for specific validation cases.

Uploaded by

jetblackaqua957
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXCEPTION HANDLING QUESTIONS

QUESTION 1

Problem Statement

Imagine you are tasked with creating a program that calculates the volume of a
sphere based on user input for the sphere's radius.

The program should handle exceptions for invalid input types. Use try-catch blocks
to handle potential exceptions: If the user enters a non-numeric value for the
radius, catch and handle the InputMismatchException.

Formula: Volume of sphere = 1.33 * 3.14 * radius3

Input format :
The input consists of a double value R, representing the radius of the sphere.

Output format :
The output prints a double value, representing the volume of the sphere, rounded
off to two decimal places.

If an exception occurs, print the error message "Error: Invalid input".

Refer to the sample output for formatting specifications.

Code constraints :
1.0 ≤ R ≤ 25.0

Sample test cases :


Input 1 :
2.5
Output 1 :
65.25
Input 2 :
D
Output 2 :
Error: Invalid input
Input 3 :
@
Output 3 :
Error: Invalid input

QUESTIONS 2

Problem Statement

James, a meticulous user, is developing a program that requires robust input


validation. He wants to ensure that the program gracefully handles different types
of user input.

Your task is to design and write a class UserInputValidation that prompts James to
input a value. The program should use exception handling to catch any
InputMismatchException that may occur if the entered value is not an integer.

If an exception occurs, the program should display an error message instructing


James to enter a valid input. Additionally, the program should always close the
scanner in the final block, even in the presence of exceptions, and display a
message confirming the closure. Implement this functionality within a
try-catch-finally block to ensure a comprehensive user experience.

Input format :
The input consists of an integer N, representing the integer.

Output format :
The output displays one of the following results:

If the entered value is an integer, the output displays "You entered: " followed by
the entered integer and "Scanner closed in the finally block" in a new line.
If the entered value is not an integer, the output displays "Error: Please enter a
valid input" and "Scanner closed in the finally block" in a new line.

Refer to the sample output for formatting specifications.

Code constraints :
-106 ≤ N ≤ 106

Sample test cases :


Input 1 :
-6
Output 1 :
You entered: -6
Scanner closed in the finally block
Input 2 :
Ten
Output 2 :
Error: Please enter a valid input
Scanner closed in the finally block

QUESTION 3

Problem Statement

Sam is developing a bakery sales management system that tracks sales data for a
given number of days. The system allows entering daily sales amounts, retrieving a
sales figure for a specific day, and updating the sales amount for that day. It
also needs to handle various exceptions:

Validate Number of Days: The number of days must be between 1 and 30. If it’s out
of range, it throws an IllegalArgumentException with: "Error: Number of days sales
must be between 1 and 30".
Handles Invalid Index: When retrieving or updating sales data, if the index is out
of bounds, it throws an ArrayIndexOutOfBoundsException with: "Error: Invalid index.
Please provide a valid day number".
Handles Non-Numeric Inputs: If any non-integer value is entered for sales amounts
or index, it catches a NumberFormatException with: "Error: Please enter valid
integer sales amounts".

Help Sam complete her task efficiently.

Input format :
The first line consists of an integer d, representing the number of days for which
sales data is provided.

The next d lines each contain an integer representing the sales amount for each
day.

The following line contains an integer i, representing the index of the day whose
sales amount should be displayed and updated.

The final line contains an integer s, representing the new sales amount to set for
the specified day.

Output format :
If the index is valid (i.e., between 0 and days - 1):

The first line of output should display the sales amount for the specified day.
The second line of output should display the updated sales amount for that day.
If the index is invalid (i.e., less than 0 or greater than or equal to days),
print: "Error: Invalid index. Please provide a valid day number"

If the number of days is less than 1 or greater than 30, print: "Error: Number of
days sales must be between 1 and 30"

If any input is not a valid integer, print: "Error: Please enter valid integer
sales amounts"

Refer to the sample output for formatting specifications.

Code constraints :
1 ≤ d ≤ 30

1 ≤ sales amount ≤ 10000

0 ≤ index < d

Input values must be valid integers

Sample test cases :


Input 1 :
5
234
183
329
404
525
3
924
Output 1 :
404
924
Input 2 :
4
104
645
835
645
6
385
Output 2 :
Error: Invalid index. Please provide a valid day number
Input 3 :
0
Output 3 :
Error: Number of days sales must be between 1 and 30
Input 4 :
3
274
two hundred
946
2
816
Output 4 :
Error: Please enter valid int

QUESTION 4

Problem Statement

Anna is creating a user ID management system that handles a list of user IDs for a
given number of entries. The system allows users to view and update a specific user
ID based on its index in the list. The program needs to manage various types of
errors:

Validates List Size: The number of user IDs must be between 1 and 500. If the size
is out of this range, it throws an IllegalArgumentException with: "Error: Number of
user IDs must be between 1 and 500".
Handles Invalid Index: When accessing or updating a user ID, if the index is out of
bounds, it throws an ArrayIndexOutOfBoundsException with: "Error: Invalid index.
Please select a valid index from the list".
Handles Non-Numeric Inputs: If any non-integer value is entered for user IDs or
index, it catches a NumberFormatException with: "Error: Please enter valid integer
values for user IDs".

Help Anna complete her task efficiently.

Input format :
The first line consists of an integer n, representing the number of user IDs.

The following n lines each contain an integer representing a user ID.

The next line contains an integer i, representing the position in the array for
which the user ID should be accessed and updated.

The final line contains an integer s, representing the new user ID to set at the
specified position.
Output format :
If the index is valid (i.e., between 0 and size - 1):

The first line of output should display the current user ID at the specified index.
The second line of output should display the updated user ID at that index.
If the index is invalid (i.e., less than 0 or greater than or equal to size),
print: "Error: Invalid index. Please select a valid index from the list"

If the number of user IDs is less than 1 or greater than 500, print: "Error: Number
of user IDs must be between 1 and 500"

If any input is not a valid integer, print: "Error: Please enter valid integer
values for user IDs"

Refer to the sample output for formatting specifications.

Code constraints :
1 ≤ n ≤ 500

1 ≤ userID ≤ 10000

0 ≤ i < n

1 ≤ s ≤ 10000

Sample test cases :


Input 1 :
5
394
945
723
925
274
1
946
Output 1 :
945
946
Input 2 :
4
647
932
745
558
8
596
Output 2 :
Error: Invalid index. Please select a valid index from the list
Input 3 :
0
Output 3 :
Error: Number of user IDs must be between 1 and 500
Input 4 :
3
835
twenty
375
1
946
Output 4 :
Error: Please enter valid inte

QUESTION 5

Problem Statement

Mithun is working on a program to manage a list of products in a retail store. He


has created a Product class with attributes such as product name, price, and
quantity in stock.

The program should take input for two products and perform basic validations on the
input data.

Ensure that the program uses try-catch blocks to handle exceptions and prints the
following error messages if validation fails:

if the length of the name > 25, throw an IllegalArgumentException - "Product name
should be 25 characters or less".
if the price ≤ 0, throw an IllegalArgumentException - "Price should be a positive
value".
if the quantity < 0, throw an IllegalArgumentException - "Quantity should be a
non-negative integer".

Mithun needs your help to implement this error handling.

Input format :
The first line of input consists of a string, representing the name of the first
product.

The second line consists of a double value, representing the price of the first
product.

The third line consists of an integer, representing the quantity in stock of the
first product.

The fourth line consists of a string, representing the name of the second product.

The fifth line consists of a double value, representing the price of the second
product.

The sixth line consists of an integer, representing the quantity in stock of the
second product.

Output format :
If the first product details are valid, print "Product 1 details are valid".

Otherwise, print "Product 1 details are not valid", along with the corresponding
error message.

If the second product details are valid, print "Product 2 details are valid".

Otherwise, print "Product 2 details are not valid", along with the corresponding
error message.

Refer to the sample output for formatting specifications.

Code constraints :
1 ≤ Length of the input string ≤ 50 characters

Sample test cases :


Input 1 :
Laptop
899.99
15
Desktop
1200.00
8
Output 1 :
Product 1 details are valid
Product 2 details are valid
Input 2 :
Wireless Mouse
-10.50
8
Mechanical Keyboard
69.99
-3
Output 2 :
Price should be a positive value
Product 1 details are not valid
Quantity should be a non-negative integer
Product 2 details are not valid
Input 3 :
Office Chair
89.99
30
Conference Table
500.00
-5
Output 3 :
Product 1 details are valid
Quantity should be a non-negative integer
Product 2 details are not valid
Input 4 :
Biological Research Microscope
25000.00
15
Cryogenic Storage
40.78
2
Output 4 :
Product name should be 25 characters or less
Product 1 details are not valid
Product 2 details are valid

QUESTION 6

Problem Statement

Anu is developing a program that represents a book and ensures the validity of its
attributes upon creation. The program should handle exceptions for invalid book
details and provide informative error messages.

Implement a Book class with the following attributes: title (String), author
(String), publicationYear (int).
Create a constructor for the Book class that initializes the book's attributes
(title, author, publication year).
Implement private validation methods (validateTitle, validateAuthor,
validatePublicationYear) to ensure that the title is not empty, the author is not
empty, and the publication year is a positive integer.
Throw IllegalArgumentException with appropriate error messages if validation fails.
Implement a method addBook in the Book class that prints "Book added successfully"
to the console.

Help Anu to design the program.

Input format :
The first line of input consists of a string, representing the title of the book.

The second line consists of a string, representing the author of the book.

The third line consists of an integer, representing the publication year of the
book.

Output format :
If the book is added, print "Book added successfully"

If the title is an empty string, print "Invalid title: Title should not be empty"

If the author is an empty string, print "Invalid author: Author should not be
empty"

If the publication year is not a positive integer, print "Invalid publication year:
Publication year should be a positive integer".

Refer to the sample output for formatting specifications.

Code constraints :
length of the input string ≤ 100 characters

Sample test cases :


Input 1 :
The Great Gatsby
F. Scott Fitzgerald
1925
Output 1 :
Book added successfully
Input 2 :

F. Scott Fitzgerald
1925
Output 2 :
Invalid title: Title should not be empty
Input 3 :
Harry Potter and the Sorcerer's Stone
J.K. Rowling
-1997
Output 3 :
Invalid publication year: Publication year should be a positive integer
Input 4 :
Ambedkar: A Life

2022
Output 4 :
Invalid author: Author shoul

QUESTION 7

Problem Statement

Arun is tasked with creating a program that allows a user to input the size of an
array and then input the elements of the array. Find and display the element at the
given index. The program should handle the possibility of an invalid array index
input and report any errors.

Use try-catch blocks to handle potential exceptions: If the user enters an index
that is out of bounds (greater or less than the array size), catch and handle the
ArrayIndexOutOfBoundsException.

Assist Arun in implementing this program.

Input format :
The first line of input consists of an integer N, representing the number of
elements in the array.

The second line consists of N space-separated integers, representing the array


elements.

The third line consists of an integer M, representing the index(0-indexed).

Output format :
The output prints "Element at index {index}: ", followed by the element at the
specified index.

If an exception occurs, print the error message "Error: Array index out of bounds".
Refer to the sample output for formatting specifications.

Code constraints :
The given test cases fall under the following constraints:

1 ≤ N ≤ 10

1 ≤ array elements ≤ 100

Sample test cases :


Input 1 :
5
12 23 45 56 89
4
Output 1 :
Element at index 4: 89
Input 2 :
3
45 85 67
0
Output 2 :
Element at index 0: 45
Input 3 :
6
41 56 94 73 61 54
9
Output 3 :
Error: Array index out of bounds
Input 4 :
6
34 78 52 98 61 79
-5
Output 4 :
Error: Array index out of bounds

[USER DEFIENED EXCEPTIONS]


QUESTION 8

An ISBN (International Standard Book Number) consists of 10 digits


d1d2d3d4d5d6d7d8d9d10. The last digit d10 is a checksum, which is calculated from
the other nine digits using the following formula: (d1 * 1 + d2 * 2 + d3 * 3 + d4 *
4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9)% 11.

If the checksum is 10, the last digit is denoted X according to the ISBN
convention.
Create a class ‘NumberManipulation’ which contains an integer array (size 9) as its
instance variable and two methods extractDigits( ) and findLastDigit( ). The
extractDigits( ) method must take an integer number as argument and extract the
individual digits of the number and store them in the 9-element integer array. The
method findLastDigit( ) should find out the 10th digit using the above formula and
return it.

Define a main class and read the 9-digit number as a String. Throw a user defined
exception "InvalidInputException" if the length of the input String is not exactly
9 digits. If valid, convert the input into integer and pass it to the method
extractDigits( ) and also invoke findLastDigit( ) method which would return the
last digit. If the last digit is 10, append ‘X’ to the original string else append
the 11 modulo of calculated digit’ and display the ISBN number.

Input format :
ISBN number as a string

Output format :
New ISBN Number or Exception

Sample test cases :


Input 1 :
8967346787655
Output 1 :
InvalidInputException: ISBN must be exactly 9 digits
Input 2 :
100000001
Output 2 :
ISBN : 100000001X
Input 3 :
103000001
Output 3 :
ISBN : 1030000018

QUESTION 9

Problem Statement

Queency wants to write a program to validate the email address and display suitable
exceptions if there is a mistake.
Create 3 custom exception classes as below

DotException
AtTheRateException
DomainException

A typical email address should have a " . " or "@" character, and the domain name
should be valid. Valid domain names for practice are 'in', 'com', 'net', or 'biz'.

Display an Invalid Dot usage, Invalid @ usage, or Invalid Domain message based on
the email ID.

Get the email address from the user, validate the email by checking the
above-mentioned criteria, and print the validity status of the input email address.

Input format :
The input consists of a string representing the email to be validated.

Output format :
The output prints,

If the email is valid, print "Valid email address".

If there is a dot exception, print "DotException: Invalid Dot usage", followed by


"Invalid email address".

If there is a rate exception, print "AtTheRateException: Invalid @ usage", followed


by "Invalid email address".

If there is a domain exception, print "DomainException: Invalid Domain", followed


by "Invalid email address".

Refer to the sample output for the formatting specifications.

Code constraints :
The given test cases fall under the following constraints:

The input consists of a string representing the email to be validated which is a


maximum of 20 characters only.
Sample test cases :
Input 1 :
sample@[Link]
Output 1 :
Valid email address
Input 2 :
sample@[Link].
Output 2 :
DotException: Invalid Dot usage
Invalid email address
Input 3 :
sample@g@[Link]
Output 3 :
AtTheRateException: Invalid @ usage
Invalid email address
Input 4 :
sample@[Link]
Output 4 :
DomainException: Invalid Domain
Invalid email address

QUESTION 10

You might also like