📖 Scenario: Imagine you are organizing a list of books and want to log some information while building the list. Kotlin's also function helps you do extra actions on an object without changing it.
🎯 Goal: You will create a list of book titles, use the also function to print each book as it is added, and finally print the full list.
📋 What You'll Learn
Create a mutable list of strings called books with initial titles
Create a variable newBook with a book title
Use the also function to print the newBook before adding it to books
Print the final books list
💡 Why This Matters
🌍 Real World
The <code>also</code> function is useful when you want to do extra work with an object, like logging or debugging, without changing the object itself.
💼 Career
Understanding <code>also</code> helps you write cleaner Kotlin code, which is valuable for Android development and backend Kotlin projects.
Progress0 / 4 steps
1
Create the initial list of books
Create a mutable list of strings called books with these exact titles: "Kotlin Basics", "Advanced Kotlin", and "Kotlin Coroutines".
Kotlin
Hint
Use mutableListOf to create a list you can add to later.
2
Create a new book title variable
Create a variable called newBook and set it to the string "Kotlin Flow".
Kotlin
Hint
Use val to create a read-only variable with the exact string.
3
Use also to print the new book before adding
Use the also function on newBook to print "Adding book: " followed by the book title, then add the book to the books list.
Kotlin
Hint
The also function lets you do something with the object (like print) and then returns it so you can add it to the list.
4
Print the final list of books
Write a println statement to print the books list.
Kotlin
Hint
Use println(books) to show the full list after adding the new book.
Practice
(1/5)
1. What does the also function do in Kotlin?
easy
A. Runs extra code on an object and returns the original object
B. Transforms the object and returns a new one
C. Stops the execution of the program
D. Creates a copy of the object
Solution
Step 1: Understand the purpose of also
The also function is used to run some extra code on an object without changing it.
Step 2: Check the return value of also
It returns the original object, not a transformed copy.
Final Answer:
Runs extra code on an object and returns the original object -> Option A
Quick Check:
also returns original object [OK]
Hint: Remember: also returns the original object unchanged [OK]
Common Mistakes:
Thinking it returns a transformed object
Confusing it with map or let
Assuming it stops program execution
2. Which of the following is the correct syntax to use also with a variable num in Kotlin?
easy
A. also(num) { println(it) }
B. num.also -> { println(it) }
C. num.also println(it)
D. num.also { println(it) }
Solution
Step 1: Recall the syntax of also
The also function is called on an object with a lambda block: object.also { ... }.
Step 2: Check each option for correct syntax
num.also { println(it) } uses the correct dot call and lambda syntax. Options A, C, and D have syntax errors or wrong usage.
Final Answer:
num.also { println(it) } -> Option D
Quick Check:
Correct also syntax uses dot and lambda [OK]
Hint: Use dot and curly braces for lambda with also [OK]
Common Mistakes:
Calling also as a standalone function
Missing curly braces for lambda
Using arrow without lambda properly
3. What is the output of this Kotlin code?
val list = mutableListOf(1, 2, 3)
val result = list.also { it.add(4) }
println(result)
medium
A. Compilation error
B. [1, 2, 3]
C. [1, 2, 3, 4]
D. Runtime exception
Solution
Step 1: Understand what also does here
The also block adds 4 to the mutable list. Since the list is mutable, it changes the original list.
Step 2: Check the returned value
also returns the original list, now modified with 4 added.
Final Answer:
[1, 2, 3, 4] -> Option C
Quick Check:
also returns original object after modification [OK]
Hint: also returns original object, so changes show in output [OK]
Common Mistakes:
Thinking also returns a new list
Ignoring that list is mutable
Expecting original list without 4
4. Identify the error in this Kotlin code using also:
val text = "Hello"
text.also { it = it + " World" }
println(text)
medium
A. println syntax is incorrect
B. Cannot reassign 'it' inside also lambda
C. also cannot be used with strings
D. Missing return statement in also
Solution
Step 1: Analyze the lambda parameter 'it'
In also, 'it' is a val (read-only), so you cannot reassign it inside the lambda.
Step 2: Check the code for reassignment
The code tries to do it = it + " World", which is illegal and causes a compilation error.
Final Answer:
Cannot reassign 'it' inside also lambda -> Option B
Quick Check:
Lambda parameter 'it' is read-only [OK]
Hint: Remember: 'it' in also lambda is read-only [OK]
Common Mistakes:
Trying to reassign 'it' inside lambda
Confusing also with let for transformations
Ignoring immutability of lambda parameters
5. Given a list of user names, how can you use also to log each name while returning the original list unchanged?
val users = listOf("Alice", "Bob", "Carol")
// Insert code here
println(users)
hard
A. users.also { it.forEach { name -> println(name) } }
B. users.map { println(it) }
C. users.let { it.forEach { println(it) } }
D. users.forEach { println(it) }
Solution
Step 1: Understand the goal
We want to log each name but keep the original list unchanged and returned.
Step 2: Check each option's behavior
users.also { it.forEach { name -> println(name) } } uses also to run logging side effects and returns the original list. Options B and C return transformed or Unit types, and D returns Unit.
Final Answer:
users.also { it.forEach { name -> println(name) } } -> Option A
Quick Check:
also runs side effects and returns original object [OK]
Hint: Use also for side effects without changing the object [OK]
Common Mistakes:
Using map which changes the list
Using let which returns last lambda result
Using forEach alone which returns Unit
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.