Bird
0
0

Find the problem in this Kotlin code snippet:

medium📝 Debug Q7 of 15
Kotlin - Variables and Type System
Find the problem in this Kotlin code snippet:
object Constants {
    const val VERSION = getVersion()
    fun getVersion() = "1.0"
}
A<code>const val</code> must be assigned a compile-time constant, not a function call
BFunction getVersion() should be declared outside the object
CVERSION should be a var, not const val
DObject cannot contain functions
Step-by-Step Solution
Solution:
  1. Step 1: Understand const val assignment rules

    const val requires a compile-time constant value, not a runtime function call.
  2. Step 2: Analyze the code

    Assigning getVersion() to VERSION is invalid because it's a function call.
  3. Final Answer:

    const val must be assigned a compile-time constant, not a function call -> Option A
  4. Quick Check:

    const val assignment rule = A [OK]
Quick Trick: const val needs fixed value, no function calls [OK]
Common Mistakes:
MISTAKES
  • Assigning function calls to const val
  • Thinking functions can't be inside objects
  • Using var instead of val for constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes