lem-Based Questions on ArrayIndexOutOfBoundsException & ArithmeticExcep
Problem 1: Handling Invalid Task Index
A to-do list app allows users to update tasks by index. Handle invalid indexes gracefully.
Sample Input:
Sample Input:
Tasks: ['Task1', 'Task2', 'Task3']
Input Index: 5
Sample Output:
Sample Output:
Error: Invalid index! Please try again.
Problem 2: Marks Management System
A system stores marks for 5 subjects. Handle invalid array indexes while viewing/updating marks.
Sample Input:
Sample Input:
Marks: [85, 90, 78, 88, 76]
Input Index: 7
Sample Output:
Sample Output:
Error: Index out of bounds! Valid range is 0 to 4.
Problem 3: E-Commerce Order Processing
Orders are stored in an array. Handle attempts to access invalid order indices.
Sample Input:
Sample Input:
Orders: ['Order1', 'Order2']
Input Index: 3
Sample Output:
Sample Output:
Error: No order exists at this index.
Problem 4: Weather App Temperature Data
Daily temperatures are stored in an array. Handle access to non-existent days.
Sample Input:
Sample Input:
Temperatures: [30, 31, 29, 28]
Input Day: 5
Sample Output:
Sample Output:
Error: Day does not exist!
Problem 5: Card Deck Simulation
A deck of cards is represented as an array. Handle invalid card index inputs.
Sample Input:
Sample Input:
Deck: ['Ace', 'King', 'Queen']
Input Index: 10
Sample Output:
Sample Output:
Error: Invalid card index!
Problem 6: Simple Calculator
A calculator divides two numbers. Handle division by zero.
Sample Input:
Sample Input:
Enter numerator: 10
Enter denominator: 0
Sample Output:
Sample Output:
Error: Division by zero is not allowed.
Problem 7: Tax Calculation
Taxable income is divided by the number of dependents. Handle division by zero.
Sample Input:
Sample Input:
Income: 50000
Dependents: 0
Sample Output:
Sample Output:
Error: Dependents cannot be zero.
Problem 8: Average Calculation
Calculate the average of numbers in an array. Handle empty arrays.
Sample Input:
Sample Input:
Numbers: []
Sample Output:
Sample Output:
Error: Cannot divide by zero.
Problem 9: Bank Interest Calculation
Calculate interest by dividing balance by months. Handle months being zero.
Sample Input:
Sample Input:
Balance: 10000
Months: 0
Sample Output:
Sample Output:
Error: Months cannot be zero.
Problem 10: Sports Winning Percentage
Winning percentage is calculated by dividing wins by matches played. Handle matches being zero.
Sample Input:
Sample Input:
Wins: 5
Matches: 0
Sample Output:
Sample Output:
Error: Matches cannot be zero.
Problem 11: Combined Array and Arithmetic Exception
Access invalid array indices or divide by zero based on user input.
Sample Input:
Sample Input:
Array: [1, 2, 3]
Index: 5
Denominator: 0
Sample Output:
Sample Output:
Error: Invalid index or division by zero.
Problem 12: Payroll Management
Average salary is calculated. Handle no employees or invalid array indices.
Sample Input:
Sample Input:
Salaries: []
Sample Output:
Sample Output:
Error: No employees or invalid index.
Problem 13: Matrix Operations
Perform division of matrix elements and handle invalid indices or division by zero.
Sample Input:
Sample Input:
Matrix: [[1, 2], [3, 4]]
Index: (2,2)
Sample Output:
Sample Output:
Error: Invalid index or division by zero.
Problem 14: Quiz Application
Access scores in an array or divide scores. Handle invalid indices or zero divisions.
Sample Input:
Sample Input:
Scores: [10, 20, 30]
Index: 5
Sample Output:
Sample Output:
Error: Invalid index or division by zero.
Problem 15: Marks Percentage Calculation
Handle invalid array indices or zero divisions in marks percentage calculation.
Sample Input:
Sample Input:
Marks: [90, 80]
Index: 5
Divisor: 0
Sample Output:
Sample Output:
Error: Invalid index or division by zero.