Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Collections Fundamentals
What will be the output of this Kotlin code?
val numbers = listOf(10, 20, 30)
numbers.forEach { print(it); print(",") }
A10 20 30
B10 20 30
C10, 20, 30,
D10,20,30,
Step-by-Step Solution
Solution:
  1. Step 1: Understand print behavior

    The code prints each number followed immediately by a comma without spaces or newlines.
  2. Step 2: Trace output sequence

    Output is: 10,20,30, all on the same line with commas after each number.
  3. Final Answer:

    10,20,30, -> Option D
  4. Quick Check:

    print without newline outputs continuous text [OK]
Quick Trick: print() does not add newline; println() does [OK]
Common Mistakes:
MISTAKES
  • Expecting spaces after commas
  • Confusing print and println
  • Assuming newlines after each print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes