Challenge - 5 Problems
Else–if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Else–if Ladder with Multiple Conditions
What is the output of the following Java code?
Java
public class Test { public static void main(String[] args) { 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 75 satisfies first in the else-if ladder.
✗ Incorrect
The score 75 is not >= 90 or >= 80, but it is >= 70, so the else-if block for Grade C runs.
❓ Predict Output
intermediate2:00remaining
Output When No Conditions Match in Else–if Ladder
What will be printed when the input value is 45 in this Java program?
Java
public class Test { public static void main(String[] args) { int value = 45; if (value > 100) { System.out.println("High"); } else if (value > 50) { System.out.println("Medium"); } else if (value > 60) { System.out.println("Above Average"); } else { System.out.println("Low"); } } }
Attempts:
2 left
💡 Hint
Check all conditions carefully and see which one matches 45.
✗ Incorrect
45 is not greater than 100, 50, or 60, so the else block runs printing "Low".
🔧 Debug
advanced2:00remaining
Identify the Error in Else–if Ladder Syntax
What error does this Java code produce?
Java
public class Test { public static void main(String[] args) { int num = 10; if (num > 0) { System.out.println("Positive"); } else if (num == 0) { System.out.println("Zero"); } else { System.out.println("Negative"); } } }
Attempts:
2 left
💡 Hint
Check the syntax of the else-if statement carefully.
✗ Incorrect
In Java, else-if conditions must be enclosed in parentheses. Missing parentheses cause a syntax error.
🚀 Application
advanced2:00remaining
Determine the Final Value After Else–if Ladder Execution
What is the value of variable 'result' after running this Java code?
Java
public class Test { public static void main(String[] args) { int x = 15; String result; if (x < 10) { result = "Less"; } else if (x < 20) { result = "Between"; } else if (x < 30) { result = "More"; } else { result = "Unknown"; } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check which condition the value 15 satisfies first.
✗ Incorrect
15 is not less than 10 but is less than 20, so 'Between' is assigned and printed.
🧠 Conceptual
expert2:00remaining
Understanding Else–if Ladder Execution Flow
Consider the following Java code snippet. Which statement best describes the execution flow when x = 25?
Java
int x = 25; if (x > 30) { System.out.println("Above 30"); } else if (x > 20) { System.out.println("Above 20"); } else if (x > 10) { System.out.println("Above 10"); } else { System.out.println("10 or below"); }
Attempts:
2 left
💡 Hint
Remember that else-if ladder stops checking after the first true condition.
✗ Incorrect
Since x=25 is not >30 but is >20, it prints "Above 20" and skips the rest.