Bird
0
0

What will be the output of the following Kotlin code?

medium📝 query result Q5 of 15
Kotlin - Collections Fundamentals
What will be the output of the following Kotlin code?
val pairs = listOf("a" to 100, "b" to 200)
for ((char, num) in pairs) {
  println("$char -> $num")
}
Achar -> num\nchar -> num
Ba100\nb200
Ca -> 100\nb -> 200
DCompilation error due to incorrect destructuring
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the list

    The list contains pairs: "a" to 100 and "b" to 200.
  2. Step 2: Destructuring in loop

    Each pair is destructured into char and num.
  3. Step 3: Output

    Prints:
    a -> 100
    b -> 200
  4. Final Answer:

    a -> 100\nb -> 200 -> Option C
  5. Quick Check:

    Pairs destructure to variables correctly [OK]
Quick Trick: Pairs destructure into two variables in loops [OK]
Common Mistakes:
MISTAKES
  • Printing concatenated values without separator
  • Assuming destructuring causes errors with pairs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes