0% found this document useful (0 votes)
164 views3 pages

Java If-Else Practice Questions

The document provides Java programming practice questions focusing on real-life scenarios using if-else statements. It includes examples for checking voting eligibility, determining even or odd numbers, giving clothing advice based on temperature, evaluating student grades, and simulating an ATM withdrawal. Each example includes the necessary logic and code implementation.

Uploaded by

rohanbisaria58
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)
164 views3 pages

Java If-Else Practice Questions

The document provides Java programming practice questions focusing on real-life scenarios using if-else statements. It includes examples for checking voting eligibility, determining even or odd numbers, giving clothing advice based on temperature, evaluating student grades, and simulating an ATM withdrawal. Each example includes the necessary logic and code implementation.

Uploaded by

rohanbisaria58
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

Java If-Else Practice Questions (Real-Life Examples)

1. Eligibility to Vote
Write a Java program that checks if a person is eligible to vote. The user inputs their
age.

Logic:
import [Link];

public class VotingEligibility {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your age: ");
int age = [Link]();

if (age >= 18) {


[Link]("You are eligible to vote.");
} else {
[Link]("You are not eligible to vote.");
}
}
}

2. Even or Odd Number


Write a Java program that takes a number and determines if it's even or odd.

Logic:
import [Link];

public class EvenOdd {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();

if (num % 2 == 0) {
[Link](num + " is even.");
} else {
[Link](num + " is odd.");
}
}
}

3. Temperature Check
Write a program to give clothing advice based on the temperature:
- >30°C -> "It's hot, wear light clothes"
- 15-30°C -> "Nice weather, dress comfortably"
- <15°C -> "It's cold, wear a jacket"
Java If-Else Practice Questions (Real-Life Examples)

Logic:
import [Link];

public class WeatherAdvice {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the temperature in °C: ");
int temp = [Link]();

if (temp > 30) {


[Link]("It's hot, wear light clothes.");
} else if (temp >= 15) {
[Link]("Nice weather, dress comfortably.");
} else {
[Link]("It's cold, wear a jacket.");
}
}
}

4. Student Grade Evaluator


A student enters a percentage. Print the grade:
- >= 90 -> A
- 80-89 -> B
- 70-79 -> C
- 60-69 -> D
- <60 -> F

Logic:
import [Link];

public class GradeEvaluator {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your percentage: ");
int percent = [Link]();

if (percent >= 90) {


[Link]("Grade: A");
} else if (percent >= 80) {
[Link]("Grade: B");
} else if (percent >= 70) {
[Link]("Grade: C");
} else if (percent >= 60) {
[Link]("Grade: D");
} else {
[Link]("Grade: F");
}
}
}
Java If-Else Practice Questions (Real-Life Examples)

5. ATM Withdrawal
Simulate an ATM machine that checks if the user has enough balance to withdraw money.

Logic:
import [Link];

public class ATM {


public static void main(String[] args) {
double balance = 5000.0;
Scanner sc = new Scanner([Link]);

[Link]("Enter amount to withdraw: ");


double amount = [Link]();

if (amount <= balance) {


balance -= amount;
[Link]("Withdrawal successful. Remaining balance: $" + balance);
} else {
[Link]("Insufficient balance.");
}
}
}

Common questions

Powered by AI

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 .

You might also like