<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?
AIt is a compile-time constant and can be used in annotations.
BIt can be declared inside any function.
CIt can hold any object type.
DIt can be changed after initialization.
✗ 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?
AInside a regular class.
BInside a function.
CInside an <code>object</code> or at the top level.
DInside a <code>var</code> declaration.
✗ 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?
AOnly nullable types.
BOnly primitive types and strings.
COnly collections like lists or maps.
DAny object type.
✗ Incorrect
const val can only hold primitive types (like Int, Boolean) and String.
Why might you choose const val over val?
ABecause <code>const val</code> values are known at compile time and can be inlined.
BBecause <code>const val</code> values are mutable.
CBecause <code>val</code> can only be used inside functions.
DBecause <code>val</code> cannot hold strings.
✗ 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?
Aconst val list = listOf(1, 2, 3)
Bconst val name = getName()
Cconst val user = User()
Dconst val PI = 3.14
✗ 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.
Practice
(1/5)
1. What is the main purpose of using const val in Kotlin?
easy
A. To declare a constant value that cannot change during program execution
B. To create a variable that can be changed anytime
C. To define a function that returns a constant
D. To declare a class property that is mutable
Solution
Step 1: Understand what const val means
const val is used to declare constants in Kotlin, which are values fixed at compile time and cannot be changed later.
Step 2: Compare options with the definition
To declare a constant value that cannot change during program execution correctly states that const val declares a constant value that never changes. Other options describe variables or functions, which are not correct.
Final Answer:
To declare a constant value that cannot change during program execution -> Option A
Quick Check:
const val = fixed value [OK]
Hint: Remember: const val means fixed value, no changes allowed [OK]
Common Mistakes:
Thinking const val can be changed like a variable
Confusing const val with regular val or var
Assuming const val can hold complex objects
2. Which of the following is the correct syntax to declare a constant integer named MAX_COUNT with value 100 in Kotlin?
easy
A. val MAX_COUNT = 100
B. var MAX_COUNT = 100
C. const MAX_COUNT = 100
D. const val MAX_COUNT = 100
Solution
Step 1: Recall the correct syntax for constants
In Kotlin, constants are declared using const val followed by the name and value.
Step 2: Check each option
const val MAX_COUNT = 100 uses const val MAX_COUNT = 100, which is the correct syntax. val MAX_COUNT = 100 misses const, C misses val, and B uses var which is mutable.
Final Answer:
const val MAX_COUNT = 100 -> Option D
Quick Check:
const val + name + = + value = correct syntax [OK]
Hint: const val always together for constants [OK]
Common Mistakes:
Omitting const keyword
Using var instead of val
Writing const without val
3. What will be the output of the following Kotlin code?
const val PI = 3.14
fun main() {
println("Value of PI: $PI")
}
medium
A. Compilation error
B. Value of PI: $PI
C. Value of PI: 3.14
D. Value of PI: 3
Solution
Step 1: Understand string interpolation in Kotlin
The println statement uses $PI which means it will replace $PI with the value of the constant PI.
Step 2: Check the constant value and output
PI is declared as 3.14, so the output will be "Value of PI: 3.14" exactly.
Final Answer:
Value of PI: 3.14 -> Option C
Quick Check:
String interpolation replaces $PI with 3.14 [OK]
Hint: Look for $variable inside strings for value substitution [OK]
Common Mistakes:
Thinking $PI prints literally as $PI
Expecting integer output instead of float
Assuming compilation error due to const val usage
4. Identify the error in the following Kotlin code:
const val greeting = "Hello"
fun main() {
greeting = "Hi"
println(greeting)
}
medium
A. const val must be declared inside a function
B. Cannot reassign a value to a const val
C. Missing semicolon after greeting declaration
D. Variable greeting is not initialized
Solution
Step 1: Understand const val immutability
const val declares a constant that cannot be changed after initialization.
Step 2: Analyze the reassignment
The code tries to assign a new value to greeting which is a constant. This causes a compilation error.
Final Answer:
Cannot reassign a value to a const val -> Option B
Quick Check:
const val = fixed, no reassignment allowed [OK]
Hint: const val values cannot be changed after declaration [OK]
Common Mistakes:
Trying to assign new value to const val
Thinking semicolon is required in Kotlin
Declaring const val inside functions (allowed only at top-level or object)
5. You want to create a constant string APP_NAME that can be used throughout your Kotlin app. Which of the following is the best way to declare it to ensure it is a compile-time constant accessible everywhere?
hard
A. Declare const val APP_NAME = "MyApp" at the top level or inside an object
B. Declare val APP_NAME = "MyApp" inside main() function
C. Declare var APP_NAME = "MyApp" inside a class constructor
D. Declare const var APP_NAME = "MyApp" inside a class
Solution
Step 1: Understand where const val can be declared
In Kotlin, const val must be declared at the top level or inside an object to be a compile-time constant accessible everywhere.
Step 2: Evaluate each option
Declare const val APP_NAME = "MyApp" at the top level or inside an object correctly declares const val APP_NAME = "MyApp" at the top level or inside an object. Declare val APP_NAME = "MyApp" inside main() function uses val inside a function, which is not compile-time constant. Declare var APP_NAME = "MyApp" inside a class constructor uses var which is mutable. Declare const var APP_NAME = "MyApp" inside a class uses invalid syntax const var.
Final Answer:
Declare const val APP_NAME = "MyApp" at the top level or inside an object -> Option A
Quick Check:
const val + top-level/object = global constant [OK]
Hint: const val must be top-level or in object for global constant [OK]
Common Mistakes:
Declaring const val inside functions
Using var with const keyword
Using const without val
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.