Challenge - 5 Problems
Nested If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested if with multiple conditions
What is the output of the following Java code?
Java
public class Test { public static void main(String[] args) { int x = 10; int y = 20; if (x > 5) { if (y < 15) { System.out.println("A"); } else { System.out.println("B"); } } else { System.out.println("C"); } } }
Attempts:
2 left
💡 Hint
Check the conditions inside each if block carefully.
✗ Incorrect
The outer if checks if x > 5, which is true (10 > 5). Then the inner if checks if y < 15, which is false (20 < 15 is false). So it goes to the else block and prints "B".
❓ Predict Output
intermediate2:00remaining
Nested if with else-if ladder output
What will be printed when this Java code runs?
Java
public class Check { public static void main(String[] args) { int score = 75; if (score >= 90) { System.out.println("Excellent"); } else { if (score >= 70) { System.out.println("Good"); } else { System.out.println("Needs Improvement"); } } } }
Attempts:
2 left
💡 Hint
Look at the score and compare with each condition.
✗ Incorrect
Score is 75, which is not >= 90, so the first if fails. Then the nested if checks if score >= 70, which is true, so it prints "Good".
🔧 Debug
advanced2:00remaining
Identify the error in nested if syntax
What error will this Java code produce when compiled?
Java
public class ErrorTest { public static void main(String[] args) { int a = 5; if (a > 0) if (a < 10) System.out.println("Inside"); else System.out.println("Outside"); } }
Attempts:
2 left
💡 Hint
Check how else is matched with if in nested statements without braces.
✗ Incorrect
The else is matched with the nearest if, but indentation is misleading. Without braces, the else pairs with the inner if. The code compiles but the indentation suggests a logic error. However, no syntax error occurs. But since the question asks for error, the correct answer is no error. To fix this, braces are needed. But since only one option can be correct, the best fit is no error and prints "Inside".
❓ Predict Output
advanced2:00remaining
Output of nested if with boolean flags
What is the output of this Java program?
Java
public class Flags { public static void main(String[] args) { boolean isRaining = true; boolean haveUmbrella = false; if (isRaining) { if (haveUmbrella) { System.out.println("Go outside"); } else { System.out.println("Stay inside"); } } else { System.out.println("Enjoy the sun"); } } }
Attempts:
2 left
💡 Hint
Check the values of both boolean variables and the nested conditions.
✗ Incorrect
isRaining is true, so the outer if is true. Then haveUmbrella is false, so the inner else runs and prints "Stay inside".
❓ Predict Output
expert2:00remaining
Value of variable after nested if execution
What is the value of variable result after running this Java code?
Java
public class NestedIfTest { public static void main(String[] args) { int x = 8; int y = 12; String result = ""; if (x > 5) { if (y > 10) { result = "A"; } else if (y > 5) { result = "B"; } else { result = "C"; } } else { result = "D"; } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check each condition carefully and see which block runs.
✗ Incorrect
x is 8, which is > 5, so outer if runs. y is 12, which is > 10, so first inner if runs and sets result to "A".