Bird
0
0

Find the mistake in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Loops and Ranges
Find the mistake in this Kotlin code snippet:
for (i in 1..5 step 0) { println(i) }
Astep zero means increment by 1
Bstep zero means decrement by 1
CNo mistake, code runs fine
Dstep cannot be zero, causes runtime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand step parameter

    The step value must be a positive integer greater than zero.
  2. Step 2: Effect of step 0

    Using step 0 causes an illegal argument exception at runtime.
  3. Final Answer:

    step cannot be zero, causes runtime error -> Option D
  4. Quick Check:

    Step must be > 0 [OK]
Quick Trick: Step value must be positive and non-zero [OK]
Common Mistakes:
MISTAKES
  • Assuming step 0 means step 1
  • Expecting code to run without error
  • Confusing step with decrement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes