Bird
0
0

What will this Kotlin code print?

medium📝 Predict Output Q5 of 15
Kotlin - Loops and Ranges
What will this Kotlin code print?
var sum = 0
repeat(4) { sum += it }
println(sum)
A6
B4
C10
D0
Step-by-Step Solution
Solution:
  1. Step 1: Calculate sum of repeat indices

    repeat(4) runs with it = 0,1,2,3; sum = 0+1+2+3 = 6.
  2. Step 2: Confirm printed value

    After loop, sum is 6, so println prints 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum of 0 to 3 = 6 [OK]
Quick Trick: Sum indices 0 to n-1 for repeat(n) [OK]
Common Mistakes:
MISTAKES
  • Adding wrong range
  • Starting sum at 1
  • Miscounting iterations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes