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?
for (i in 1..7 step 3) {
    print(i)
}
A1 3 6
B1 4 7
C1 4 6
D1 3 5 7
Step-by-Step Solution
Solution:
  1. Step 1: Identify loop values with step 3

    Start at 1, add 3 each time until 7: 1, 4, 7.
  2. Step 2: Confirm loop stops at or before 7

    Next step after 7 would be 10 which is beyond 7, so loop ends.
  3. Final Answer:

    1 4 7 -> Option B
  4. Quick Check:

    Range with step 3 outputs 1 4 7 [OK]
Quick Trick: Add step value each iteration until reaching end [OK]
Common Mistakes:
MISTAKES
  • Including numbers not in step sequence
  • Confusing step with increment by 1
  • Misreading range boundaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes