Bird
0
0

Why does Kotlin allow accessing properties like length directly after an is check without explicit casting?

hard📝 Conceptual Q10 of 15
Kotlin - Variables and Type System
Why does Kotlin allow accessing properties like length directly after an is check without explicit casting?
ABecause Kotlin performs smart casts after type checks
BBecause <code>is</code> operator changes the variable type permanently
CBecause Kotlin ignores type safety inside <code>if</code> blocks
DBecause <code>length</code> is a property of all types
Step-by-Step Solution
Solution:
  1. Step 1: Understand smart casts in Kotlin

    Kotlin automatically casts a variable to a specific type after a successful is check within that scope.
  2. Step 2: Clarify why explicit cast is unnecessary

    This smart cast allows direct access to properties and methods of the checked type without manual casting.
  3. Final Answer:

    Because Kotlin performs smart casts after type checks -> Option A
  4. Quick Check:

    Smart casts explain direct property access = B [OK]
Quick Trick: Smart casts save you from writing explicit casts [OK]
Common Mistakes:
MISTAKES
  • Thinking is changes variable type permanently
  • Assuming all types have length
  • Believing Kotlin ignores type safety

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes