0
0
Javaprogramming~20 mins

Break statement in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Break Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of break in nested loops
What is the output of this Java code snippet?
Java
public class Test {
    public static void main(String[] args) {
        int count = 0;
        outer: for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i * j > 3) {
                    break outer;
                }
                count++;
            }
        }
        System.out.println(count);
    }
}
A5
B3
C4
D6
Attempts:
2 left
💡 Hint
Look at how the labeled break exits the outer loop immediately.
🧠 Conceptual
intermediate
2:00remaining
Effect of break in switch-case
What will be printed when the following Java code runs?
Java
public class Test {
    public static void main(String[] args) {
        int x = 2;
        switch (x) {
            case 1:
                System.out.println("One");
            case 2:
                System.out.println("Two");
            case 3:
                System.out.println("Three");
                break;
            default:
                System.out.println("Default");
        }
    }
}
A
Two
Three
BTwo
C
Two
Three
Default
D
One
Two
Three
Attempts:
2 left
💡 Hint
Remember that break stops execution inside switch cases.
🔧 Debug
advanced
2:00remaining
Why does this break not stop the loop?
Consider this Java code. Why does the loop terminate immediately without printing any numbers instead of stopping at 3?
Java
public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 3);
                break;
            System.out.println(i);
        }
    }
}
AThe loop condition is wrong, so break never executes.
BThe break is inside the if block and stops the loop at i=3 as expected.
CThe break statement is unreachable and causes a compile error.
DThe semicolon after if condition ends the if statement, so break always executes on first iteration.
Attempts:
2 left
💡 Hint
Check the semicolon after the if condition carefully.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with break usage
Which option shows the code that will cause a compile-time error due to incorrect break usage?
Java
public class Test {
    public static void main(String[] args) {
        // Code snippet here
    }
}
A
int i = 0;
break;
B
while(true) {
  break;
}
C
for(int i=0; i&lt;5; i++) {
  if(i==3) break;
}
D
switch(1) {
  case 1: break;
}
Attempts:
2 left
💡 Hint
break must be inside a loop or switch block.
🚀 Application
expert
2:00remaining
Count iterations before break in nested loop
What is the value of variable total after running this Java code?
Java
public class Test {
    public static void main(String[] args) {
        int total = 0;
        outer: for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                if (i + j > 5) {
                    break outer;
                }
                total++;
            }
        }
        System.out.println(total);
    }
}
A9
B7
C12
D10
Attempts:
2 left
💡 Hint
Count how many times total increments before i+j > 5 triggers break.