0
0
Kotlinprogramming~5 mins

Type checking with is operator in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the is operator do in Kotlin?
The is operator checks if a variable is of a specific type and returns true if it matches, otherwise false.
Click to reveal answer
beginner
How do you check if a variable obj is a String in Kotlin?
Use if (obj is String) { ... }. This checks if obj is a String type.
Click to reveal answer
intermediate
What happens to the variable inside the if block after checking with is?
Inside the if block, Kotlin smart casts the variable to the checked type automatically, so you can use it as that type without explicit casting.
Click to reveal answer
beginner
How do you check if a variable is NOT a certain type using the is operator?
Use the negation operator: if (obj !is String) { ... } checks if obj is NOT a String.
Click to reveal answer
intermediate
Can the is operator be used with nullable types?
Yes, but if the variable is null, is returns false because null is not an instance of any type.
Click to reveal answer
What does obj is Int return if obj is 42?
Afalse
Btrue
CThrows an error
DReturns null
What is the result of obj !is String if obj is "hello"?
Afalse
Btrue
CThrows an error
DReturns null
Inside if (obj is String) { }, what type is obj treated as?
AString
BNullable String
CInt
DAny
What does obj is String return if obj is null?
AThrows NullPointerException
Btrue
Cfalse
DReturns null
Which operator checks if a variable is NOT a certain type?
Ais
Bnot
C!==
D!is
Explain how the is operator works in Kotlin and what happens to the variable inside the if block.
Think about how Kotlin lets you use the variable as the checked type without extra casting.
You got /3 concepts.
    Describe how to check if a variable is NOT a certain type and what result you get if the variable is null.
    Remember how null behaves with type checks and how to negate the check.
    You got /3 concepts.