Java Conditional Statement Questions
1. Check Vowel or Consonant
public class Main {
public static void main(String[] args) {
char ch = 'E';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
System.out.println("Vowel");
} else {
System.out.println("Consonant");
}
}
}
What is the output?
2. Check Age Group
public class Main {
public static void main(String[] args) {
int age = 20;
if (age < 13) {
System.out.println("Child");
} else if (age >= 13 && age < 20) {
System.out.println("Teenager");
} else if (age >= 20 && age < 60) {
System.out.println("Adult");
} else {
System.out.println("Senior Citizen");
}
}
}
What is the output?
3. Ternary Operator for Positive or Negative
public class Main {
public static void main(String[] args) {
int num = 0;
String result = (num > 0) ? "Positive" : (num < 0) ? "Negative" : "Zero";
System.out.println(result);
}
}
What is the output?
4. Find Smallest Number
public class Main {
public static void main(String[] args) {
int x = 12, y = 7, z = 19;
if (x < y && x < z) {
System.out.println("Smallest: " + x);
} else if (y < x && y < z) {
System.out.println("Smallest: " + y);
} else {
System.out.println("Smallest: " + z);
}
}
}
What is the output?
5. Leap Year Without `if-else`
public class Main {
public static void main(String[] args) {
int year = 1900;
String result = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? "Leap Year" :
"Not a Leap Year";
System.out.println(result);
}
}
What is the output?