w3resource

Kotlin program: Find out your Kotlin version


Write a Kotlin program to find out what version of Kotlin you are using.


Pre-Knowledge (Before you start!)

  • Basic Kotlin Syntax: Familiarity with writing and running Kotlin programs.
  • Kotlin Functions: Understanding the structure of the main() function, which serves as the entry point of a Kotlin program.
  • Variables in Kotlin: Knowledge of declaring variables using val for immutable values.
  • KotlinVersion Class: Awareness of the KotlinVersion.CURRENT property, which provides the current version of Kotlin being used.
  • String Templates: Understanding how to use string templates (e.g., ${variable}) to embed variable values within strings.
  • Printing Output: Ability to use the println() function to display output on the screen.

Hints (Try before looking at the solution!)

  • Use the main() Function: Start by defining the main() function, which is the starting point of any Kotlin program.
  • Access Kotlin Version: Use the KotlinVersion.CURRENT property to retrieve the current Kotlin version and store it in a variable.
    • Example: val kotlinVersion = KotlinVersion.CURRENT.
  • Print the Version: Use the println() function to display the Kotlin version. Use a string template to include the version in the output message.
    • Example: println("Kotlin Version: ${kotlinVersion}").
  • Run the Program: Execute the program to see the Kotlin version displayed on the screen.
  • Common Errors to Avoid:
    • Forgetting to use string templates (${}) when embedding the variable in the output string.
    • Misplacing parentheses or quotes, leading to syntax errors.

Sample Solution:

Kotlin Code:

fun main() {
    val kotlinVersion = KotlinVersion.CURRENT
    println("Kotlin Version: ${kotlinVersion}")
}

Sample Output:

Kotlin Version: 1.8.21

Note:

When you run this program, it will output the version of Kotlin you are using, such as:
Kotlin Version: 1.5.30

The actual version number may vary depending on your Kotlin installation.

Kotlin Editor:


Previous: Print 'Hello' and name on separate lines.
Next: Display current date and time.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.