article banner (priority)

Kotlin Essentials now with exercises

I must say, I am happy with the great feedback I received for the book Kotlin Essentials. In the end, it is a result of years of practice in teaching Kotlin. However, there was one request I also often received: Readers commented that this book would benefit from practical exercises after each chapter. They argued that such exercises would help them to better understand and practice the presented concepts. I agree with this argument, and I am happy to announce that the book Kotlin Essentials now has exercises after each chapter.

About exercises

I did my best for each exercise to base only on what was already covered in the book, and to be and appropriate level for reader. Some exercises might seem easy, like the one after the section about basic values and operations, that ask you to predict what will be printed as a result of specific operations.

fun main() { println(1 + 2 * 3) // ? println(10 % 3) // ? println(-1 % 3) // ? println(8.8 / 4) // ? println(10 / 3) // ? println(11.toFloat()) // ? println(10.10.toInt()) // ? // ... }

Others are very practical, like the first exercise that makes reader follow a set of steps to create and run the first program in Kotlin. Finally, some exercises might be demanding, like the exercise that ends the section about generics, and asks to implement Stack.

The majority of exercises have starting code, sample usages, and unit tests. I located them in the project kotlin-exercises, that can be cloned and used as a starting point for each exercise. Solutions are presented on the last pages of the book.

If you are teaching Kotlin at a public school or a university, feel free to use my exercises on your course.

Sample exercise: Pretty time display

Implement the secondsToPrettyTime function that takes an integer number of seconds and returns a string representation of the time in the following format: "X h Y min Z sec", where X, Y, and Z are the number of hours, minutes, and seconds respectively. If a value is zero, it should be omitted from the result. If the input is negative, return "Invalid input".

fun secondsToPrettyTime(seconds: Int): String { return "" }

Example usage:

fun main() { println(secondsToPrettyTime(-1)) // Invalid input println(secondsToPrettyTime(0)) // println(secondsToPrettyTime(45)) // 45 sec println(secondsToPrettyTime(60)) // 1 min println(secondsToPrettyTime(150)) // 2 min 30 sec println(secondsToPrettyTime(1410)) // 23 min 30 sec println(secondsToPrettyTime(60 * 60)) // 1 h println(secondsToPrettyTime(3665)) // 1 h 1 min 5 sec }

Starting code, unit tests and example usage can be found in the kotlin-exercises project on GitHub in the file essentials/conditions/PrettyTime.kt. You can clone this project and solve this exercise locally.

Hint: You can use trim function on a string to remove leading and trailing whitespace characters.


I hope those exercises will help readers' practice present a topic, and as a result, understand it on a deeper level. I believe this is a big step for making this book better.