Java If-Else Practice Questions
Java If-Else Practice Questions
The Java program evaluates a student's grade by taking an integer percentage input and using nested if-else statements to assign a grade. The grades are categorized into the following percentage ranges: 90 and above is an A, 80–89 is a B, 70–79 is a C, 60–69 is a D, and below 60 is an F. This systematic approach ensures that each student's performance is accurately reflected in their grade based on defined academic standards .
To create a voter eligibility checker in Java using if-else statements, a simple program can be structured to ask the user for their age and determine eligibility based on legal voting age. Specifically, the program will check if the user's age is 18 or older, in which case it prints that the person is eligible to vote. If the age is less than 18, it outputs that the person is not eligible to vote. This structure ensures that the program adheres to standard legal voting age requirements .
A Java program determines if a number is even or odd by using the modulus operator (%). The program reads an integer input from the user and checks if it is divisible by 2 without a remainder. If 'num % 2 == 0' evaluates to true, the number is even, and the program prints that it is even. Otherwise, it prints that the number is odd. This approach uses simple division to classify the number efficiently .
The Java program provides clothing advice based on the input temperature as follows: if the temperature is greater than 30°C, it advises wearing light clothes due to the heat. For temperatures between 15°C and 30°C, it suggests dressing comfortably in nice weather. If the temperature is below 15°C, it advises wearing a jacket due to the cold. This decision-making is executed through a series of if-else statements that categorize temperature ranges and pair them with corresponding clothing recommendations .
The Java ATM simulation handles withdrawal requests by verifying if the requested amount is less than or equal to the current balance. If the condition 'amount <= balance' is true, the program subtracts the amount from the balance and confirms a successful withdrawal with the new balance. If the amount exceeds the balance, the program outputs a message indicating insufficient funds, effectively preventing the withdrawal. This logic maintains account integrity by ensuring that overdrafts cannot occur .