Bird
0
0

What is the output of this Kotlin code?

medium📝 query result Q13 of 15
Kotlin - Collections Fundamentals
What is the output of this Kotlin code?
val map = mapOf("x" to 10, "y" to 20)
for ((k, v) in map) {
  println("$k$v")
}
ASyntax error
B10x\n20y
Ck v\nk v
Dx10\ny20
Step-by-Step Solution
Solution:
  1. Step 1: Understand map entries and destructuring

    The map has entries "x" to 10 and "y" to 20. The loop destructures each entry into k and v.
  2. Step 2: Print key and value concatenated

    Inside the loop, println("$k$v") prints key and value together without space, so outputs are "x10" and "y20" on separate lines.
  3. Final Answer:

    x10\ny20 -> Option D
  4. Quick Check:

    Print key + value per entry [OK]
Quick Trick: Destructure map entries to print key and value [OK]
Common Mistakes:
MISTAKES
  • Swapping key and value in output
  • Expecting spaces between key and value
  • Confusing variable names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes