Bird
0
0

What will be printed by this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Null Safety
What will be printed by this Kotlin code?
val map: Map? = null
println(map?.get("key") ?: "No value")
Anull
BNo value
CThrows NullPointerException
Dkey
Step-by-Step Solution
Solution:
  1. Step 1: Safe call on nullable map

    The map is null, so map?.get("key") returns null safely.
  2. Step 2: Elvis operator provides fallback

    The Elvis operator returns "No value" because the safe call result is null.
  3. Final Answer:

    No value -> Option B
  4. Quick Check:

    Safe call null + Elvis operator = fallback string [OK]
Quick Trick: Safe call on null map returns null, Elvis gives default [OK]
Common Mistakes:
MISTAKES
  • Expecting null output
  • Assuming exception thrown
  • Confusing key string with value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes