Exercises for the chapter “Data types in Kotlin”

Exercise #1

Answer the following questions:

Does Kotlin have dynamic or static typing?
Kotlin is a statically typed language.
What does static typing mean and what advantages does it offer?
Static typing means that each variable is assigned a specific data type at compile time and cannot be changed at runtime.
What two parameters are determined by data type in Kotlin?
The data type determines how much memory must be reserved for an object (e.g. variable) and which ones operations can be applied to this object.

Exercise #2 – Integer Data Types

a). Initialize for each data type Byte, Short, Int, Long a variable and give the values ​​using the function println() in console.

Solution to exercise # 2 a).

fun main() {
   val byteVariable: Byte = -1
   val shortVariable: Short = 99
   val intVariable: Int = 15000
   val longVariable: Long = 800000
   println(byteVariable)
   println(shortVariable)
   println(intVariable)
   println(longVariable)
}

b). The code below contains three errors. Find these and correct them.

Solution to exercise # 2 b).

fun main() {
    val byteVariable: Byte = 130        // The value 130 is outside the permitted range of -128 to 127
    val shortVariable: Short = -33000  // The value -33000 is outside the permitted range -32,768 to 32,767
    val intVariable: Int = 25120
    val longVariable: Long = "40500"   // It is not possible to assign a string to the Long data type
    println(byteVariable)
    println(shortVariable)
    println(intVariable)
    println(longVariable)
}

c). Initialize for each data type UByte, UShort, UInt, ULong a variable and print the values ​​in console.

Solution to exercise # 2 c).

fun main() {
    val uByteVariable: UByte = 20U
    val uShortVariable: UShort = 176U
    val uIntVariable: UInt = 99000U
    val uLongVariable: ULong = 4520004880U
    println(uByteVariable)
    println(uShortVariable)
    println(uIntVariable)
    println(uLongVariable)
}



Exercise #3 – Data Types for floating point numbers

The following code has two errors. Find these and correct them.

Solution to exercise # 3

fun main() {
    val floatVariable1: Float = 5.18f // A suffix "f" or "F" must be appended to a float number to tell the compiler that it is a float number and not a double number.
    val floatVariable2: Double = -4.123 // The suffix "F" is unnecessary at this point. Value assignment for the Double data type occurs without a suffix.
    println(floatVariable1)
    println(floatVariable2)
}



Exercise #4 – Boolean logical data type

Create two variables with logical values ​​true and false, print the values ​​in console.

Solution to exercise # 4

fun main() {
    val isEarthCube: Boolean = false
    val isSunHot: Boolean = true
    println(isEarthCube)    
    println(isSunHot) 
}



Exercise #5 – Type inference (type derivation)

Create six variables with different values ​​without specifying the data type. Output the values ​​to console. Explain which data types were determined by Kotlin in your variables.

Solution to exercise # 5

fun main() {
    val age = 100          // Integers are recognized as Int
    val name = "Arnold"    // In this case the data type String is identified
    val route = 1.2        // Any number with a decimal point is interpreted as the data type Double
    val sum = 160L         // To explicitly specify the Long data type, you should use the suffix “L”.
    val height = 5.40F     // To initialize a float type variable, one must specify the suffix "f" or "F" after the number
    val width = 50U        // Unsigned integer data types are specified with "u" or "U"
    println(age)
    println(name)
    println(route)
    println(height)
    println(width)
}


Exercise #6 – Any data type

  • Declare a variable with data type Any. First assign a string value to the variable. Output the value.
  • Then assign an int value and output it to console.
  • Finally, repeat the same thing with a double value.
Solution to exercise # 6

fun main() {
    var anyVariable: Any
    anyVariable = "London is the capital city of England and the United Kingdom"
    println(anyVariable)
    anyVariable = 100
    println(anyVariable)
    anyVariable = 1.9
    println(anyVariable)
}



Exercise #7 – String templates

Write a short story about a boy who particularly enjoys playing basketball. Declare the following given parameters as individual variables and incorporate them into your text (string templates):

  • First name: Tim
  • Last name: Johnson
  • Age: 19 years
  • Size: 182 cm
  • Hobby: basketball

Output the result to console.

Solution to exercise # 7

fun main() {
// I have prepared the following text in English as a solution:
   val name: String = "Tim"
   val surname: String = "Johnson"
   val age: Int = 19
   val height: Double = 1.82
   val hobby: String = "basketball"
   val finalText: String
   finalText = "Meet $name $surname, a $age-year-old boy who is passionate\nabout his hobby $hobby. Standing tall at $height meters,\nhe spends most of his free time practicing his skills on the court."
   println(finalText)
}


Leave a Comment

Your e-mail address will not be published. Required fields are marked with * marked