0
0
Javaprogramming~20 mins

Labeled break and continue in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Labeled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of labeled break in nested loops
What is the output of the following Java code using a labeled break?
Java
public class Test {
    public static void main(String[] args) {
        outer:
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (i * j > 3) {
                    break outer;
                }
                System.out.print(i * j + " ");
            }
        }
    }
}
A1 2 3 2 4 6
B1 2 3
C1 2 3 2 4 6 3 6 9
D1 2 3 2
Attempts:
2 left
💡 Hint
The labeled break exits the outer loop immediately when the condition is met.
Predict Output
intermediate
2:00remaining
Output of labeled continue in nested loops
What is the output of this Java code using a labeled continue?
Java
public class Test {
    public static void main(String[] args) {
        outer:
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    continue outer;
                }
                System.out.print(i * j + " ");
            }
        }
    }
}
A1 3 2 3
B1 2
C1 3 3 6
D1 3 2 4
Attempts:
2 left
💡 Hint
The labeled continue skips to the next iteration of the outer loop when j == 2, skipping the print for j=2 and remaining inner iterations.
🔧 Debug
advanced
2:00remaining
Identify error with labeled continue usage
What error does this Java code produce?
Java
public class Test {
    public static void main(String[] args) {
        outer:
        for (int i = 0; i < 3; i++) {
            if (i == 1) {
                continue outer;
            }
            System.out.print(i + " ");
        }
    }
}
AOutput: 0 2
BRuntime exception: Illegal continue label
COutput: 0 1 2
DCompile-time error: continue label not allowed here
Attempts:
2 left
💡 Hint
Labeled continue can be used within the body of its own labeled loop and acts like an unlabeled continue.
Predict Output
advanced
2:00remaining
Output with nested loops and labeled continue
What is the output of this Java program using labeled continue inside nested loops?
Java
public class Test {
    public static void main(String[] args) {
        outer:
        for (int i = 1; i <= 3; i++) {
            inner:
            for (int j = 1; j <= 3; j++) {
                if (i == j) {
                    continue outer;
                }
                System.out.print(i + "-" + j + " ");
            }
        }
    }
}
A1-2 1-3 2-1 2-3 3-1 3-2
B1-2 1-3 2-1 2-3 3-1
C2-1 3-1 3-2
D1-2 1-3 2-1 3-1
Attempts:
2 left
💡 Hint
The continue outer skips the rest of the inner loop and continues the next iteration of the outer loop when i == j.
🧠 Conceptual
expert
2:00remaining
Effect of labeled break on loop variables
Consider the following Java code snippet. What is the final value of variable 'count' after execution?
Java
public class Test {
    public static void main(String[] args) {
        int count = 0;
        outer:
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                if (i + j > 5) {
                    break outer;
                }
                count++;
            }
        }
        System.out.println(count);
    }
}
A14
B21
C25
D10
Attempts:
2 left
💡 Hint
The labeled break exits both loops immediately when i + j > 5.