0
0
Kotlinprogramming~20 mins

Any type as universal base in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Any Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of casting Any to specific types
What is the output of this Kotlin code snippet?
Kotlin
fun main() {
    val value: Any = 42
    when (value) {
        is String -> println("String: $value")
        is Int -> println("Int: $value")
        else -> println("Unknown type")
    }
}
AString: 42
BInt: 42
CUnknown type
DCompilation error
Attempts:
2 left
💡 Hint
Remember that 'value' is assigned an Int but typed as Any.
🧠 Conceptual
intermediate
2:00remaining
Why is Any the universal base type in Kotlin?
Which statement best explains why Kotlin uses 'Any' as the universal base type?
ABecause all Kotlin classes inherit from 'Any', allowing any value to be stored in variables of type 'Any'.
BBecause 'Any' is used only for nullable types.
CBecause 'Any' is an interface that all classes implement explicitly.
DBecause 'Any' can hold only primitive types like Int and Double.
Attempts:
2 left
💡 Hint
Think about inheritance and type hierarchy in Kotlin.
🔧 Debug
advanced
2:00remaining
Identify the runtime error with Any casting
What runtime error will this Kotlin code produce?
Kotlin
fun main() {
    val obj: Any = "Hello"
    val num: Int = obj as Int
    println(num)
}
APrints 'Hello' without error
BCompilation error due to invalid cast
CClassCastException at runtime
DPrints 0 due to default conversion
Attempts:
2 left
💡 Hint
Casting a String to Int directly is unsafe.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly checks type of Any?
Which of the following Kotlin snippets correctly checks if a variable of type Any holds a String and prints it?
Aif (obj is String()) println(obj) else println("Not a String")
Bif (obj instanceof String) println(obj) else println("Not a String")
Cif (obj.type == String) println(obj) else println("Not a String")
Dif (obj is String) println(obj) else println("Not a String")
Attempts:
2 left
💡 Hint
Remember Kotlin uses 'is' for type checking.
🚀 Application
expert
2:00remaining
Result of storing different types in a List
What will be the output of this Kotlin program?
Kotlin
fun main() {
    val items: List<Any> = listOf(10, "Kotlin", 3.14, true)
    for (item in items) {
        when (item) {
            is Int -> print("I")
            is String -> print("S")
            is Double -> print("D")
            is Boolean -> print("B")
            else -> print("?")
        }
    }
}
AISDB
BIDSB
CISD?
DCompilation error
Attempts:
2 left
💡 Hint
Check the type of each element in the list and the corresponding print letter.