📖 Scenario: You are working on a Kotlin program that processes user information. You want to use the run function to execute a block of code with an object and get a result from it.
🎯 Goal: Learn how to use the Kotlin run function to execute code blocks and return results in a clean and readable way.
📋 What You'll Learn
Create a data class called User with properties name and age
Create an instance of User with specific values
Use the run function on the User instance to create a greeting message
Print the greeting message
💡 Why This Matters
🌍 Real World
The <code>run</code> function helps write clean and readable code when you want to perform operations on an object and get a result without creating extra variables.
💼 Career
Understanding Kotlin scope functions like <code>run</code> is important for writing concise and maintainable Android apps and backend services.
Progress0 / 4 steps
1
Create the User data class
Create a Kotlin data class called User with two properties: name of type String and age of type Int.
Kotlin
Hint
Use the data class keyword followed by the class name and properties inside parentheses.
2
Create a User instance
Create a variable called user and assign it a User object with name set to "Alice" and age set to 30.
Kotlin
Hint
Use the User constructor with named arguments to create the instance.
3
Use run function to create a greeting
Use the run function on the user variable to create a variable called greeting that contains the string "Hello, Alice! You are 30 years old.". Use this.name and this.age inside the run block.
Kotlin
Hint
Use user.run { ... } and return the greeting string inside the block.
4
Print the greeting message
Print the greeting variable using println.
Kotlin
Hint
Use println(greeting) to display the message.
Practice
(1/5)
1. What does the Kotlin run function do when called without an object?
easy
A. Creates a new thread to run the block asynchronously
B. Delays the execution of the block until explicitly called
C. Executes the given block and returns the last expression's value
D. Throws an exception if the block contains no return statement
Solution
Step 1: Understand run without receiver
When run is called without an object, it simply executes the block of code inside it.
Step 2: Identify the return value
The function returns the value of the last expression inside the block.
Final Answer:
Executes the given block and returns the last expression's value -> Option C
Quick Check:
run without object returns last expression [OK]
Hint: run returns last expression value in its block [OK]
Common Mistakes:
Thinking run creates a new thread
Assuming run delays execution
Believing run throws error without explicit return
2. Which of the following is the correct syntax to use run with an object in Kotlin?
easy
A. val result = run.obj { property + 1 }
B. val result = obj.run { this.property + 1 }
C. val result = run { obj.property + 1 }
D. val result = obj.run() { property + 1 }
Solution
Step 1: Identify correct run usage with receiver
The run function is called as an extension on the object: obj.run { ... }.
Step 2: Check syntax correctness
val result = obj.run { this.property + 1 } correctly uses obj.run { this.property + 1 } without extra parentheses or misplaced dots.
Final Answer:
val result = obj.run { this.property + 1 } -> Option B
Quick Check:
run called as extension function on object [OK]
Hint: run is called as extension: obj.run { ... } [OK]
Common Mistakes:
Using dot after run like run.obj
Adding empty parentheses after run
Calling run without object but expecting receiver
3. What is the output of this Kotlin code?
val result = run {
val x = 5
val y = 10
x + y
}
println(result)
medium
A. Compilation error
B. 510
C. null
D. 15
Solution
Step 1: Execute the run block
The block declares x = 5 and y = 10, then returns x + y which is 15.
Step 2: Print the result
The variable result holds 15, so println(result) outputs 15.
Final Answer:
15 -> Option D
Quick Check:
run returns last expression value 15 [OK]
Hint: run returns last expression value inside block [OK]
Common Mistakes:
Concatenating numbers as strings
Expecting null because no explicit return
Thinking run causes compilation error
4. Identify the error in this Kotlin code using run:
val text = "Hello"
val length = text.run {
val len = length
}
println(length)
medium
A. The run block does not return a value, so length is assigned Unit
B. The variable len is not accessible outside run block
C. The property length is undefined for String
D. The run function cannot be called on a String
Solution
Step 1: Analyze run block return
The run block declares val len = length but does not return any expression explicitly, so it returns Unit.
Step 2: Check assignment to length variable
Variable length is assigned the result of run, which is Unit, not the integer length of the string.
Final Answer:
The run block does not return a value, so length is assigned Unit -> Option A
Quick Check:
run returns last expression; no explicit return means Unit [OK]
Hint: run returns last expression; no explicit return means Unit [OK]
Common Mistakes:
Assuming len is returned automatically
Confusing property length with variable
Thinking run cannot be called on String
5. Given a list of nullable integers val numbers = listOf(1, null, 3, null, 5), how can you use run to compute the sum of non-null values safely in Kotlin?
hard
A. val sum = numbers.run { filterNotNull().sum() }
B. val sum = run(numbers) { filterNotNull().sum() }
C. val sum = numbers.run { sum() }
D. val sum = run { filterNotNull().sum() }
Solution
Step 1: Use run as extension on list
Calling numbers.run { ... } lets you access list functions directly inside the block.
Step 2: Filter non-null and sum
Inside the block, filterNotNull() removes nulls, then sum() adds remaining numbers.
Final Answer:
val sum = numbers.run { filterNotNull().sum() } -> Option A
Quick Check:
run with receiver simplifies code, returns last expression [OK]
Hint: Use run as extension to chain calls on object [OK]
Common Mistakes:
Passing list as argument to run incorrectly
Calling sum() without filtering nulls
Using run without receiver and accessing list members directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.