Bird
0
0

What will be printed by the following Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Null Safety
What will be printed by the following Kotlin code?
val message: String? = null
println(message?.toUpperCase() ?: "Empty")
AException thrown
Bnull
CEmpty
Dnull.toUpperCase()
Step-by-Step Solution
Solution:
  1. Step 1: Analyze safe call operator

    The safe call operator (?.) calls toUpperCase() only if message is not null.
  2. Step 2: Use Elvis operator

    Since message is null, the Elvis operator (?:) returns "Empty".
  3. Final Answer:

    Empty -> Option C
  4. Quick Check:

    Safe call with Elvis returns default if null [OK]
Quick Trick: Safe call with ?: returns default if null [OK]
Common Mistakes:
MISTAKES
  • Expecting null to be printed
  • Assuming exception is thrown
  • Ignoring Elvis operator behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes