Bird
0
0

What will be the output of this Kotlin code?

medium📝 Predict Output Q4 of 15
Kotlin - Null Safety
What will be the output of this Kotlin code?
fun main() {
    val message: String = "Hello"
    println(message.length)
}
A5
Bnull
CCompilation error
D0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze variable declaration

    message is a non-nullable String initialized with "Hello".
  2. Step 2: Calculate length of the string

    "Hello" has 5 characters, so message.length is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Non-nullable String length = 5 [OK]
Quick Trick: Non-nullable String holds value, length is count of chars [OK]
Common Mistakes:
MISTAKES
  • Expecting null output
  • Thinking compilation error due to null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes