Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q5 of 15
Kotlin - Null Safety
What will be the output of this Kotlin code?
var number: Int? = 10
number = null
println(number?.plus(5) ?: "No value")
ANo value
Bnull
C15
DThrows NullPointerException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze nullable variable and safe call

    number is null, so number?.plus(5) returns null.
  2. Step 2: Apply Elvis operator fallback

    Since the safe call returns null, the expression prints "No value".
  3. Final Answer:

    No value -> Option A
  4. Quick Check:

    Safe call with null triggers fallback [OK]
Quick Trick: Safe calls return null if variable is null, use ?: for fallback [OK]
Common Mistakes:
MISTAKES
  • Expecting 15 despite null value
  • Thinking null is printed
  • Assuming exception is thrown

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes