Bird
Raised Fist0
Javaprogramming~20 mins

Nested if statements in Java - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does a nested if statement mean in Java?
easy
A. An if statement without any condition
B. An if statement with multiple conditions joined by ||
C. An if statement inside another if statement
D. An if statement that runs only once

Solution

  1. Step 1: Understand the meaning of nested if

    A nested if means placing one if statement inside another to check conditions step-by-step.
  2. Step 2: Compare options

    Only An if statement inside another if statement describes an if inside another if. Others describe different concepts.
  3. Final Answer:

    An if statement inside another if statement -> Option C
  4. Quick Check:

    Nested if = if inside if [OK]
Hint: Look for 'if inside if' meaning nested [OK]
Common Mistakes:
  • Confusing nested if with multiple conditions in one if
  • Thinking nested if runs only once
  • Ignoring the inner if statement
2. Which of the following is the correct syntax for a nested if statement in Java?
easy
A. if (x > 0) if y > 0 { System.out.println("Both positive"); }
B. if (x > 0) { if (y > 0) { System.out.println("Both positive"); } }
C. if x > 0 { if y > 0 { System.out.println("Both positive"); } }
D. if (x > 0) { if (y > 0) System.out.println("Both positive") }

Solution

  1. Step 1: Check Java syntax rules for if

    Java requires parentheses around conditions and braces for blocks.
  2. Step 2: Validate each option

    if (x > 0) { if (y > 0) { System.out.println("Both positive"); } } uses correct parentheses and braces. if (x > 0) if y > 0 { System.out.println("Both positive"); } misses parentheses around y > 0. if x > 0 { if y > 0 { System.out.println("Both positive"); } } misses parentheses and braces. if (x > 0) { if (y > 0) System.out.println("Both positive") } misses semicolon after println.
  3. Final Answer:

    if (x > 0) { if (y > 0) { System.out.println("Both positive"); } } -> Option B
  4. Quick Check:

    Correct syntax = if (x > 0) { if (y > 0) { System.out.println("Both positive"); } } [OK]
Hint: Remember: conditions need parentheses, blocks need braces [OK]
Common Mistakes:
  • Omitting parentheses around conditions
  • Forgetting braces for nested blocks
  • Missing semicolons after statements
3. What will be the output of this code?
int a = 5;
int b = 10;
if (a > 0) {
if (b < 5) {
System.out.println("Inside inner if");
} else {
System.out.println("Inside else of inner if");
}
} else {
System.out.println("Inside outer else");
}
medium
A. Inside else of inner if
B. Inside outer else
C. Inside inner if
D. No output

Solution

  1. Step 1: Evaluate outer if condition

    a = 5, so a > 0 is true, enter outer if block.
  2. Step 2: Evaluate inner if condition

    b = 10, b < 5 is false, so else block of inner if runs.
  3. Final Answer:

    Inside else of inner if -> Option A
  4. Quick Check:

    Outer if true, inner if false -> inner else [OK]
Hint: Check outer if first, then inner if conditions [OK]
Common Mistakes:
  • Ignoring the else block of inner if
  • Confusing outer else with inner else
  • Assuming no output if inner if false
4. Find the error in this nested if code:
int x = 10;
if (x > 5)
if (x < 20)
System.out.println("x is between 5 and 20");
else
System.out.println("x is 5 or less");
medium
A. Missing braces causing else to pair with wrong if
B. Incorrect condition syntax
C. Missing semicolon after println
D. Variable x not declared

Solution

  1. Step 1: Analyze if-else pairing without braces

    Without braces, else pairs with nearest if, here inner if.
  2. Step 2: Understand the logic error

    Else prints "x is 5 or less" only if x >= 20, which is wrong logically.
  3. Final Answer:

    Missing braces causing else to pair with wrong if -> Option A
  4. Quick Check:

    Braces fix else pairing [OK]
Hint: Use braces to avoid else pairing mistakes [OK]
Common Mistakes:
  • Assuming else pairs with outer if
  • Ignoring braces importance
  • Thinking syntax error instead of logic error
5. You want to write a nested if to check if a number is positive and even. Which code correctly does this?
hard
A. if (num > 0) { if (num % 2 = 0) { System.out.println("Positive even"); } }
B. if (num > 0 && num % 2 == 0) System.out.println("Positive even");
C. if (num > 0) if (num / 2 == 0) System.out.println("Positive even");
D. if (num > 0) { if (num % 2 == 0) { System.out.println("Positive even"); } }

Solution

  1. Step 1: Check condition for positive number

    First if checks if num > 0, correct for positive check.
  2. Step 2: Check condition for even number inside first if

    Inner if uses num % 2 == 0, correct for even check.
  3. Final Answer:

    if (num > 0) { if (num % 2 == 0) { System.out.println("Positive even"); } } -> Option D
  4. Quick Check:

    Nested if with correct conditions = if (num > 0) { if (num % 2 == 0) { System.out.println("Positive even"); } } [OK]
Hint: Use % 2 == 0 to check even inside positive check [OK]
Common Mistakes:
  • Using single = instead of == in condition
  • Using division instead of modulo for even check
  • Combining conditions without nesting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard