Bird
0
0

What does Kotlin's smart cast feature do inside an if or when block?

easy📝 Conceptual Q11 of 15
Kotlin - Control Flow as Expressions
What does Kotlin's smart cast feature do inside an if or when block?
ADisables type checking inside the block
BRequires explicit casting of variables after type checks
CAutomatically treats a variable as a specific type after a type check
DConverts all variables to strings inside the block
Step-by-Step Solution
Solution:
  1. Step 1: Understand smart cast purpose

    Smart casts let Kotlin automatically treat a variable as a more specific type after you check its type.
  2. Step 2: Apply to if/when blocks

    Inside if or when, Kotlin knows the variable's type and lets you use it without manual casting.
  3. Final Answer:

    Automatically treats a variable as a specific type after a type check -> Option C
  4. Quick Check:

    Smart cast = automatic type use after check [OK]
Quick Trick: Smart casts auto-change type after check in if/when [OK]
Common Mistakes:
MISTAKES
  • Thinking manual cast is always needed
  • Assuming smart casts disable type checks
  • Believing variables convert to string automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes