0
0
Javaprogramming~20 mins

Nested if statements in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested If Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
        }
    }
}
AA
BNo output
CC
DB
Attempts:
2 left
💡 Hint
Check the conditions inside each if block carefully.
Predict Output
intermediate
2: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");
            }
        }
    }
}
AExcellent
BGood
CNeeds Improvement
DNo output
Attempts:
2 left
💡 Hint
Look at the score and compare with each condition.
🔧 Debug
advanced
2: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");
    }
}
ANo error, prints "Inside"
BRuntime error
CNo error, prints "Outside"
DSyntax error: else without if
Attempts:
2 left
💡 Hint
Check how else is matched with if in nested statements without braces.
Predict Output
advanced
2: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");
        }
    }
}
AGo outside
BEnjoy the sun
CStay inside
DNo output
Attempts:
2 left
💡 Hint
Check the values of both boolean variables and the nested conditions.
Predict Output
expert
2: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);
    }
}
AA
BB
CD
DC
Attempts:
2 left
💡 Hint
Check each condition carefully and see which block runs.