Bird
Raised Fist0
Kotlinprogramming~5 mins

Also function behavior and use cases in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the also function in Kotlin?
The also function lets you perform additional actions on an object without changing it. It returns the original object after running the given block.
Click to reveal answer
intermediate
How does also differ from apply in Kotlin?
also passes the object as it and returns the original object, mainly for side effects. apply uses this and is often used to configure an object.
Click to reveal answer
beginner
Show a simple example of using also to log a value during a chain of calls.
val result = "Hello".also { println("Logging: $it") }.uppercase() // Prints: Logging: Hello // result = "HELLO"
Click to reveal answer
beginner
When is it useful to use also in Kotlin?
Use also when you want to do something extra with an object (like logging, debugging, or validation) without changing the object or breaking a chain of calls.
Click to reveal answer
beginner
What does also return after executing its block?
also always returns the original object it was called on, regardless of what happens inside the block.
Click to reveal answer
What does the also function return in Kotlin?
AThe original object it was called on
BThe result of the block inside <code>also</code>
CA new object created inside the block
DNull
In the also function, how is the object passed to the block?
AAs <code>this</code>
BAs <code>it</code>
CAs a parameter named <code>obj</code>
DIt is not passed
Which of these is a common use case for also?
AChanging the object’s properties
BReturning a different object
CPerforming side effects like logging
DCreating a new object
How does also help in method chaining?
AIt breaks the chain by returning null
BIt modifies the object and returns a new one
CIt stops the chain and returns a Boolean
DIt allows side effects without breaking the chain
Which keyword is used inside the also block to refer to the object?
Ait
Bself
Cthis
Dobj
Explain how the also function works in Kotlin and give an example of when you might use it.
Think about doing something extra without changing the object.
You got /4 concepts.
    Compare also and apply in Kotlin. When would you choose one over the other?
    Focus on how the object is referenced and what you want to do with it.
    You got /4 concepts.

      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

      1. Step 1: Understand the purpose of also

        The also function is used to run some extra code on an object without changing it.
      2. Step 2: Check the return value of also

        It returns the original object, not a transformed copy.
      3. Final Answer:

        Runs extra code on an object and returns the original object -> Option A
      4. 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

      1. Step 1: Recall the syntax of also

        The also function is called on an object with a lambda block: object.also { ... }.
      2. 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.
      3. Final Answer:

        num.also { println(it) } -> Option D
      4. 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

      1. 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.
      2. Step 2: Check the returned value

        also returns the original list, now modified with 4 added.
      3. Final Answer:

        [1, 2, 3, 4] -> Option C
      4. 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

      1. Step 1: Analyze the lambda parameter 'it'

        In also, 'it' is a val (read-only), so you cannot reassign it inside the lambda.
      2. Step 2: Check the code for reassignment

        The code tries to do it = it + " World", which is illegal and causes a compilation error.
      3. Final Answer:

        Cannot reassign 'it' inside also lambda -> Option B
      4. 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

      1. Step 1: Understand the goal

        We want to log each name but keep the original list unchanged and returned.
      2. 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.
      3. Final Answer:

        users.also { it.forEach { name -> println(name) } } -> Option A
      4. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard