0
0
Kotlinprogramming~20 mins

Smart casts in when and if in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Smart Casts Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of smart cast in if condition
What is the output of this Kotlin code using smart casts in an if statement?
Kotlin
fun main() {
    val obj: Any = "Hello"
    if (obj is String) {
        println(obj.length)
    } else {
        println("Not a string")
    }
}
A5
BNot a string
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
The variable obj is checked to be a String before accessing length.
Predict Output
intermediate
2:00remaining
Smart cast behavior in when expression
What will be printed by this Kotlin code using smart casts in a when expression?
Kotlin
fun main() {
    val x: Any = 42
    when (x) {
        is String -> println("String of length ${x.length}")
        is Int -> println("Int doubled is ${x * 2}")
        else -> println("Unknown type")
    }
}
ACompilation error
BInt doubled is 84
CUnknown type
DString of length 2
Attempts:
2 left
💡 Hint
Check the type of x and how smart casts work in when branches.
🔧 Debug
advanced
2:00remaining
Why does this smart cast fail?
Consider this Kotlin code snippet. Why does it fail to compile?
Kotlin
fun main() {
    var obj: Any? = "Kotlin"
    if (obj is String && obj.length > 3) {
        println(obj.length)
    }
}
ABecause length property is not accessible on String
BBecause obj is not declared as val
CBecause && operator cannot be used with smart casts
DBecause obj is nullable, smart cast is not possible without explicit null check
Attempts:
2 left
💡 Hint
Think about nullability and smart casts combined with logical operators.
📝 Syntax
advanced
2:00remaining
Identify the syntax error with smart casts in when
Which option contains a syntax error related to smart casts in this Kotlin when expression?
Kotlin
fun main() {
    val y: Any = 10
    when (y) {
        is String -> println(y.length)
        is Int -> println(y + 5)
        else -> println("Unknown")
    }
}
AReplacing 'else -> println("Unknown")' with 'else println("Unknown")'
BReplacing 'is String -> println(y.length)' with 'is String -> println(y.toUpperCase())'
CReplacing 'when (y)' with 'when y'
DReplacing 'is Int -> println(y + 5)' with 'is Int -> println(y.length)'
Attempts:
2 left
💡 Hint
Check the syntax of when branches and the use of arrows.
🚀 Application
expert
3:00remaining
Determine the number of items in the resulting map
What is the number of entries in the map produced by this Kotlin code using smart casts in when?
Kotlin
fun main() {
    val items: List<Any> = listOf("apple", 10, "banana", 3.14)
    val result = items.mapNotNull {
        when (it) {
            is String -> it.uppercase()
            is Int -> (it * 2).toString()
            else -> null
        }
    }.associateWith { it.length }
    println(result.size)
}
A2
B5
C3
D4
Attempts:
2 left
💡 Hint
Count how many items are transformed and not filtered out.