Bird
0
0

What will be printed by this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Loops and Ranges
What will be printed by this Kotlin code?
var total = 0
for (i in 2..6 step 2) {
  total += i
}
println(total)
A10
B14
C12
D8
Step-by-Step Solution
Solution:
  1. Step 1: Identify loop values with step

    The loop iterates i = 2, 4, 6 (step 2 from 2 to 6).
  2. Step 2: Calculate total sum

    Sum = 2 + 4 + 6 = 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    step controls increment; sum of 2,4,6 = 12 [OK]
Quick Trick: step defines increment size in ranges [OK]
Common Mistakes:
MISTAKES
  • Adding wrong numbers (like 2,3,4,5,6)
  • Ignoring step and summing all numbers
  • Confusing step with decrement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes