0
0
Javaprogramming~20 mins

If–else statement in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If–else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if–else
What is the output of this Java code snippet?
Java
int x = 10;
int y = 20;
if (x > y) {
    System.out.println("X is greater");
} else if (x == y) {
    System.out.println("X equals Y");
} else {
    System.out.println("Y is greater");
}
AY is greater
BX equals Y
CX is greater
DNo output
Attempts:
2 left
💡 Hint
Compare the values of x and y carefully.
Predict Output
intermediate
2:00remaining
If–else with boolean condition
What will this Java program print?
Java
boolean isRaining = false;
if (isRaining) {
    System.out.println("Take an umbrella");
} else {
    System.out.println("No umbrella needed");
}
ANo umbrella needed
BTake an umbrella
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Check the value of the boolean variable.
Predict Output
advanced
2:00remaining
If–else with multiple conditions
What is the output of this Java code?
Java
int score = 75;
if (score >= 90) {
    System.out.println("Grade A");
} else if (score >= 80) {
    System.out.println("Grade B");
} else if (score >= 70) {
    System.out.println("Grade C");
} else {
    System.out.println("Grade F");
}
AGrade F
BGrade A
CGrade B
DGrade C
Attempts:
2 left
💡 Hint
Check which condition the score satisfies first.
Predict Output
advanced
2:00remaining
If–else with assignment inside condition
What will this Java code print?
Java
int a = 5;
if ((a = 10) > 5) {
    System.out.println(a);
} else {
    System.out.println("Less or equal to 5");
}
A5
B10
CCompilation error
DLess or equal to 5
Attempts:
2 left
💡 Hint
Notice the assignment inside the if condition.
Predict Output
expert
3:00remaining
If–else with complex boolean expressions
What is the output of this Java code?
Java
int x = 3;
int y = 4;
if (x > 2 && y < 5) {
    if (x + y == 7) {
        System.out.println("Sum is seven");
    } else {
        System.out.println("Sum is not seven");
    }
} else {
    System.out.println("Condition not met");
}
ACondition not met
BSum is not seven
CSum is seven
DCompilation error
Attempts:
2 left
💡 Hint
Evaluate both conditions carefully and then the sum.