Challenge - 5 Problems
Const Val Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of accessing const val in Kotlin
What is the output of this Kotlin code snippet?
Kotlin
object Constants { const val PI = 3.14 } fun main() { println(Constants.PI) }
Attempts:
2 left
💡 Hint
const val values are compile-time constants and can be accessed directly.
✗ Incorrect
The const val PI is a compile-time constant and can be accessed directly without an instance. The println prints 3.14.
❓ Predict Output
intermediate1:30remaining
Value of const val in companion object
What will this Kotlin program print?
Kotlin
class Circle { companion object { const val RADIUS = 5 } } fun main() { println(Circle.RADIUS) }
Attempts:
2 left
💡 Hint
const val in companion object acts like a static constant.
✗ Incorrect
The const val RADIUS is accessible via the class name and prints 5.
❓ Predict Output
advanced2:00remaining
Effect of const val on memory usage
Consider this Kotlin code. What is the output?
Kotlin
object Config { const val MAX_USERS = 1000 val maxUsersRuntime = 1000 } fun main() { println(Config.MAX_USERS === Config.maxUsersRuntime) }
Attempts:
2 left
💡 Hint
const val is a primitive compile-time constant, val is a runtime object.
✗ Incorrect
MAX_USERS is a primitive constant, maxUsersRuntime is an object. === compares references, so false.
❓ Predict Output
advanced1:30remaining
Compilation error with const val usage
What error does this Kotlin code produce?
Kotlin
class Test { const val number = 10 } fun main() { println(Test().number) }
Attempts:
2 left
💡 Hint
const val can only be declared at top-level or in object/companion object.
✗ Incorrect
const val cannot be declared as a member property of a regular class. It must be in object or companion object.
🧠 Conceptual
expert2:00remaining
Why use const val instead of val in Kotlin?
Which statement best explains the advantage of using const val over val in Kotlin?
Attempts:
2 left
💡 Hint
Think about when the value is fixed and how Kotlin uses it internally.
✗ Incorrect
const val is a compile-time constant, so its value is inlined and can be used in annotations. val is a runtime constant and cannot be used in annotations.