What does the forEach function do in Kotlin collections?
It runs a given block of code on each item in the collection, one by one.
Click to reveal answer
beginner
How do you write a forEach loop to print each element in a list called numbers?
numbers.forEach { number -> println(number) }
Click to reveal answer
intermediate
Can you modify elements inside a forEach loop in Kotlin?
No, forEach is used for actions like printing or logging. To change elements, use map or other transformation functions.
Click to reveal answer
intermediate
What is the difference between a for loop and forEach in Kotlin?
for is a language construct that can be used with any iterable, while forEach is a function that takes a lambda and runs it on each element.
Click to reveal answer
beginner
Write a forEach example that prints each string in a list with its length.
val words = listOf("apple", "banana", "cherry")
words.forEach { word -> println("$word has ${word.length} letters") }
Click to reveal answer
What type of argument does forEach take in Kotlin?
AA string value
BAn integer index
CA lambda function that takes each element
DA Boolean condition
✗ Incorrect
forEach takes a lambda function that is called for each element in the collection.
Which of these is a correct way to use forEach on a list items?
AforEach(items) { println(it) }
Bitems.forEach -> println(it)
Citems.forEach(println(it))
Ditems.forEach { println(it) }
✗ Incorrect
The correct syntax is calling forEach on the collection with a lambda: items.forEach { println(it) }.
Can forEach be used to change the elements of a mutable list directly?
ANo, it is for performing actions only
BYes, it modifies elements automatically
COnly if the list is immutable
DOnly with a special keyword
✗ Incorrect
forEach is for running code on each element, not for changing elements. Use map or direct indexing for changes.
What will this code print?
listOf(1, 2, 3).forEach { println(it * 2) }
A2 4 6
B1 2 3
CError
D6 4 2
✗ Incorrect
Each element is multiplied by 2 and printed: 2, 4, 6.
Which Kotlin collection function is best for transforming elements instead of just iterating?
AforEach
Bmap
Cfilter
Dreduce
✗ Incorrect
map creates a new collection by transforming each element.
Explain how to use forEach to print all elements in a Kotlin list.
Think about how you tell the list to do something with each item.
You got /3 concepts.
Describe the difference between forEach and a for loop in Kotlin.
Consider how each handles looping and control flow.
You got /4 concepts.
Practice
(1/5)
1. What does the forEach function do when used on a list in Kotlin?
easy
A. It runs a block of code for each item in the list.
B. It creates a new list with modified items.
C. It removes items from the list.
D. It sorts the list in ascending order.
Solution
Step 1: Understand the purpose of forEach
The forEach function is designed to execute a given block of code on every element in a collection.
Step 2: Compare options with forEach behavior
Only It runs a block of code for each item in the list. correctly describes this behavior. Options B, C, and D describe other functions like map, remove, and sort.
Final Answer:
It runs a block of code for each item in the list. -> Option A
Quick Check:
forEach = run code on each item [OK]
Hint: forEach runs code on every element, not changing the list [OK]
Common Mistakes:
Confusing forEach with map that creates new lists
Thinking forEach modifies the list itself
Assuming forEach sorts or filters the list
2. Which of the following is the correct syntax to print each element of a list numbers using forEach in Kotlin?
easy
A. numbers.forEach -> println(it)
B. forEach(numbers) { println(it) }
C. numbers.forEach { println(it) }
D. numbers.forEach(println(it))
Solution
Step 1: Recall the correct forEach syntax
In Kotlin, forEach is called on the collection with a lambda block: collection.forEach { item -> ... }. The shorthand uses it for the current item.
Step 2: Check each option's syntax
numbers.forEach { println(it) } matches the correct syntax. forEach(numbers) { println(it) } wrongly uses forEach as a function with the list as argument. numbers.forEach(println(it)) incorrectly calls println inside forEach parentheses. numbers.forEach -> println(it) uses an invalid arrow syntax.
3. What will be the output of the following Kotlin code?
val fruits = listOf("apple", "banana", "cherry")
fruits.forEach { println(it.length) }
medium
A. 5 6 6
B. 5
6
6
C. apple banana cherry
D. 3 6 6
Solution
Step 1: Understand the code behavior
The list fruits contains three strings: "apple", "banana", and "cherry". The forEach prints the length of each string.
Step 2: Calculate lengths and output format
"apple" has length 5, "banana" has length 6, "cherry" has length 6. Each println prints on a new line, so output is:
5
6
6
Final Answer:
5
6
6 -> Option B
Quick Check:
Print lengths with println = lines of 5,6,6 [OK]
Hint: println prints each item on a new line [OK]
Common Mistakes:
Assuming output is on one line separated by spaces
Confusing string length with number of vowels
Printing the strings instead of their lengths
4. Identify the error in this Kotlin code snippet:
val nums = listOf(1, 2, 3)
nums.forEach(i -> println(i))
medium
A. Calling println inside forEach is not allowed
B. Missing parentheses after forEach
C. Using val instead of var for list
D. Using arrow (->) instead of curly braces for lambda
Solution
Step 1: Check lambda syntax in Kotlin
Kotlin uses curly braces {} for lambdas, not arrow notation like -> outside braces. The arrow is used inside the lambda to separate parameters from body.
Step 2: Identify the incorrect usage
The code uses i -> println(i) directly after forEach without braces, which is invalid syntax. Correct form is nums.forEach { i -> println(i) }.
Final Answer:
Using arrow (->) instead of curly braces for lambda -> Option D
Quick Check:
Lambda needs braces {} around arrow -> [OK]
Hint: Use { param -> code } for lambdas, not (param -> code) [OK]
Common Mistakes:
Using arrow notation without braces
Confusing Kotlin lambda syntax with other languages
Thinking val vs var affects forEach usage
5. Given a list of integers nums, how can you use forEach to print only the even numbers?
hard
A. nums.forEach { if (it % 2 == 0) println(it) }
B. nums.forEach { println(it % 2 == 0) }
C. nums.forEach { if (it / 2 == 0) println(it) }
D. nums.forEach { println(it) if (it % 2 == 0) }
Solution
Step 1: Understand the condition for even numbers
An even number has zero remainder when divided by 2, so it % 2 == 0 checks if a number is even.
Step 2: Use if inside forEach to print only even numbers
nums.forEach { if (it % 2 == 0) println(it) } correctly uses an if statement inside the lambda to print only when the condition is true. Other options either print booleans or have incorrect syntax.
Final Answer:
nums.forEach { if (it % 2 == 0) println(it) } -> Option A
Quick Check:
Use if inside forEach to filter printing [OK]
Hint: Use if (it % 2 == 0) inside forEach to print evens [OK]
Common Mistakes:
Printing boolean results instead of numbers
Using division instead of modulo for even check
Placing if after println causing syntax error
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.