Bird
0
0

Which of the following Kotlin snippets correctly verifies if value is a String?

easy📝 Conceptual Q2 of 15
Kotlin - Variables and Type System
Which of the following Kotlin snippets correctly verifies if value is a String?
Aif (value is String) { /* code */ }
Bif (value instanceof String) { /* code */ }
Cif (value typeOf String) { /* code */ }
Dif (value == String) { /* code */ }
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's type check

    Kotlin uses the is operator to check types.
  2. Step 2: Analyze options

    if (value is String) { /* code */ } uses is correctly; B uses Java syntax, C and D are invalid.
  3. Final Answer:

    if (value is String) { /* code */ } -> Option A
  4. Quick Check:

    Correct Kotlin syntax uses is [OK]
Quick Trick: Use 'is' to check type in Kotlin [OK]
Common Mistakes:
MISTAKES
  • Using 'instanceof' instead of 'is'
  • Trying to compare with '==' for type checking
  • Using non-existent 'typeOf' operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes