0
0
Kotlinprogramming~20 mins

Constant values with const val in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Const Val Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1: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)
}
A3.14
BPI
CCompilation error
D0.0
Attempts:
2 left
💡 Hint
const val values are compile-time constants and can be accessed directly.
Predict Output
intermediate
1: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)
}
ARuntime error
BRADIUS
Cnull
D5
Attempts:
2 left
💡 Hint
const val in companion object acts like a static constant.
Predict Output
advanced
2: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)
}
Atrue
BCompilation error
Cfalse
DRuntime exception
Attempts:
2 left
💡 Hint
const val is a primitive compile-time constant, val is a runtime object.
Predict Output
advanced
1: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)
}
AModifier 'const' is not allowed here
BUnresolved reference: number
CNo error, prints 10
DRuntime exception
Attempts:
2 left
💡 Hint
const val can only be declared at top-level or in object/companion object.
🧠 Conceptual
expert
2:00remaining
Why use const val instead of val in Kotlin?
Which statement best explains the advantage of using const val over val in Kotlin?
Aconst val allows changing the value at runtime for flexibility
Bconst val values are inlined at compile time, improving performance and allowing usage in annotations
Cval variables are always compile-time constants, const val is redundant
Dconst val can only be used inside functions, val cannot
Attempts:
2 left
💡 Hint
Think about when the value is fixed and how Kotlin uses it internally.