Challenge - 5 Problems
If–else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"); }
Attempts:
2 left
💡 Hint
Compare the values of x and y carefully.
✗ Incorrect
Since x (10) is less than y (20), the else block runs printing 'Y is greater'.
❓ Predict Output
intermediate2: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"); }
Attempts:
2 left
💡 Hint
Check the value of the boolean variable.
✗ Incorrect
The variable is false, so the else block runs printing 'No umbrella needed'.
❓ Predict Output
advanced2: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"); }
Attempts:
2 left
💡 Hint
Check which condition the score satisfies first.
✗ Incorrect
Score 75 is less than 80 but greater or equal to 70, so 'Grade C' is printed.
❓ Predict Output
advanced2: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"); }
Attempts:
2 left
💡 Hint
Notice the assignment inside the if condition.
✗ Incorrect
The assignment sets a to 10, which is greater than 5, so it prints 10.
❓ Predict Output
expert3: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"); }
Attempts:
2 left
💡 Hint
Evaluate both conditions carefully and then the sum.
✗ Incorrect
x=3 > 2 and y=4 < 5 are true, sum is 7, so prints 'Sum is seven'.