Kotlin Interview Questions. Part 1

davthecoder
5 min readNov 26, 2022
Kotlin logo before a river
Photo by Marc Reichelt on Unsplash

If you are reading these lines is because you already know Kotlin and you are already or are expecting to face an interview in this field. First of all, congratulations, that means a company is giving you a chance to prove yourself, and along the following lines, I want to give you the hand and support you need to pass this critical step.

I do not want to bore you with long history about myself (I already wrote an article about that) you just need to know I did some interviews with different Android developers.

While not all the questions will be asked, it is important for you to know the concepts and meanings behind the questions and to know how to apply them, so with the answers, I did add a small example about what it is about.

Ready? then let’s start in this first part from the basics.

1. What is Kotlin?

Kotlin is a Statically typed programming language(*1) created by JetBrains with zero-cost Java interoperability and open-source under a license Apache 2 OSS. Currently targets Java Virtual Machine(JVM), Android and a Web Browser.

2. How to declare a Variable?

Variables in Kotlin can be declared by using var or val declaration.

val name = "David" //IMMUTABLE
var surname = "Cruz" //MUTABLE

3. What is the difference between var and val declarations?

The difference between them is val is used to declare immutable variables, while a variable with var can be re-assigned by another value of the same type. Example:

val name = "David"
name = "Roberto" //This is NOT permited

var age = 39
age = 40 //This is permited

4. Can you declare the Type of a variable beforehand?

Yes, after using the declaration and the name of your choice, you can use a colon mark following the type before the equals mark. Some examples are the following:

val name: String = "David" //String (chain)
val age: Int = 28 //Integer (Numbers)
val temp: Double = 37.1 //Double (decimal numbers)
val isMale: Boolean = true //Boolean (true or false)

5. How to convert a String into an Integer type? and an Integer into a String type?

With Kotlin, you can convert a valid number String using the inline function toInt(), and to an Integer into a String you can use toString() for example:

// Int to String
val numText: String = "909"
val num: Int = numText.toInt()

println(num) //Output: 909
println(num is Int) //Output: true

// String to Int
val age: Int = 34
val ageText: String = age.toString()

println(ageText) //Output: 34
println(ageText is String) //Output: true

6. What is a constant?

Constants in Kotlin are used to define a variable that has a constant value using the declaration const.

//Initials of Java Virtual Machine
const val INITIALS = "JVM"

7. What is the difference between val and const val declarations?

Both are immutable variables, but the difference is using val with the modifier const (const val) the properties are set in compile-time while using val on his own is set in runtime (const modifier cannot be used with var, neither can be used used in local variables).

8. What is the entry point in a Kotlin project?

The entry point is the function main with an array of Strings as an argument.

fun main(args: Array<String>) {
println("Hello World!")
}

// Output: Hello World!

9. What usability have the array of strings passed as a parameter in the entry point?

args: Array<String> passed as a parameter in the entry point is used to retrieve the command line arguments.

10. What is Null Safety in Kotlin?

One of the biggest advantages of Kotlin is Null Safety which is an approach to prevent dreaded Null Pointer Exceptions by using nullable types,
e.g.: String?, Int?, Double?, Boolean?, Char?, …

var age: Int? = 45 // Nullable
age = null //This IS permited

var temp: Double = 40.1
temp = null //This is not permited

These types can be used as wrapper types to hold null values. If on retrieving the value, the value is null, we can choose to ignore it and use a different value instead by using an Elvis operator (?:)

var name: String? = "David"
println(name ?: "No name")
//Output: David

name = null
println(name ?: "No name")
//Output: No name

11. Are variables Nullable by default in Kotlin?

No, if you declare a variable without specifying the type, the variable will be set to the type detected but non-nullable. Variables with nullable type must be declared beforehand.

var name = "David" //Similar as var name: String = "david"
name = null //This IS NOT permited :(

var name: String? = "David"
name = null //This IS Permited :)

12. What is an Elvis Operator?

Elvis operator is used to return a non-null value in the case scenario where the first or left value is null and is declared by a question mark and a colon (?:) e.g.:

val firstName: String? = null
val secondName: String? = null
var thirdName: String? = "David"
pintln(firstName ?: secondName ?: thirdName)
//Output: David

Explanation: As the first and second values are null it jumps into the third value printing in the console output the word “David”

Fact: You can concatenate all Elvis operations as you like, however, it is intended to use it only in case the first value is null to give a non-null default value.

13. Kotlin has interoperability with Java. Does Kotlin allow us to use primitive types?

No, we cannot use or declare primitive types at the language level, as in Kotlin, all are Objects. However, the JVM bytecode where it is compiled has them.

14. For what are the double exclamation marks !! used?

The double exclamation marks (!!) are used to force unwrap the nullable type to get the value, and this is something to be used with caution because if the value is null, it will crash in the runtime.

var city: String? = "London"
println(city!!)
//Output: London

city = null
print(city!!)
//Output: Exception in thread "main" java.lang.NullPointerException

15. How are functions declared?

functions in Kotlin are declared by the keyword fun, and the function can return or not a value. If the function returns a value, that type must be declared after the parenthesis and before the curly brackets with a colon.

In Kotlin, if the function does not returns a value is considered that this function returns a Unit type. However, adding a Unit as a return value is redundant.

fun sayHello(paramOne: String, paramTwo: String): String {
return "Hello $paramOne $paramTwo"
}
println(sayHello("David", "Cruz"))
//Output: "Hello David Cruz"

// Function that return Unit (Not Return value)
fun nameOfFunction(paramOne: String, paramTwo: String) {
println(sayHello("David", "Cruz"))
}
println(nameOfFunction("David", "Cruz"))
//Output: "Hello David Cruz"


/* FOR REFERENCE ONLY
fun nameOfFunction(paramOne: String, paramTwo: String): Unit {
println(sayHello("David", "Cruz"))
}
*/

This is the end of the first part of the Kotlin Interview Questions series.

If you found these 15 questions easy, that is a good sign; on every part, the complexity will increase, so keep an eye on the next episode :)

Happy Interview!

Let’s Keep in touch:
davthecoder Web: https://www.davthecoder.com
Medium Blog: https://davthecoder.medium.com/
Twitch: https://www.twitch.tv/davthecoder
GitHub: https://github.com/davthecodercom
Instagram: https://www.instagram.com/davthecoder
Twitter: https://twitter.com/davthecoder
Youtube: https://youtube.com/@davthecoder
Support me on Patreon! https://www.patreon.com/davthecoder

(*1) A statically-typed language is a language where variable types are known at compile time.

--

--