Recall & Review
beginner
What does <code>const val</code> mean in Kotlin?<p><code>const val</code> declares a compile-time constant. It means the value is fixed and known when the program is compiled, not changed at runtime.</p>Click to reveal answer
beginner
Where can <code>const val</code> be used in Kotlin?<p><code>const val</code> can only be used at the top level or inside an <code>object</code>. It cannot be used inside regular classes or functions.</p>Click to reveal answer
intermediate
Why use <code>const val</code> instead of just <code>val</code>?<p><code>const val</code> is a compile-time constant, so it can be inlined and used in annotations. <code>val</code> is a runtime constant and cannot be used in these cases.</p>Click to reveal answer
beginner
Example: How to declare a constant string with <code>const val</code>?<pre>object Constants {
const val APP_NAME = "MyApp"
}</pre><p>This creates a constant string <code>APP_NAME</code> accessible as <code>Constants.APP_NAME</code>.</p>Click to reveal answer
intermediate
Can <code>const val</code> hold complex objects or only primitives and strings?<p><code>const val</code> can only hold primitive types (like <code>Int</code>, <code>Double</code>) and <code>String</code>. It cannot hold objects or types that require runtime initialization.</p>Click to reveal answer
Which of the following is true about
const val in Kotlin?✗ Incorrect
const val is a compile-time constant, usable in annotations. It cannot be declared inside functions, cannot hold objects, and cannot be changed.
Where can you declare a
const val in Kotlin?✗ Incorrect
const val must be declared at the top level or inside an object. It cannot be inside functions or regular classes.
What types of values can
const val hold?✗ Incorrect
const val can only hold primitive types (like Int, Boolean) and String.
Why might you choose
const val over val?✗ Incorrect
const val values are compile-time constants and can be inlined, making them more efficient and usable in annotations.
Which of these is a valid
const val declaration?✗ Incorrect
Only primitive or string literals can be const val. Calling functions or creating objects is not allowed.
Explain what
const val means in Kotlin and where you can use it.Think about when the value is fixed and where Kotlin allows this declaration.
You got /4 concepts.
Describe the difference between
const val and val in Kotlin.Consider when the value is known and how Kotlin treats each.
You got /4 concepts.