Bird
0
0

Which of the following is the correct syntax for a safe cast in Kotlin?

easy📝 Syntax Q12 of 15
Kotlin - Null Safety

Which of the following is the correct syntax for a safe cast in Kotlin?

val x = obj ___ String
Aas?
Bto
Cis
Das
Step-by-Step Solution
Solution:
  1. Step 1: Identify safe cast syntax

    In Kotlin, the safe cast operator is as? which tries to cast safely.
  2. Step 2: Match syntax to code

    The code val x = obj as? String attempts to cast obj to String safely.
  3. Final Answer:

    as? -> Option A
  4. Quick Check:

    Safe cast syntax = as? [OK]
Quick Trick: Safe cast uses as? with question mark [OK]
Common Mistakes:
MISTAKES
  • Using as without question mark causes exception
  • Confusing is (type check) with as? (cast)
  • Using to which is for conversions, not casting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes