Bird
0
0

Why does Kotlin require const val to be assigned only compile-time constants and not runtime values?

hard📝 Conceptual Q10 of 15
Kotlin - Variables and Type System
Why does Kotlin require const val to be assigned only compile-time constants and not runtime values?
ABecause const val is only for variables inside functions
BBecause runtime values are not allowed in Kotlin
CBecause const val values are inlined at compile time for performance
DBecause const val can be changed later
Step-by-Step Solution
Solution:
  1. Step 1: Understand purpose of const val

    const val values are replaced directly in code during compilation (inlined).
  2. Step 2: Reason why only compile-time constants allowed

    Inlining requires values known at compile time, so runtime values are disallowed.
  3. Final Answer:

    Because const val values are inlined at compile time for performance -> Option C
  4. Quick Check:

    Reason for compile-time const = D [OK]
Quick Trick: const val is inlined at compile time, needs fixed value [OK]
Common Mistakes:
MISTAKES
  • Thinking runtime values are allowed
  • Confusing const val with mutable variables
  • Assuming const val is only for function variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes