Bird
Raised Fist0
Kotlinprogramming~10 mins

Apply function behavior and use cases in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the apply function to initialize the object.

Kotlin
val person = Person().[1] {
    name = "Alice"
    age = 30
}
Drag options to blanks, or click blank then click option'
Aapply
Balso
Crun
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' instead of 'apply' which returns the last expression, not the object.
Using 'run' which returns the lambda result, not the object.
Using 'also' which uses 'it' as context, not 'this'.
2fill in blank
medium

Complete the code to print the object's properties using apply.

Kotlin
val car = Car().[1] {
    brand = "Toyota"
    year = 2020
    println("Brand: $brand, Year: $year")
}
Drag options to blanks, or click blank then click option'
Aapply
Bwith
Calso
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'also' which uses 'it' and is less convenient for property access.
Using 'let' which returns the lambda result, not the object.
Using 'with' which is not called as an extension function.
3fill in blank
hard

Fix the error in the code by choosing the correct function to initialize and return the object.

Kotlin
val book = Book().[1] {
    title = "Kotlin Guide"
    pages = 250
}
println(book.title)
Drag options to blanks, or click blank then click option'
Alet
Bapply
Crun
Dalso
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' or 'run' which return the lambda result, not the object.
Using 'also' which uses 'it' and is less convenient here.
4fill in blank
hard

Fill both blanks to create a mutable list and add elements using apply.

Kotlin
val numbers = mutableListOf<Int>()[1] {
    add(1)
    add(2)
    add(3)
}[2]
println(numbers)
Drag options to blanks, or click blank then click option'
Aapply
Blet
Calso
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' or 'run' which return the lambda result, not the list.
Using 'also' which uses 'it' and is less convenient for adding elements.
5fill in blank
hard

Fill all three blanks to create a data class instance, configure it with apply, and print its properties.

Kotlin
data class User(var username: String = "", var email: String = "")

val user = User()[1] {
    username = "john_doe"
    email = "john@example.com"
}

println("Username: ${user.[2], Email: ${user.[3]")
Drag options to blanks, or click blank then click option'
Aapply
Busername
Cemail
Dlet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' instead of 'apply' which returns the lambda result.
Misspelling property names in the print statement.
Using 'also' which uses 'it' and is less convenient here.

Practice

(1/5)
1. What does the apply function do in Kotlin?
easy
A. Runs code on an object and returns the same object
B. Creates a new object and returns it
C. Runs code and returns the last expression result
D. Runs code without returning anything

Solution

  1. Step 1: Understand the purpose of apply

    The apply function executes a block of code on the object it is called on.
  2. Step 2: Identify the return value of apply

    It always returns the original object after applying the block, not a new value.
  3. Final Answer:

    Runs code on an object and returns the same object -> Option A
  4. Quick Check:

    apply returns the object itself [OK]
Hint: Remember: apply returns the original object after changes [OK]
Common Mistakes:
  • Thinking apply returns the last expression result
  • Confusing apply with let or run
  • Assuming apply creates a new object
2. Which of the following is the correct syntax to use apply on an object person to set its name property to "Alice"?
easy
A. person.apply { name = "Alice" }
B. apply(person) { name = "Alice" }
C. person.apply(name = "Alice")
D. person.apply { setName("Alice") }

Solution

  1. Step 1: Identify the correct lambda syntax for apply

    apply takes a lambda with receiver where this is the object, so properties can be set directly.
  2. Step 2: Check each option's syntax

    person.apply { name = "Alice" } correctly uses person.apply { name = "Alice" }. apply(person) { name = "Alice" } is invalid syntax. person.apply(name = "Alice") wrongly passes a parameter. person.apply { setName("Alice") } assumes a setter method which may not exist.
  3. Final Answer:

    person.apply { name = "Alice" } -> Option A
  4. Quick Check:

    Use object.apply { property = value } syntax [OK]
Hint: Use curly braces with apply to set properties inside [OK]
Common Mistakes:
  • Passing parameters directly to apply
  • Using parentheses instead of curly braces
  • Calling non-existent setter methods
3. What is the output of this Kotlin code?
val list = mutableListOf<Int>().apply {
    add(1)
    add(2)
    add(3)
}
println(list)
medium
A. Compilation error
B. []
C. null
D. [1, 2, 3]

Solution

  1. Step 1: Understand what apply does on the list

    The apply block adds 1, 2, and 3 to the mutable list.
  2. Step 2: Check the returned value of apply

    apply returns the original list after adding the elements, so list contains [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option D
  4. Quick Check:

    apply adds elements and returns the list [OK]
Hint: apply returns the object after running code inside [OK]
Common Mistakes:
  • Expecting apply to return last added element
  • Thinking list is empty after apply
  • Confusing apply with run or let
4. Identify the error in this Kotlin code using apply:
val map = mutableMapOf()
map.apply {
    put("a", 1)
    put("b", 2)
}
println(map.get("c"))
medium
A. Syntax error in apply block
B. Compilation error due to missing return
C. No error; output is null
D. Runtime error: key "c" not found

Solution

  1. Step 1: Check the apply block usage

    The apply block correctly adds two entries to the map.
  2. Step 2: Understand the map.get("c") call

    Since key "c" does not exist, get returns null, which is printed without error.
  3. Final Answer:

    No error; output is null -> Option C
  4. Quick Check:

    Accessing missing key returns null, no error [OK]
Hint: Missing keys in map.get return null, no crash [OK]
Common Mistakes:
  • Expecting runtime error for missing key
  • Thinking apply must return a value explicitly
  • Confusing map access syntax
5. You want to create and configure a StringBuilder in Kotlin to build the string "Hello World!" using apply. Which code snippet correctly does this?
hard
A. val result = StringBuilder().apply { append("Hello"); append(" World!") }
B. val result = StringBuilder().apply { append("Hello"); append(" World!") }.toString()
C. val result = StringBuilder().apply { append("Hello World!") }
D. val result = StringBuilder().apply { append("Hello"); append(" World!") }.toString

Solution

  1. Step 1: Understand apply returns the object itself

    The apply block configures the StringBuilder, but to get the string, toString() must be called after.
  2. Step 2: Check each option's correctness

    val result = StringBuilder().apply { append("Hello"); append(" World!") }.toString() calls toString() after apply, returning the built string. val result = StringBuilder().apply { append("Hello World!") } returns the StringBuilder object, not the string. val result = StringBuilder().apply { append("Hello"); append(" World!") } misses toString(). val result = StringBuilder().apply { append("Hello"); append(" World!") }.toString misses parentheses on toString, causing error.
  3. Final Answer:

    val result = StringBuilder().apply { append("Hello"); append(" World!") }.toString() -> Option B
  4. Quick Check:

    Call toString() after apply to get string [OK]
Hint: Call toString() after apply to get final string [OK]
Common Mistakes:
  • Forgetting to call toString() after apply
  • Missing parentheses on toString
  • Expecting apply to return string automatically
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