Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Loops and Ranges
What will be the output of this Kotlin code?
outerLoop@ for (a in 1..2) {
  innerLoop@ for (b in 1..3) {
    if (b == 2) continue@outerLoop
    print("$a$b ")
  }
}
A11 13 21 23
B11 12 13 21 22 23
C11 21
D12 13 22 23
Step-by-Step Solution
Solution:
  1. Step 1: Understand loops

    Outer loop a=1..2, inner loop b=1..3.
  2. Step 2: Condition and continue@outerLoop

    When b==2, continue@outerLoop skips to next iteration of outer loop.
  3. Step 3: Output reasoning

    For a=1: b=1 prints "11 ", b=2 triggers continue@outerLoop (skip rest of inner loop and increment a). For a=2: b=1 prints "21 ", b=2 triggers continue@outerLoop again.
  4. Final Answer:

    11 13 21 23 -> Option A
  5. Quick Check:

    continue@outerLoop skips inner loop rest and continues outer [OK]
Quick Trick: continue@label skips to next iteration of labeled loop [OK]
Common Mistakes:
MISTAKES
  • Assuming continue@label skips only inner loop iteration
  • Printing values after continue condition
  • Confusing continue@label with break@label

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes