WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kotlin · Intermediate · question 26 of 100

Explain the difference between the also and let scope functions. Provide examples for each.?

📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

Kotlin provides several scope functions, including ‘also‘ and ‘let‘, that allow us to execute a block of code on an object in a certain context. In this answer, we will explain the difference between ‘also‘ and ‘let‘ and provide examples for each.

The ‘also‘ function executes a block of code on an object and then returns the object. It is useful for performing some side effects on the object, such as logging, printing or modifying its properties. Here is an example that demonstrates the use of ‘also‘:

val numberList = mutableListOf<Int>()

numberList.also {
    println("Populating the list")
}.apply {
    add(1)
    add(2)
    add(3)
}

println(numberList)

In this example, we create an empty mutable list ‘numberList‘. We then call ‘also‘ on it and pass a block of code as an argument. Inside the block, we print a message to the console before the list is populated. The ‘also‘ function then returns the object, which is still ‘numberList‘.

We then chain the ‘apply‘ function to ‘also‘. This function applies a block of code to the object and returns it. Inside the block, we add three integers to the list.

Finally, we print the list to the console, which should contain the values ‘1‘, ‘2‘, and ‘3‘. As we can see, the ‘also‘ function has allowed us to perform a side effect (printing a message) without modifying the list.

The ‘let‘ function, on the other hand, executes a block of code on an object and returns the result of the block. It is useful for transforming an object into another type or performing some computation based on its properties. Here is an example that demonstrates the use of ‘let‘:

val message = "Hello, World!"

val length = message.let {
    println("Calculating the length of the message")
    it.length
}

println("The length of the message is $length")

In this example, we create a string ‘message‘ that contains a greeting. We then call ‘let‘ on it and pass a block of code as an argument. Inside the block, we print a message to the console before returning the length of the string.

We then assign the result to a variable ‘length‘ and print it to the console along with a message that confirms it is the length of the original string. As we can see, the ‘let‘ function has allowed us to transform the string into another type without modifying it.

To summarize, the ‘also‘ and ‘let‘ functions are two of several scope functions in Kotlin that allow you to execute a block of code on an object in a certain context. ‘also‘ is useful for performing side effects on the object, while ‘let‘ is useful for transforming it into another type or performing computations based on its properties.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kotlin interview — then scores it.
📞 Practice Kotlin — free 15 min
📕 Buy this interview preparation book: 100 Kotlin questions & answers — PDF + EPUB for $5

All 100 Kotlin questions · All topics