Bird
Raised Fist0
Kotlinprogramming~10 mins

Constant values with const val in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a constant integer named MAX_COUNT with value 100.

Kotlin
const val MAX_COUNT: Int = [1]
Drag options to blanks, or click blank then click option'
A"100"
B100
CMAX
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers, which makes them strings.
Trying to assign a variable name instead of a value.
2fill in blank
medium

Complete the code to declare a constant string named APP_NAME with value "MyApp".

Kotlin
const val APP_NAME: String = [1]
Drag options to blanks, or click blank then click option'
AMy_App
BMyApp
C'MyApp'
D"MyApp"
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Not using quotes at all for string values.
3fill in blank
hard

Fix the error in the constant declaration for PI with value 3.14.

Kotlin
const val PI: Double = [1]
Drag options to blanks, or click blank then click option'
A3.14
B"3.14"
C3,14
Dpi
Attempts:
3 left
💡 Hint
Common Mistakes
Using a comma instead of a dot for decimals.
Putting the number inside quotes, making it a string.
4fill in blank
hard

Fill both blanks to declare two constants: MAX_SPEED as 120 and MIN_SPEED as 0.

Kotlin
const val MAX_SPEED: Int = [1]
const val MIN_SPEED: Int = [2]
Drag options to blanks, or click blank then click option'
A120
B100
C0
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the values between MAX_SPEED and MIN_SPEED.
Using incorrect numbers like 100 or -1.
5fill in blank
hard

Fill all three blanks to declare constants: PI as 3.1415, E as 2.718, and GREETING as "Hello".

Kotlin
const val PI: Double = [1]
const val E: Double = [2]
const val GREETING: String = [3]
Drag options to blanks, or click blank then click option'
A3.1415
B2.718
C"Hello"
D"Hi"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings for numeric constants.
Using wrong string values for GREETING.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To declare a constant value that cannot change during program execution -> Option A
  4. 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

  1. Step 1: Recall the correct syntax for constants

    In Kotlin, constants are declared using const val followed by the name and value.
  2. 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.
  3. Final Answer:

    const val MAX_COUNT = 100 -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Value of PI: 3.14 -> Option C
  4. 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

  1. Step 1: Understand const val immutability

    const val declares a constant that cannot be changed after initialization.
  2. Step 2: Analyze the reassignment

    The code tries to assign a new value to greeting which is a constant. This causes a compilation error.
  3. Final Answer:

    Cannot reassign a value to a const val -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Declare const val APP_NAME = "MyApp" at the top level or inside an object -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard