A smart cast is when Kotlin automatically converts a variable to a more specific type after checking its type with conditions like if or when, so you don't need to cast it manually.
Click to reveal answer
beginner
How does Kotlin use smart casts inside an if statement?
Inside an if block, if you check a variable's type (e.g., if (x is String)), Kotlin automatically treats x as that type inside the block without needing a manual cast.
Click to reveal answer
intermediate
Explain smart casts with a when expression.
When you use when to check a variable's type, Kotlin smart casts the variable to the matched type inside each branch, so you can use it directly as that type.
Click to reveal answer
intermediate
Why can't Kotlin smart cast a variable if it is mutable (var) and can change?
If a variable is mutable (var) and can be changed elsewhere, Kotlin can't guarantee its type stays the same, so it won't smart cast it to avoid errors.
Click to reveal answer
beginner
Show a simple Kotlin example using smart casts in an if statement.
fun printLength(obj: Any) {
if (obj is String) {
// obj is smart cast to String here
println(obj.length)
} else {
println("Not a string")
}
}
Click to reveal answer
What does Kotlin do after you check if (x is String) inside the if block?
AThrows an error if you use x as String
BAutomatically treats x as String without manual cast
CRequires you to cast x manually
DChanges the type of x permanently
✗ Incorrect
Kotlin smart casts x to String inside the if block after the type check.
Can Kotlin smart cast a var variable inside an if statement?
AOnly if the variable is null
BYes, always
CNo, if the variable can change
DOnly if the variable is a primitive type
✗ Incorrect
Kotlin won't smart cast mutable variables if they can be changed elsewhere.
In a when expression, how does Kotlin handle smart casts?
AIt smart casts the variable to the matched type in each branch
BIt never smart casts in when expressions
CIt requires manual casting in each branch
DIt only smart casts if the variable is a String
✗ Incorrect
Kotlin smart casts the variable to the matched type inside each when branch.
Which of these is a reason Kotlin might NOT smart cast a variable?
AThe variable is immutable (val)
BThe variable is a local variable
CThe variable is checked with is operator
DThe variable is mutable (var) and can be changed
✗ Incorrect
If the variable is mutable and can change, Kotlin avoids smart casting to prevent errors.
What is the benefit of smart casts in Kotlin?
AYou write less code by avoiding manual casts
BIt makes the program slower
CIt requires more manual type checks
DIt disables type safety
✗ Incorrect
Smart casts let you use variables as their checked type without extra casting code.
Explain how Kotlin smart casts work inside an if statement with a type check.
Think about how Kotlin treats the variable after confirming its type.
You got /3 concepts.
Describe why Kotlin might not smart cast a mutable variable and how this affects your code.
Consider what happens if the variable can change after the check.
You got /3 concepts.
Practice
(1/5)
1. What does Kotlin's smart cast feature do inside an if or when block?
easy
A. Disables type checking inside the block
B. Requires explicit casting of variables after type checks
C. Automatically treats a variable as a specific type after a type check
D. Converts all variables to strings inside the block
Solution
Step 1: Understand smart cast purpose
Smart casts let Kotlin automatically treat a variable as a more specific type after you check its type.
Step 2: Apply to if/when blocks
Inside if or when, Kotlin knows the variable's type and lets you use it without manual casting.
Final Answer:
Automatically treats a variable as a specific type after a type check -> Option C
Quick Check:
Smart cast = automatic type use after check [OK]
Hint: Smart casts auto-change type after check in if/when [OK]
Common Mistakes:
Thinking manual cast is always needed
Assuming smart casts disable type checks
Believing variables convert to string automatically
2. Which of the following is the correct syntax to use smart casts in a when expression for variable obj?
easy
A. when obj { is String -> println(obj.length) else -> println("Not a string") }
B. when (obj) { obj is String -> println(obj.length) else -> println("Not a string") }
C. when (obj) { String -> println(obj.length) else -> println("Not a string") }
D. when (obj) { is String -> println(obj.length) else -> println("Not a string") }
Solution
Step 1: Check correct when syntax
The when expression requires parentheses around the variable: when (obj).
Step 2: Use type check correctly
The correct type check is is String without repeating variable name inside the block.
Final Answer:
when (obj) { is String -> println(obj.length) else -> println("Not a string") } -> Option D
Quick Check:
Correct when syntax uses parentheses and is [OK]
Hint: Use parentheses and 'is' keyword in when [OK]
Common Mistakes:
Omitting parentheses around variable
Using variable name inside when branches
Using type name without 'is' keyword
3. What will be the output of this Kotlin code?
fun test(obj: Any) {
when (obj) {
is String -> println(obj.length)
is Int -> println(obj + 1)
else -> println("Unknown")
}
}
test("Hello")
test(5)
test(3.14)
medium
A. 5
6
7
B. 5
6
Unknown
C. Hello
6
Unknown
D. 5
5
Unknown
Solution
Step 1: Analyze each call
For test("Hello"), obj is String, so prints length 5. For test(5), obj is Int, so prints 5 + 1 = 6. For test(3.14), no matching type, prints "Unknown".
Step 2: Confirm output lines
Outputs are: 5 6 Unknown
Final Answer:
5
6
Unknown -> Option B
Quick Check:
Smart cast uses type to choose output [OK]
Hint: Match type in when to get output [OK]
Common Mistakes:
Confusing string length with string itself
Adding 1 to string instead of int
Expecting default else to print number
4. Identify the error in this Kotlin code using smart casts:
fun check(obj: Any) {
if (obj is String) {
println(obj.length)
} else if (obj is Int) {
println(obj + 1)
} else {
println(obj.length)
}
}
medium
A. Trying to access length on obj in else block without type check
B. Missing parentheses in if condition
C. Using else if instead of when
D. Smart casts do not work inside if blocks
Solution
Step 1: Review else block usage
In the else block, obj can be any type, but code tries to access obj.length which is invalid for non-String types.
Step 2: Understand smart cast limits
Smart casts only apply after type checks. Here, no type check before accessing length in else block causes error.
Final Answer:
Trying to access length on obj in else block without type check -> Option A
Quick Check:
Access property only after type check [OK]
Hint: Check type before accessing properties [OK]
Common Mistakes:
Assuming else block has smart cast automatically
Confusing else if with when
Thinking smart casts fail inside if blocks
5. Given a list of Any objects, write a Kotlin function using when with smart casts to return a list of strings describing each element's type and value. What is the correct implementation?
fun describeList(items: List<Any>): List<String> {
return items.map { item ->
when (item) {
is String -> "String of length ${'$'}{item.length}"
is Int -> "Int with value ${'$'}item"
else -> "Unknown type"
}
}
}
hard
A. Correct as is, smart casts allow using item.length and item directly
B. Needs explicit casts like (item as String).length and (item as Int)
C. Cannot use smart casts inside lambda expressions
D. Must use if-else instead of when for smart casts
Solution
Step 1: Check smart cast usage in when
The when expression smart casts item to String or Int automatically after type check.
Step 2: Confirm lambda and map usage
Inside the lambda, smart casts work normally, so accessing item.length and item is valid without explicit casts.
Final Answer:
Correct as is, smart casts allow using item.length and item directly -> Option A
Quick Check:
Smart casts work inside lambdas and when [OK]
Hint: Smart casts work inside lambdas and when blocks [OK]
Common Mistakes:
Adding unnecessary explicit casts
Thinking smart casts don't work in lambdas
Replacing when with if-else unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.