Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Loops and Ranges
What will be the output of this Kotlin code?
for (i in 1..5) {
    if (i == 3) break
    print(i)
}
AError
B12345
C345
D12
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop and break condition

    The loop runs from 1 to 5. When i equals 3, break stops the loop immediately.
  2. Step 2: Determine printed values before break

    Values 1 and 2 are printed before hitting i == 3, then loop stops.
  3. Final Answer:

    12 -> Option D
  4. Quick Check:

    Loop stops at 3, prints 1 and 2 [OK]
Quick Trick: Break stops loop before printing 3 [OK]
Common Mistakes:
MISTAKES
  • Assuming 3 is printed before break
  • Thinking loop continues after break
  • Confusing break with continue

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes