Challenge - 5 Problems
Labeled Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 + " "); } } } }
Attempts:
2 left
💡 Hint
The labeled break exits the outer loop immediately when the condition is met.
✗ Incorrect
For i=1, j=1 prints 1, j=2 prints 2, j=3 prints 3. For i=2, j=1: 2*1=2 <=3 prints 2, j=2: 2*2=4 >3 break outer. Output: 1 2 3 2
❓ Predict Output
intermediate2: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 + " "); } } } }
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.
✗ Incorrect
i=1, j=1: prints 1, j=2: continue outer (skips j=3, print), goes to i=2. i=2, j=1: prints 2, j=2: continue outer (end). Output: 1 2
🔧 Debug
advanced2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Labeled continue can be used within the body of its own labeled loop and acts like an unlabeled continue.
✗ Incorrect
No compile error. The labeled continue 'continue outer;' skips the print statement when i==1, equivalent to unlabeled continue. Prints 0 (i=0), skips i=1, prints 2 (i=2).
❓ Predict Output
advanced2: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 + " "); } } } }
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.
✗ Incorrect
i=1 j=1: i==j continue outer (no print, skips j=2,3) -> i=2. i=2 j=1: != print 2-1, j=2== continue outer (skips j=3) -> i=3. i=3 j=1 != print 3-1, j=2 != print 3-2, j=3== continue outer (end). Output: 2-1 3-1 3-2
🧠 Conceptual
expert2: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); } }
Attempts:
2 left
💡 Hint
The labeled break exits both loops immediately when i + j > 5.
✗ Incorrect
Counts for i=0 all j=0-4 (5), i=1 all j=0-4 (5), i=2 j=0-3 (4, since j=4: 2+4=6>5 break outer). Total 14.