Bird
0
0

Which of the following is the correct syntax for a when expression checking if x is an Int type in Kotlin?

easy📝 Syntax Q3 of 15
Kotlin - Control Flow as Expressions
Which of the following is the correct syntax for a when expression checking if x is an Int type in Kotlin?
Awhen (x) { type Int -> "Integer" }
Bwhen (x) { is Int -> "Integer" }
Cwhen (x) { instanceof Int -> "Integer" }
Dwhen (x) { Int -> "Integer" }
Step-by-Step Solution
Solution:
  1. Step 1: Recall Kotlin type checking syntax

    Kotlin uses is Type inside when to check types.
  2. Step 2: Identify correct syntax

    when (x) { is Int -> "Integer" } uses is Int correctly. Other options use invalid or Java-like syntax.
  3. Final Answer:

    when (x) { is Int -> "Integer" } -> Option B
  4. Quick Check:

    Use 'is Type' to check type in when = A [OK]
Quick Trick: Use 'is' keyword to check type in when expression [OK]
Common Mistakes:
MISTAKES
  • Using 'instanceof' which is Java syntax
  • Writing 'type Int' which is invalid
  • Omitting the 'is' keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes