📖 Scenario: You are working on a Kotlin program that manages a list of book titles. You want to practice using Kotlin's scope functions and lambdas to manipulate and display this list in a clean and readable way.
🎯 Goal: Build a Kotlin program that creates a list of book titles, uses a scope function to filter and transform the list, and then prints the final result using a lambda expression.
📋 What You'll Learn
Create a list of book titles with exact values
Use a scope function to filter and transform the list
Use a lambda expression to print each book title
Follow exact variable names and function calls as instructed
💡 Why This Matters
🌍 Real World
Scope functions and lambdas help write clean and readable Kotlin code when working with collections and objects.
💼 Career
Understanding scope functions and lambdas is essential for Kotlin developers, especially in Android app development and backend services.
Progress0 / 4 steps
1
Create the list of book titles
Create a val called books and assign it a list containing these exact strings: "Kotlin Basics", "Advanced Kotlin", "Kotlin Coroutines", "Android Development".
Kotlin
Hint
Use listOf() to create the list with the exact book titles.
2
Add a filter threshold variable
Create a val called minLength and set it to 15. This will be used to filter book titles longer than this length.
Kotlin
Hint
Just create a simple integer variable named minLength with value 15.
3
Use a scope function to filter and transform
Use the books list and call run on it. Inside the run lambda, filter the list to keep only titles with length greater than minLength, then map each title to uppercase. Assign the result to a val called filteredBooks.
Kotlin
Hint
Use run on books, then inside the lambda use filter and map with lambdas.
4
Print the filtered book titles
Use filteredBooks.forEach with a lambda that prints each book title exactly as it is.
Kotlin
Hint
Use forEach on filteredBooks and print each item inside the lambda.
Practice
(1/5)
1. Which Kotlin scope function returns the object it was called on after executing the given lambda?
easy
A. run
B. also
C. let
D. apply
Solution
Step 1: Understand scope function behavior
The also function executes the lambda with the object as it and returns the original object.
Step 2: Compare with other functions
let returns the lambda result, run returns lambda result, apply returns the object but uses this inside lambda.
Final Answer:
also -> Option B
Quick Check:
Returns original object after lambda = also [OK]
Hint: Remember: also returns object, let returns lambda result [OK]
Common Mistakes:
Confusing 'also' with 'let' return values
Mixing 'apply' and 'also' usage
Assuming 'run' returns the object
2. Which of the following is the correct syntax to use the let scope function on a nullable variable name?
easy
A. name?.let { println(it) }
B. name.let { println(it) }
C. let(name) { println(it) }
D. name?.let println(it)
Solution
Step 1: Check nullable safe call usage
To safely call let on a nullable variable, use the safe call operator ?..
Step 2: Validate lambda syntax
The lambda must be enclosed in braces {} and use it as the implicit parameter.
listOf creates an immutable list; calling add is not allowed.
Step 2: Understand scope function effect
also runs the lambda but does not change immutability; trying to add causes a runtime error.
Final Answer:
Cannot call add on immutable list -> Option C
Quick Check:
Immutable list blocks add() method = Cannot call add on immutable list [OK]
Hint: listOf creates immutable list; use mutableListOf to add [OK]
Common Mistakes:
Assuming also changes list mutability
Ignoring immutable list restrictions
Thinking lambda syntax is wrong
5. Given a list of nullable integers val nums = listOf(1, null, 3, null, 5), which code snippet correctly uses scope functions and lambdas to create a list of doubled non-null values?
hard
A. nums.filterNotNull().map { it * 2 }
B. nums.map { it * 2 }.filterNotNull()
C. nums.also { it.map { it * 2 } }
D. nums.run { map { it!! * 2 } }
Solution
Step 1: Remove nulls before mapping
Use filterNotNull() to exclude null values safely.
Step 2: Map remaining values to double
Apply map { it * 2 } on non-null list to double each integer.
Final Answer:
nums.filterNotNull().map { it * 2 } -> Option A
Quick Check:
Filter nulls then map doubling = nums.filterNotNull().map { it * 2 } [OK]
Hint: Filter nulls first, then map to avoid null errors [OK]
Common Mistakes:
Mapping before filtering nulls causes null results
Using unsafe !! operator risking exceptions
Misusing also or run without returning correct list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.