The fold function starts with an initial value and combines it with elements of a collection one by one using a given operation, producing a single result.
Click to reveal answer
beginner
How is reduce different from fold in Kotlin?
reduce does not take an initial value; it uses the first element of the collection as the starting point and combines the rest. fold requires an explicit initial value.
Click to reveal answer
intermediate
What happens if you call reduce on an empty list in Kotlin?
Calling reduce on an empty list throws a NoSuchElementException because there is no first element to start the operation.
Click to reveal answer
beginner
Explain the role of the lambda function in fold and reduce.
The lambda function defines how to combine the current accumulated value with the next element. It takes two parameters: the accumulator and the current element, and returns the new accumulator.
Click to reveal answer
beginner
Give a simple example of using fold to sum a list of numbers starting from 10.
Example: val numbers = listOf(1, 2, 3) val sum = numbers.fold(10) { acc, num -> acc + num } This results in sum = 16 because 10 + 1 + 2 + 3 = 16.
Click to reveal answer
What initial value does reduce use when combining elements?
ANull
BZero
CThe first element of the collection
DAn explicit initial value provided by the user
✗ Incorrect
reduce uses the first element of the collection as the starting point.
Which function requires an explicit initial value: fold or reduce?
Afold
Breduce
CBoth
DNeither
✗ Incorrect
fold requires an initial value; reduce does not.
What exception is thrown if reduce is called on an empty list?
ANullPointerException
BIndexOutOfBoundsException
CIllegalArgumentException
DNoSuchElementException
✗ Incorrect
Calling reduce on an empty list throws NoSuchElementException.
In the lambda for fold, what do the two parameters represent?
AThe index and the element
BThe accumulator and the current element
CThe collection and the element
DThe previous element and the next element
✗ Incorrect
The lambda takes the accumulator (current result) and the current element to produce a new accumulator.
What will be the result of listOf(2, 3, 4).fold(1) { acc, n -> acc * n }?
A24
B10
C9
D1
✗ Incorrect
The calculation is 1 * 2 * 3 * 4 = 24.
Describe how fold and reduce work in Kotlin and when you might use each.
Think about starting points and empty lists.
You got /4 concepts.
Write a Kotlin example using reduce to find the maximum number in a list.
Use maxOf or comparison inside lambda.
You got /4 concepts.
Practice
(1/5)
1. What is the main difference between fold and reduce in Kotlin?
easy
A. fold starts with an initial value; reduce starts with the first list item.
B. fold only works on numbers; reduce works on any type.
C. fold modifies the original list; reduce creates a new list.
D. fold is faster than reduce in all cases.
Solution
Step 1: Understand fold behavior
fold requires an initial value and then applies the operation starting from that value.
Step 2: Understand reduce behavior
reduce uses the first element of the list as the initial value and then applies the operation to the rest.
Final Answer:
fold starts with an initial value; reduce starts with the first list item. -> Option A
Quick Check:
fold initial value vs reduce first item = C [OK]
Hint: Remember: fold needs your start, reduce uses first item [OK]
Common Mistakes:
Thinking fold modifies the original list
Assuming reduce needs an initial value
Believing fold only works on numbers
2. Which of the following is the correct syntax to use fold on a list listOf(1, 2, 3) to sum all elements starting from 10?
easy
A. listOf(1, 2, 3).fold(10, (acc, item) -> acc + item)