Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java If...Else Practice

Decision making with if, else if, else, nested if and the ternary operator.

1
Check Positive Negative
Easy
Read an integer N and print "Positive", "Negative" or "Zero".
A single integer N.
"Positive", "Negative" or "Zero".
Sample Input 1
5
Sample Output 1
Positive
Sample Input 2
-3
Sample Output 2
Negative
Sample Input 3
0
Sample Output 3
Zero
Sample Input 4
1
Sample Output 4
Positive
Sample Input 5
-1
Sample Output 5
Negative
-10^6
Use if/else if/else.
2
Largest of Two
Easy
Read two integers A and B and print the larger one.
A single line with two integers A and B.
A single integer: max(A, B).
Sample Input 1
5 9
Sample Output 1
9
Sample Input 2
-3 -7
Sample Output 2
-3
Sample Input 3
7 7
Sample Output 3
7
Sample Input 4
100 1
Sample Output 4
100
Sample Input 5
0 0
Sample Output 5
0
-10^9
if (a > b) print a else print b.
3
Leap Year
Easy
Read a year Y and print "Leap Year" or "Not a Leap Year". A year is a leap year if divisible by 400, or divisible by 4 but not by 100.
A single integer Y.
"Leap Year" or "Not a Leap Year".
Sample Input 1
2024
Sample Output 1
Leap Year
Sample Input 2
2023
Sample Output 2
Not a Leap Year
Sample Input 3
2000
Sample Output 3
Leap Year
Sample Input 4
1900
Sample Output 4
Not a Leap Year
Sample Input 5
2100
Sample Output 5
Not a Leap Year
1
(Y % 400 == 0) || (Y % 4 == 0 && Y % 100 != 0).
4
Grade the Marks
Easy
Read marks M (0-100) and print the grade: A (>=90), B (>=80), C (>=70), D (>=60), E (>=50), F (
A single integer M.
A single letter: A, B, C, D, E or F.
Sample Input 1
85
Sample Output 1
B
Sample Input 2
45
Sample Output 2
F
Sample Input 3
100
Sample Output 3
A
Sample Input 4
90
Sample Output 4
A
Sample Input 5
50
Sample Output 5
E
0
Chain of if/else if.
5
Vowel or Consonant
Easy
Read a single lowercase letter and print "Vowel" if it is a, e, i, o or u, otherwise "Consonant".
A single lowercase letter.
"Vowel" or "Consonant".
Sample Input 1
a
Sample Output 1
Vowel
Sample Input 2
b
Sample Output 2
Consonant
Sample Input 3
e
Sample Output 3
Vowel
Sample Input 4
z
Sample Output 4
Consonant
Sample Input 5
u
Sample Output 5
Vowel
a-z
Check membership in the set {a,e,i,o,u}.
6
Odd or Even Number
Easy
Read an integer N and print "Odd" or "Even".
A single integer N.
"Odd" or "Even".
Sample Input 1
4
Sample Output 1
Even
Sample Input 2
7
Sample Output 2
Odd
Sample Input 3
1
Sample Output 3
Odd
Sample Input 4
100
Sample Output 4
Even
Sample Input 5
999
Sample Output 5
Odd
1
if (n % 2 == 0) even else odd.
7
Age Group
Easy
Read an age and print "Child" (0-12), "Teen" (13-19), "Adult" (20-59) or "Senior" (60+).
A single integer age.
"Child", "Teen", "Adult" or "Senior".
Sample Input 1
10
Sample Output 1
Child
Sample Input 2
16
Sample Output 2
Teen
Sample Input 3
0
Sample Output 3
Child
Sample Input 4
25
Sample Output 4
Adult
Sample Input 5
70
Sample Output 5
Senior
0
Use if/else if chains on ranges.
1
Largest of Three
Medium
Read three integers A, B, C and print the largest.
A single line with three integers A, B, C.
A single integer: the largest.
Sample Input 1
5 9 3
Sample Output 1
9
Sample Input 2
7 7 7
Sample Output 2
7
Sample Input 3
1 2 3
Sample Output 3
3
Sample Input 4
100 50 75
Sample Output 4
100
Sample Input 5
-5 -2 -9
Sample Output 5
-2
-10^9
Nested if/else or compare each pair.
2
Ternary Max
Medium
Read two integers A and B and print the larger one using the ternary operator (? :).
A single line with two integers A and B.
A single integer: max(A, B).
Sample Input 1
5 9
Sample Output 1
9
Sample Input 2
-3 -7
Sample Output 2
-3
Sample Input 3
7 7
Sample Output 3
7
Sample Input 4
100 1
Sample Output 4
100
Sample Input 5
-100 -1
Sample Output 5
-1
-10^9
int max = (a > b) ? a : b;
3
Nested If - Triangle Type
Medium
Read three side lengths A, B, C and print "Equilateral", "Isosceles" or "Scalene". Input is guaranteed to form a valid triangle.
A single line with three integers A, B, C.
"Equilateral", "Isosceles" or "Scalene".
Sample Input 1
5 5 5
Sample Output 1
Equilateral
Sample Input 2
5 5 3
Sample Output 2
Isosceles
Sample Input 3
3 4 5
Sample Output 3
Scalene
Sample Input 4
7 7 10
Sample Output 4
Isosceles
Sample Input 5
10 20 15
Sample Output 5
Scalene
1
If A==B && B==C → Equilateral; else if any two equal → Isosceles; else Scalene.
4
Divisible by Both
Medium
Read an integer N and print "Both" if it is divisible by both 3 and 5, "Three" if only by 3, "Five" if only by 5, and "None" if by neither.
A single integer N.
"Both", "Three", "Five" or "None".
Sample Input 1
15
Sample Output 1
Both
Sample Input 2
9
Sample Output 2
Three
Sample Input 3
5
Sample Output 3
Five
Sample Input 4
1
Sample Output 4
None
Sample Input 5
30
Sample Output 5
Both
1
Check N % 15 == 0 first, then N % 3, then N % 5.
5
Compare Two Numbers
Medium
Read two integers A and B and print "A > B", "A < B" or "A == B".
A single line with two integers A and B.
"A > B", "A < B" or "A == B".
Sample Input 1
5 3
Sample Output 1
A > B
Sample Input 2
3 5
Sample Output 2
A < B
Sample Input 3
4 4
Sample Output 3
A == B
Sample Input 4
-1 -2
Sample Output 4
A > B
Sample Input 5
100 99
Sample Output 5
A > B
-10^9
Compare with if/else if/else.
6
Discount Calculator
Medium
Read the price P. If P >= 1000 apply a 20% discount, if P >= 500 apply 10%, else no discount. Print the final price (whole number).
A single integer P.
A single integer: the discounted price.
Sample Input 1
2000
Sample Output 1
1600
Sample Input 2
600
Sample Output 2
540
Sample Input 3
400
Sample Output 3
400
Sample Input 4
500
Sample Output 4
450
Sample Input 5
1000
Sample Output 5
800
0
if (p >= 1000) p = p * 80 / 100; else if (p >= 500) p = p * 90 / 100;
1
Valid Triangle Check
Hard
Read three side lengths A, B, C and print "Valid" if they can form a triangle, otherwise "Invalid". A triangle is valid if the sum of any two sides is greater than the third.
A single line with three integers A, B, C.
"Valid" or "Invalid".
Sample Input 1
3 4 5
Sample Output 1
Valid
Sample Input 2
1 2 3
Sample Output 2
Invalid
Sample Input 3
1 1 1
Sample Output 3
Valid
Sample Input 4
10 1 2
Sample Output 4
Invalid
Sample Input 5
5 5 9
Sample Output 5
Valid
1
Valid iff A+B > C && A+C > B && B+C > A.
2
Roots of Quadratic
Hard
Read coefficients a, b, c of a quadratic equation and print the nature of roots: "Real and Distinct", "Real and Equal" or "Imaginary".
A single line with three integers a, b, c.
One of: "Real and Distinct", "Real and Equal", "Imaginary".
Sample Input 1
1 -5 6
Sample Output 1
Real and Distinct
Sample Input 2
1 2 1
Sample Output 2
Real and Equal
Sample Input 3
1 1 1
Sample Output 3
Imaginary
Sample Input 4
1 -2 1
Sample Output 4
Real and Equal
Sample Input 5
2 4 1
Sample Output 5
Real and Distinct
1
disc = b*b - 4*a*c. If disc > 0 → distinct; == 0 → equal; < 0 → imaginary.
3
Date Validity
Hard
Read day, month and year and print "Valid" or "Invalid". Handle leap years for February (29 days in a leap year, 28 otherwise). Months with 31 days: 1,3,5,7,8,10,12.
A single line with three integers: day, month, year.
"Valid" or "Invalid".
Sample Input 1
29 2 2024
Sample Output 1
Valid
Sample Input 2
29 2 2023
Sample Output 2
Invalid
Sample Input 3
31 4 2024
Sample Output 3
Invalid
Sample Input 4
31 1 2020
Sample Output 4
Valid
Sample Input 5
30 2 2024
Sample Output 5
Invalid
1
Check days-in-month, with a leap-year case for February.
4
Electricity Bill
Hard
Read units consumed U and print the bill amount. Slabs: first 100 units @ 1.5, next 100 units @ 2.5, beyond @ 3.5. The result is always a whole number — print as an integer.
A single integer U.
A single integer: the bill amount.
Sample Input 1
50
Sample Output 1
75
Sample Input 2
150
Sample Output 2
275
Sample Input 3
0
Sample Output 3
0
Sample Input 4
100
Sample Output 4
150
Sample Input 5
200
Sample Output 5
400
0
If U
5
Character Type
Hard
Read a single character and print "Uppercase" if it is A-Z, "Lowercase" if a-z, "Digit" if 0-9, otherwise "Special".
A single character.
"Uppercase", "Lowercase", "Digit" or "Special".
Sample Input 1
A
Sample Output 1
Uppercase
Sample Input 2
a
Sample Output 2
Lowercase
Sample Input 3
5
Sample Output 3
Digit
Sample Input 4
!
Sample Output 4
Special
Sample Input 5

                      
Sample Output 5
Special
Any single character.
Compare against ASCII ranges: 'A'
6
Second Largest of Three
Hard
Read three distinct integers A, B, C and print the second largest.
A single line with three integers A, B, C.
A single integer: the second largest.
Sample Input 1
3 7 5
Sample Output 1
5
Sample Input 2
10 1 5
Sample Output 2
5
Sample Input 3
1 2 3
Sample Output 3
2
Sample Input 4
100 50 75
Sample Output 4
75
Sample Input 5
-5 -2 -9
Sample Output 5
-5
-10^9
Compare all pairs, or sort and take index 1.
7
Number to Words (1-9)
Hard
Read a digit N (1-9) and print its English word ("One", "Two", ... "Nine").
A single integer N.
The English word for N.
Sample Input 1
1
Sample Output 1
One
Sample Input 2
5
Sample Output 2
Five
Sample Input 3
9
Sample Output 3
Nine
Sample Input 4
3
Sample Output 4
Three
Sample Input 5
7
Sample Output 5
Seven
1
if/else if chain, or a switch.
Competitive MCQs — Java If...Else
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 36
Q1
What is the output of this code?
java
1
2
3
4
5
6
int x = 10;
if (x > 5) {
    System.out.println("A");
} else {
    System.out.println("B");
}
Correct!
Wrong — correct answer is .
x > 5 is true, so only "A" prints.
Q2
What is the output of this code?
int x = 10;
if (x > 5) {
System.out.println("A");
} else {
System.out.println("B");
}
Correct!
Wrong — correct answer is .
x > 5 is true, so only "A" prints.
Q3
Which keyword begins an alternate branch in Java?
Correct!
Wrong — correct answer is .
Java uses else if and else.
Q4
What is the output of this code?
java
1
2
3
4
5
6
int n = 7;
if (n % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}
Correct!
Wrong — correct answer is .
7 % 2 is 1, not 0, so the else prints "Odd".
Q5
What is the output of this code?
int n = 7;
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Correct!
Wrong — correct answer is .
7 % 2 is 1, not 0, so the else prints "Odd".
Q6
What is the output of this code?
java
1
2
3
int a = 5, b = 9;
int m = (a > b) ? a : b;
System.out.println(m);
Correct!
Wrong — correct answer is .
The ternary picks b (9) because a > b is false.
Q7
What is the output of this code?
int a = 5, b = 9;
int m = (a > b) ? a : b;
System.out.println(m);
Correct!
Wrong — correct answer is .
The ternary picks b (9) because a > b is false.
Q8
Which condition checks if year is a leap year (divisible by 4 but not 100, OR divisible by 400)?
Correct!
Wrong — correct answer is .
The full leap-year rule requires both parts.
Q9
What is the output of this code?
java
1
2
3
4
5
6
7
8
int score = 85;
if (score >= 90) {
    System.out.print("A");
} else if (score >= 80) {
    System.out.print("B");
} else {
    System.out.print("C");
}
Correct!
Wrong — correct answer is .
85 >= 90 is false; 85 >= 80 is true → "B".
Q10
What is the output of this code?
int score = 85;
if (score >= 90) {
System.out.print("A");
} else if (score >= 80) {
System.out.print("B");
} else {
System.out.print("C");
}
Correct!
Wrong — correct answer is .
85 >= 90 is false; 85 >= 80 is true → "B".
Q11
What does the switch statement compare against?
Correct!
Wrong — correct answer is .
switch evaluates an expression and jumps to the matching case.
Q12
Which of these is NOT valid in an if condition?
Correct!
Wrong — correct answer is .
Java does not allow declarations inside the if condition parentheses.
Q13
What is the output of this code?
java
1
2
3
4
boolean flag = false;
if (!flag) {
    System.out.println("Yes");
}
Correct!
Wrong — correct answer is .
!false is true, so "Yes" prints.
Q14
What is the output of this code?
boolean flag = false;
if (!flag) {
System.out.println("Yes");
}
Correct!
Wrong — correct answer is .
!false is true, so "Yes" prints.
Q15
What is the output of this code?
java
1
2
3
4
int x = 0;
if (x = 1) {
    System.out.println("Set");
}
Correct!
Wrong — correct answer is .
x = 1 is an assignment, not a boolean — this does not compile.
Q16
What is the output of this code?
int x = 0;
if (x = 1) {
System.out.println("Set");
}
Correct!
Wrong — correct answer is .
x = 1 is an assignment, not a boolean — this does not compile.
Q17
What is the output of this code?
int age = 18;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Correct!
Wrong — correct answer is .
18 >= 18 is true, so "Adult" prints.
Q18
What is the output of this code?
int x = 3;
if (x % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Correct!
Wrong — correct answer is .
3 % 2 is 1, not 0, so the else branch prints "Odd".
Q19
Which condition is true when x is between 1 and 10 inclusive?
Correct!
Wrong — correct answer is .
&& requires both bounds, making x lie within the range.
Q20
What is the output of this code?
int n = 0;
if (n > 0) {
System.out.println("pos");
} else if (n < 0) {
System.out.println("neg");
} else {
System.out.println("zero");
}
Correct!
Wrong — correct answer is .
n is neither positive nor negative, so the final else prints "zero".
Q21
Which keyword is required to use an else-if chain?
Correct!
Wrong — correct answer is .
Java chains alternate branches with else if.
Q22
What is the output of this code?
boolean flag = true;
if (!flag) {
System.out.println("No");
} else {
System.out.println("Yes");
}
Correct!
Wrong — correct answer is .
!flag is false, so the else prints "Yes".
Q23
What does the ternary operator do in Java?
Correct!
Wrong — correct answer is .
condition ? valueIfTrue : valueIfFalse is an expression form of if-else.
Q24
What is the output of this code?
int x = 15;
if (x > 10) {
System.out.print("A");
if (x > 20) {
System.out.print("B");
}
}
Correct!
Wrong — correct answer is .
15 > 10 prints A; the nested check 15 > 20 is false, so only A prints.
Q25
Which is a valid if condition?
Correct!
Wrong — correct answer is .
Java requires parentheses and a boolean expression in the condition.
Q26
What is the output of this code?
int x = 7;
if (x > 3 && x < 10) {
System.out.println("range");
}
Correct!
Wrong — correct answer is .
7 is greater than 3 and less than 10, so the body prints "range".
Q27
What is the output of this code?
int marks = 45;
String grade = marks >= 40 ? "Pass" : "Fail";
System.out.println(grade);
Correct!
Wrong — correct answer is .
45 >= 40 is true, so grade is "Pass".
Q28
Which of these is NOT valid?
Correct!
Wrong — correct answer is .
Assignment is not a boolean condition, so if (x = 5) fails to compile.
Q29
What is the output of this code?
int x = 100;
if (x < 50) {
System.out.println("small");
} else if (x < 150) {
System.out.println("medium");
} else {
System.out.println("large");
}
Correct!
Wrong — correct answer is .
x < 50 is false; x < 150 is true, so "medium" prints.
Q30
Which operator combines two conditions that must both hold?
Correct!
Wrong — correct answer is .
&& (logical AND) is true only when both sides are true.
Q31
Which operator is true when at least one condition holds?
Correct!
Wrong — correct answer is .
|| (logical OR) is true when either side is true.
Q32
What is the output of this code?
int a = 5, b = 9;
if (a > b) System.out.println(a); else System.out.println(b);
Correct!
Wrong — correct answer is .
5 > 9 is false, so the else prints b = 9.
Q33
Which of the following correctly tests equality of a String?
Correct!
Wrong — correct answer is .
String content is compared with equals(), not ==.
Q34
What is the output of this code?
int x = 5;
if (x == 5)
System.out.println("five");
System.out.println("done");
Correct!
Wrong — correct answer is .
Only the single statement after the if is conditional; the next line always runs.
Q35
What is the output of this code?
char c = 'a';
if (c >= 'a' && c <= 'z') {
System.out.println("lower");
} else {
System.out.println("other");
}
Correct!
Wrong — correct answer is .
Characters compare by their code values, and 'a' is within the lowercase range.
Q36
What does the if-else-if ladder do?
Correct!
Wrong — correct answer is .
The ladder stops at the first true condition and skips the rest.