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?
fun printLength(str: String?) {
  println(str!!.length)
}
printLength(null)
AThrows NullPointerException
BPrints 0
CPrints null
DPrints length of null string
Step-by-Step Solution
Solution:
  1. Step 1: Understand non-null assertion operator

    The !! operator forces the value to be non-null or throws an exception if null.
  2. Step 2: Call function with null argument

    Passing null causes str!! to throw a NullPointerException at runtime.
  3. Final Answer:

    Throws NullPointerException -> Option A
  4. Quick Check:

    Non-null assertion on null = Exception [OK]
Quick Trick: !! throws exception if value is null, use safely [OK]
Common Mistakes:
MISTAKES
  • Expecting safe null handling with !!
  • Thinking it prints 0 or null
  • Confusing !! with ?.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes