Bird
0
0

Which of the following is the correct syntax for a Kotlin for loop that prints numbers 1 to 3?

easy📝 Syntax Q3 of 15
Kotlin - Loops and Ranges
Which of the following is the correct syntax for a Kotlin for loop that prints numbers 1 to 3?
Afor (i in 1..3) println(i)
Bfor (i = 1; i <= 3; i++) println(i)
Cfor i in 1..3 { println(i) }
Dfor i = 1 to 3 { println(i) }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin for loop syntax

    Kotlin requires parentheses around the loop variable and range: for (i in 1..3).
  2. Step 2: Check each option

    for (i in 1..3) println(i) matches correct syntax. A misses parentheses, B uses Java-style syntax, D uses invalid syntax.
  3. Final Answer:

    for (i in 1..3) println(i) -> Option A
  4. Quick Check:

    Kotlin for loop needs parentheses and 'in' keyword = C [OK]
Quick Trick: Kotlin for loops use 'for (var in range)' syntax [OK]
Common Mistakes:
MISTAKES
  • Omitting parentheses around loop variable
  • Using Java-style for loop syntax
  • Using 'to' instead of '..' for ranges

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes