Challenge - 5 Problems
Kotlin Explicit Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code with explicit type declaration?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
val number: Int = 10 val text: String = "Hello" println("$text $number")
Attempts:
2 left
💡 Hint
Look at how the variables are combined inside the println statement.
✗ Incorrect
The variables 'text' and 'number' are combined with a space in between using string interpolation, so the output is 'Hello 10'.
❓ Predict Output
intermediate2:00remaining
What is the type of variable 'result' after this declaration?
Given the Kotlin code below, what is the explicit type of 'result'?
Kotlin
val result: Double = 5 / 2
Attempts:
2 left
💡 Hint
Remember that 5 and 2 are integers and division between integers behaves differently.
✗ Incorrect
Since 5 and 2 are integers, 5 / 2 performs integer division resulting in 2, then it is converted to Double as 2.0.
🔧 Debug
advanced2:00remaining
Which option causes a compilation error due to explicit type mismatch?
Identify which Kotlin code snippet will cause a compilation error because the assigned value does not match the explicitly declared type.
Attempts:
2 left
💡 Hint
Check if the value assigned matches the declared type exactly.
✗ Incorrect
Assigning an integer 123 to a variable declared as String causes a compilation error due to type mismatch.
❓ Predict Output
advanced2:00remaining
What is the output of this Kotlin code with explicit nullable type?
Analyze the Kotlin code below and select the correct output.
Kotlin
val message: String? = null println(message ?: "No message")
Attempts:
2 left
💡 Hint
The Elvis operator ?: returns the right side if the left side is null.
✗ Incorrect
Since 'message' is null, the Elvis operator returns 'No message'.
🧠 Conceptual
expert2:00remaining
How many variables are explicitly declared as nullable in this Kotlin code?
Count the number of variables explicitly declared with nullable types in the following Kotlin code snippet.
Kotlin
val a: Int? = null val b: String = "text" val c: Double? = 3.14 val d: Boolean = true val e: List<String>? = null
Attempts:
2 left
💡 Hint
Look for the question mark '?' after the type name.
✗ Incorrect
Variables a, c, and e are declared with nullable types (Int?, Double?, List?).