Bird
0
0

Which of the following when expressions correctly demonstrates Kotlin smart casting?

easy📝 Syntax Q3 of 15
Kotlin - Control Flow as Expressions
Which of the following when expressions correctly demonstrates Kotlin smart casting?
Awhen (obj) { is String -> println(obj.length) else -> println("Not a string") }
Bwhen (val item = obj) { is String -> println(item.length) else -> println("Not a string") }
Cwhen (obj) { obj is String -> println(obj.length) else -> println("Not a string") }
Dwhen { obj is String -> println(obj.length) else -> println("Not a string") }
Step-by-Step Solution
Solution:
  1. Step 1: Review syntax

    Kotlin smart casts work with when when using is Type directly in the branch condition.
  2. Step 2: Analyze options

    when (obj) { is String -> println(obj.length) else -> println("Not a string") } uses when (obj) { is String -> ... } which is correct syntax for smart casts.
  3. Final Answer:

    when (obj) { is String -> println(obj.length) else -> println("Not a string") } correctly uses smart casts in when.
  4. Quick Check:

    Use is Type directly in when branches [OK]
Quick Trick: Use 'is Type' directly in when branches for smart casts [OK]
Common Mistakes:
MISTAKES
  • Using assignment inside when condition
  • Using boolean expressions instead of 'is' checks
  • Misplacing 'is' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes