Bird
0
0

Consider nested loops in Kotlin:

hard📝 Application Q9 of 15
Kotlin - Loops and Ranges
Consider nested loops in Kotlin:
for (i in 1..3) {
    for (j in 1..3) {
        if (j == 2) break
        print("$i$j ")
    }
}

What is the output?
A11 21 31
B11 12 13 21 22 23 31 32 33
C11 12 21 22 31 32
D11 21 31 12 22 32
Step-by-Step Solution
Solution:
  1. Step 1: Understand inner loop break condition

    Inner loop breaks when j == 2, so it only prints when j == 1.
  2. Step 2: Trace output for each outer loop iteration

    For i=1, prints "11 "; i=2 prints "21 "; i=3 prints "31 ".
  3. Final Answer:

    11 21 31 -> Option A
  4. Quick Check:

    Break stops inner loop at j=2, prints only j=1 [OK]
Quick Trick: Break affects only inner loop here [OK]
Common Mistakes:
MISTAKES
  • Assuming break stops outer loop
  • Printing j=2 values
  • Confusing loop nesting effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes