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
✗ Incorrect
Since 42 is an Int, obj is Int returns true.
What is the result of obj !is String if obj is "hello"?
Afalse
Btrue
CThrows an error
DReturns null
✗ Incorrect
Because obj is a String, obj !is String returns false.
Inside if (obj is String) { }, what type is obj treated as?
AString
BNullable String
CInt
DAny
✗ Incorrect
Kotlin smart casts obj to String inside the if block.
What does obj is String return if obj is null?
AThrows NullPointerException
Btrue
Cfalse
DReturns 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?
Ais
Bnot
C!==
D!is
✗ 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.
Practice
(1/5)
1. What does the is operator do in Kotlin?
easy
A. Checks if a variable is of a specific type
B. Changes the type of a variable
C. Creates a new variable
D. Deletes a variable
Solution
Step 1: Understand the purpose of is operator
The is operator is used to check if a variable holds a value of a certain type.
Step 2: Differentiate from other options
It does not change, create, or delete variables; it only checks type.
Final Answer:
Checks if a variable is of a specific type -> Option A
Quick Check:
Type check = A [OK]
Hint: Remember: is means 'is this type?' check [OK]
Common Mistakes:
Thinking is changes the variable type
Confusing is with variable creation
Assuming is deletes variables
2. Which of the following is the correct syntax to check if obj is a String in Kotlin?
easy
A. if (obj instanceof String)
B. if (obj is String)
C. if (obj type String)
D. if (obj == String)
Solution
Step 1: Recall Kotlin syntax for type checking
Kotlin uses is keyword to check types, not instanceof or other keywords.
Step 2: Match the correct syntax
The correct syntax is if (obj is String).
Final Answer:
if (obj is String) -> Option B
Quick Check:
Correct syntax = C [OK]
Hint: Use is keyword, not Java's instanceof [OK]
Common Mistakes:
Using Java syntax instanceof in Kotlin
Writing obj type String which is invalid
Comparing with == instead of type check
3. What will be the output of this Kotlin code?
fun checkType(x: Any) {
if (x is Int) {
println(x + 5)
} else {
println("Not an Int")
}
}
checkType(10)
checkType("Hello")
medium
A. Error at runtime
B. 10
Not an Int
C. 15
Hello5
D. 15
Not an Int
Solution
Step 1: Analyze the first call checkType(10)
10 is an Int, so inside the if block, x + 5 equals 15, printed as "15".
Step 2: Analyze the second call checkType("Hello")
"Hello" is not an Int, so else block runs printing "Not an Int".
Final Answer:
15
Not an Int -> Option D
Quick Check:
Int check prints sum, else prints message = A [OK]
Hint: If is true, use variable as that type inside block [OK]
Common Mistakes:
Assuming string "Hello" can be added to 5
Expecting runtime error instead of else output
Confusing output order
4. Identify the error in this Kotlin code snippet:
fun printLength(obj: Any) {
if (obj is String) {
println(obj.length())
}
}
medium
A. No error, code is correct
B. Missing else block after if
C. Using parentheses with length property
D. Incorrect use of is operator
Solution
Step 1: Check usage of length on String
In Kotlin, length is a property, not a function, so it should not have parentheses.
Step 2: Confirm other parts are correct
The is operator is used correctly and else block is optional.
Final Answer:
Using parentheses with length property -> Option C
Quick Check:
Property access no parentheses = D [OK]
Hint: Remember: Kotlin properties have no parentheses [OK]
Common Mistakes:
Adding parentheses to properties like length
Thinking else block is mandatory
Misusing is operator syntax
5. Given a list of Any elements, how can you create a new list containing only the strings using the is operator in Kotlin?
hard
A. val strings = list.filterIsInstance<String>()
B. val strings = list.filter { it is String }
C. val strings = list.map { if (it is String) it else null }
D. val strings = list.filter { it == String }
Solution
Step 1: Understand filtering by type
To get only strings, Kotlin provides filterIsInstance<String>() which filters elements by type safely.
Step 2: Evaluate other options
val strings = list.filter { it is String } filters by is String but keeps type as Any, not cast to String. val strings = list.map { if (it is String) it else null } maps to nullable list, not filtering. val strings = list.filter { it == String } compares with type itself, which is invalid.
Final Answer:
val strings = list.filterIsInstance<String>() -> Option A
Quick Check:
Use filterIsInstance for type filtering [OK]
Hint: Use filterIsInstance to get typed list quickly [OK]
Common Mistakes:
Using filter without casting
Mapping to nullable instead of filtering
Comparing element to type object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.