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 this Kotlin code using a labeled break?
Kotlin
fun main() { outer@ for (i in 1..3) { for (j in 1..3) { if (i * j > 3) break@outer print("$i$j ") } } }
Attempts:
2 left
💡 Hint
The labeled break exits the outer loop when the condition is met.
✗ Incorrect
The code prints pairs i,j until i*j > 3. When i=2 and j=2, 2*2=4 > 3 triggers break@outer, stopping all loops. So output is 11 12 13 21.
❓ Predict Output
intermediate2:00remaining
Output of labeled continue in nested loops
What does this Kotlin code print when using a labeled continue?
Kotlin
fun main() { outer@ for (i in 1..2) { for (j in 1..3) { if (j == 2) continue@outer print("$i$j ") } } }
Attempts:
2 left
💡 Hint
The labeled continue skips to the next iteration of the outer loop when j == 2.
✗ Incorrect
When j == 2, continue@outer skips the rest of the inner loop and moves to next i. So for i=1, j=1 prints 11, j=2 triggers continue@outer skipping j=3. For i=2, j=1 prints 21, j=2 triggers continue@outer skipping j=3. So output is 11 21.
🔧 Debug
advanced2:00remaining
Identify the error with labeled break usage
What error does this Kotlin code produce?
Kotlin
fun main() { for (i in 1..3) { inner@ for (j in 1..3) { if (i == 2) break@outer print("$i$j ") } } }
Attempts:
2 left
💡 Hint
Check if the label used in break exists in the code.
✗ Incorrect
The code tries to break@outer, but no label named 'outer' is defined. This causes a compile-time error: Unresolved reference: outer.
📝 Syntax
advanced2:00remaining
Which option correctly uses labeled continue?
Which of these Kotlin code snippets correctly uses labeled continue to skip the current iteration of the outer loop?
Attempts:
2 left
💡 Hint
Labeled continue syntax requires '@' after the label name.
✗ Incorrect
Option C correctly uses 'continue@outer' to skip the current iteration of the outer loop. Option C misses '@', C uses incomplete label, D uses undefined label 'inner'.
🚀 Application
expert2:00remaining
Count iterations skipped by labeled continue
Given this Kotlin code, what value is printed by println(count)?
Kotlin
fun main() { var count = 0 outer@ for (i in 1..3) { for (j in 1..3) { count++ if (i + j == 4) continue@outer } } println(count) }
Attempts:
2 left
💡 Hint
The continue@outer skips the rest of the inner loop and moves to next i when i+j == 4.
✗ Incorrect
The pairs (i,j) are (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3). When i+j==4, pairs are (1,3),(2,2),(3,1). At these points, continue@outer skips remaining inner loop iterations and increments count only for executed count++ statements. Counting executed increments gives 6.