Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Loops and Ranges
What will be the output of this Kotlin code?
repeat(3) { i -> println("Count: $i") }
ACount: 0 Count: 1 Count: 2
BCount: 1 Count: 2 Count: 3
CCount: 0 Count: 1 Count: 2 Count: 3
DCount: 3 Count: 2 Count: 1
Step-by-Step Solution
Solution:
  1. Step 1: Understand repeat index range

    The lambda parameter i starts at 0 and goes to 2 for repeat(3).
  2. Step 2: Match output with index values

    Output prints "Count: 0", "Count: 1", "Count: 2" exactly three times.
  3. Final Answer:

    Count: 0 Count: 1 Count: 2 -> Option A
  4. Quick Check:

    repeat index starts at 0, ends at n-1 [OK]
Quick Trick: repeat index starts at zero, not one [OK]
Common Mistakes:
MISTAKES
  • Assuming index starts at 1
  • Expecting extra iteration
  • Confusing order of output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes