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?✗ Incorrect
Since 42 is an Int,
obj is Int returns true.What is the result of
obj !is String if obj is "hello"?✗ Incorrect
Because
obj is a String, obj !is String returns false.Inside
if (obj is String) { }, what type is obj treated as?✗ Incorrect
Kotlin smart casts
obj to String inside the if block.What does
obj is String return if obj is null?✗ Incorrect
Null is not an instance of any type, so
is returns false.Which operator checks if a variable is NOT a certain type?
✗ Incorrect
The
!is operator checks if a variable is NOT of a given type.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.