Bird
0
0

Where can you declare a const val in Kotlin?

easy📝 Conceptual Q2 of 15
Kotlin - Variables and Type System
Where can you declare a const val in Kotlin?
AOnly inside functions
BAt the top-level or inside objects
CInside any class constructor
DOnly inside interfaces
Step-by-Step Solution
Solution:
  1. Step 1: Recall valid locations for const val

    In Kotlin, const val can only be declared at the top-level or inside objects (including companion objects).
  2. Step 2: Evaluate each option

    At the top-level or inside objects correctly states the valid locations. Other options are invalid places for const val.
  3. Final Answer:

    At the top-level or inside objects -> Option B
  4. Quick Check:

    Valid const val location = D [OK]
Quick Trick: const val must be top-level or in objects, not in functions [OK]
Common Mistakes:
MISTAKES
  • Trying to declare const val inside functions
  • Declaring const val inside class constructors
  • Assuming interfaces allow const val

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes