Skip to content

Keel

EnglishEestiEspañolไทย

Tekstide kirjutamisel ja parandamisel kasutasin tehisaru abi, seega võib esineda kirja- ja muid vigu. Kui leiate mõne, andke palun teada: [email protected]

Kotlin Interview Questions. Part 1

Kotlin Interview Questions. Part 1

Karjäär ja juhtimine

Autor: David Cruz Anaya, mobiili ja rakendusliku tehisaru insener

Uuendatud 25. jaanuar 20264 min lugemist

Android · Mobile Development · Kotlin · Career · Interview Prep


Kotlin logo before a river

These are 15 beginner Kotlin questions, the first part of the series, starting from the basics. I have interviewed Android developers. An interview will not ask all of them. Each answer below includes a short example.

1. What is Kotlin?

Kotlin is a statically typed programming language created by JetBrains, with zero-cost Java interoperability, and open source under the Apache 2 OSS license. It targets the Java Virtual Machine (JVM), Android, and a web browser.

2. How to declare a Variable?

Variables in Kotlin are declared with var or val.

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

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

The difference is that val is immutable, while var can be reassigned to another value of the same type. Example:

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

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

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?

A valid number string converts with toInt(). An integer converts to a string with 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. const val is set at compile time. val on its own is set at runtime. const cannot be used with var, and it cannot be used on a local variable.

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?

Null safety is how Kotlin avoids null pointer exceptions. A type is nullable only when it is marked, for example String?, Int?, Double?, Boolean?, Char?.

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

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

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 permitted :(

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

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 written ?:. For example:

val firstName: String? = null
val secondName: String? = null
var thirdName: String? = "David"
println(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 (!!) force-unwrap a nullable value. Use them with caution: if the value is null, the program crashes at 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, a function that does not return a value has the type Unit. Writing Unit as the return type 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

Jaga: