Bird
0
0

What is the output of this Kotlin code?

medium📝 Predict Output Q13 of 15
Kotlin - Collections Fundamentals
What is the output of this Kotlin code?
val map = mapOf("x" to 10, "y" to 20)
println(map["x"])
println(map["z"])
A20 null
B10 null
Cnull null
D10 0
Step-by-Step Solution
Solution:
  1. Step 1: Access existing key "x"

    map["x"] returns 10 because "x" is a key with value 10.
  2. Step 2: Access non-existing key "z"

    map["z"] returns null because "z" is not in the map.
  3. Final Answer:

    10\nnull -> Option B
  4. Quick Check:

    Existing key returns value, missing key returns null [OK]
Quick Trick: map[key] returns value or null if key missing [OK]
Common Mistakes:
MISTAKES
  • Expecting 0 instead of null for missing key
  • Confusing values of different keys
  • Assuming map is mutable and can add keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes