0
0
Kotlinprogramming~10 mins

Type checking with is operator in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable obj is a String.

Kotlin
if (obj [1] String) {
    println("It's a string")
}
Drag options to blanks, or click blank then click option'
A==
Bequals
Cinstanceof
Dis
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'is' for type checking.
Using 'instanceof' which is Java syntax, not Kotlin.
2fill in blank
medium

Complete the code to check if value is NOT an Int.

Kotlin
if (value ![1] Int) {
    println("Not an integer")
}
Drag options to blanks, or click blank then click option'
Ais
Attempts:
3 left
💡 Hint
Common Mistakes
Placing the exclamation mark after 'is' instead of before.
Using '!=' instead of '!is' for type checking.
3fill in blank
hard

Fix the error in the code to check if item is a Double.

Kotlin
if (item [1] Double) {
    println("It's a double")
}
Drag options to blanks, or click blank then click option'
A==
Bequals
Cis
Dinstanceof
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java's 'instanceof' instead of Kotlin's 'is'.
Using '==' which compares values, not types.
4fill in blank
hard

Fill both blanks to check if data is a String and print its length.

Kotlin
if (data [1] String) {
    println(data[2]length)
}
Drag options to blanks, or click blank then click option'
Ais
B==
C?.
D!!
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'is' for type checking.
Using '.' or '?.' instead of '!!' which is appropriate after type check.
5fill in blank
hard

Fill all three blanks to check if input is NOT a List and print a message.

Kotlin
if (input [1] [2] [3]) {
    println("Input is not a list")
}
Drag options to blanks, or click blank then click option'
Ais
BList
C)
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the exclamation mark to negate the check.
Not closing the if condition with a parenthesis.