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 number: Int? = null
number?.let { println(it + 10) } ?: println("No value")
A10
Bnull
CNo value
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nullable variable and safe call

    number is null, so number?.let does not run the lambda.
  2. Step 2: Evaluate Elvis operator fallback

    Since let returns null, the Elvis operator ?: triggers and prints "No value".
  3. Final Answer:

    No value -> Option C
  4. Quick Check:

    Safe call with null triggers Elvis fallback = No value [OK]
Quick Trick: Use ?: to handle null case after let [OK]
Common Mistakes:
MISTAKES
  • Expecting let block to run with null
  • Ignoring Elvis operator fallback
  • Confusing null with zero output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes