Bird
Raised Fist0
Kotlinprogramming~10 mins

Fold and reduce operations 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 calculate the sum of all numbers in the list using fold.

Kotlin
val numbers = listOf(1, 2, 3, 4)
val sum = numbers.fold([1]) { acc, num -> acc + num }
println(sum)
Drag options to blanks, or click blank then click option'
A0
B1
Cnull
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as initial value will add 1 extra to the sum.
Using null causes a runtime error.
2fill in blank
medium

Complete the code to find the product of all numbers in the list using reduce.

Kotlin
val numbers = listOf(2, 3, 4)
val product = numbers.reduce { acc, num -> acc [1] num }
println(product)
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' will sum instead of multiply.
Using '-' or '/' will give wrong results or errors.
3fill in blank
hard

Fix the error in the code to correctly concatenate all strings in the list using fold.

Kotlin
val words = listOf("Kotlin", "is", "fun")
val sentence = words.fold([1]) { acc, word -> acc + " " + word }
println(sentence.trim())
Drag options to blanks, or click blank then click option'
A1
B""
C0
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using null causes a NullPointerException.
Using 0 or 1 causes type mismatch errors.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, but only include words longer than 3 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val lengths = words.associateWith { [1] }
val filtered = lengths.filter { it.value [2] 3 }
println(filtered)
Drag options to blanks, or click blank then click option'
Ait.length
B>
C<
Dit.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.size is invalid for String.
Using '<' filters shorter words, not longer.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.

Kotlin
val words = listOf("tree", "house", "car", "elephant")
val result = words.filter { it.length [1] 4 }
    .associateBy([2]) { [3] }
println(result)
Drag options to blanks, or click blank then click option'
A>
Bit.uppercase()
Cit.length
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters shorter words.
Using it.lowercase() instead of uppercase.
Swapping key and value in associateBy.

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

  1. Step 1: Understand fold behavior

    fold requires an initial value and then applies the operation starting from that value.
  2. 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.
  3. Final Answer:

    fold starts with an initial value; reduce starts with the first list item. -> Option A
  4. 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)
B. listOf(1, 2, 3).fold { acc, item -> acc + item }
C. listOf(1, 2, 3).fold(10) { acc, item -> acc + item }
D. listOf(1, 2, 3).fold(10, acc + item)

Solution

  1. Step 1: Check fold function signature

    fold takes an initial value and a lambda with two parameters: accumulator and current item.
  2. Step 2: Match syntax with options

    listOf(1, 2, 3).fold(10) { acc, item -> acc + item } correctly uses fold(10) { acc, item -> acc + item }. Others miss initial value or have wrong lambda syntax.
  3. Final Answer:

    listOf(1, 2, 3).fold(10) { acc, item -> acc + item } -> Option C
  4. Quick Check:

    fold(initial) with lambda = D [OK]
Hint: fold needs initial value and lambda with two params [OK]
Common Mistakes:
  • Omitting the initial value in fold
  • Using incorrect lambda syntax
  • Passing expression instead of lambda
3. What is the output of this Kotlin code?
val numbers = listOf(2, 3, 4)
val result = numbers.reduce { acc, item -> acc * item }
println(result)
medium
A. Error
B. 9
C. 14
D. 24

Solution

  1. Step 1: Understand reduce operation

    reduce starts with first element (2) as accumulator, then multiplies by next items.
  2. Step 2: Calculate step-by-step

    2 * 3 = 6, then 6 * 4 = 24.
  3. Final Answer:

    24 -> Option D
  4. Quick Check:

    2 * 3 * 4 = 24 [OK]
Hint: Multiply stepwise starting from first item [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Starting from zero instead of first item
  • Expecting a list output
4. Identify the error in this Kotlin code using reduce:
val emptyList = listOf<Int>()
val result = emptyList.reduce { acc, item -> acc + item }
println(result)
medium
A. Throws exception because list is empty
B. No error, output is 0
C. Syntax error in lambda
D. Returns null

Solution

  1. Step 1: Check reduce behavior on empty list

    reduce requires at least one element; empty list causes runtime exception.
  2. Step 2: Understand exception cause

    Since list is empty, no first element exists to start reduction, so it throws NoSuchElementException.
  3. Final Answer:

    Throws exception because list is empty -> Option A
  4. Quick Check:

    reduce on empty list = Exception [OK]
Hint: reduce fails on empty lists, fold needs initial value [OK]
Common Mistakes:
  • Assuming reduce returns 0 on empty list
  • Thinking lambda syntax is wrong
  • Expecting null instead of exception
5. Given a list of strings listOf("a", "b", "c"), which Kotlin expression using fold produces the string "abc-"?
hard
A. listOf("a", "b", "c").fold("-") { acc, s -> acc + s }
B. listOf("a", "b", "c").fold("") { acc, s -> acc + s + "-" }
C. listOf("a", "b", "c").reduce { acc, s -> acc + s + "-" }
D. listOf("a", "b", "c").fold("") { acc, s -> s + acc + "-" }

Solution

  1. Step 1: Understand fold with initial empty string

    Starting with empty string, each item is appended with a dash after it.
  2. Step 2: Check each step of fold

    "" + "a" + "-" = "a-"; then "a-" + "b" + "-" = "a-b-"; then "a-b-" + "c" + "-" = "abc-".
  3. Final Answer:

    listOf("a", "b", "c").fold("") { acc, s -> acc + s + "-" } -> Option B
  4. Quick Check:

    fold with empty start and append = A [OK]
Hint: fold with empty start and add dash after each item [OK]
Common Mistakes:
  • Using reduce which starts with first item, missing initial empty string
  • Appending dash before instead of after
  • Wrong initial value causing different output
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