0
0
Swiftprogramming~20 mins

BuildBlock for combining results in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift BuildBlock Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using @resultBuilder?
Consider the following Swift code that uses a custom @resultBuilder named ResultCombiner. What will be printed when combineResults() is called?
Swift
import Foundation

@resultBuilder
struct ResultCombiner {
    static func buildBlock(_ components: String...) -> String {
        components.joined(separator: ", ")
    }
}

func combineResults() -> String {
    @ResultCombiner var result: String {
        "Hello"
        "World"
        "Swift"
    }
    return result
}

print(combineResults())
A"Hello, World, Swift"
B"HelloWorldSwift"
C"[Hello, World, Swift]"
D"Hello World Swift"
Attempts:
2 left
💡 Hint
Look at how the buildBlock function joins the components.
🧠 Conceptual
intermediate
1:30remaining
What does the buildBlock function do in a Swift result builder?
In Swift's result builder, what is the main role of the buildBlock static function?
AIt initializes the result builder instance.
BIt combines multiple components into a single result.
CIt validates the types of components passed.
DIt handles errors during building.
Attempts:
2 left
💡 Hint
Think about how multiple parts are combined into one output.
🔧 Debug
advanced
2:00remaining
Why does this buildBlock implementation cause a compile error?
Examine this buildBlock function inside a Swift result builder. Why does it cause a compile error?
static func buildBlock(_ components: String) -> String {
    components + "!"
}
Swift
static func buildBlock(_ components: String) -> String {
    components + "!"
}
AbuildBlock cannot be static.
BYou cannot use + operator on String in buildBlock.
CbuildBlock must return an Int, not String.
DbuildBlock must accept a variadic parameter, not a single String.
Attempts:
2 left
💡 Hint
Check the parameter type expected by buildBlock.
📝 Syntax
advanced
2:00remaining
Which buildBlock implementation correctly combines Int values into their sum?
You want to create a buildBlock function that sums all Int components passed to it. Which option is correct?
Astatic func buildBlock(_ components: Int...) -> Int { components.add() }
Bstatic func buildBlock(components: Int...) -> Int { components.reduce(0, +) }
Cstatic func buildBlock(_ components: Int...) -> Int { components.reduce(0, +) }
Dstatic func buildBlock(_ components: [Int]) -> Int { components.sum() }
Attempts:
2 left
💡 Hint
Check parameter syntax and method names for summing arrays.
🚀 Application
expert
3:00remaining
How to combine optional String results safely in buildBlock?
You want to combine multiple optional String components in buildBlock, ignoring nil values and joining non-nil with ", ". Which implementation achieves this?
A
static func buildBlock(_ components: String?...) -> String {
    components.compactMap { $0 }.joined(separator: ", ")
}
B
static func buildBlock(_ components: String?...) -> String {
    components.map { $0 ?? "" }.joined(separator: ", ")
}
C
static func buildBlock(_ components: [String?]) -> String {
    components.joined(separator: ", ")
}
D
static func buildBlock(_ components: String?...) -> String {
    components.reduce("") { $0 + $1! + ", " }
}
Attempts:
2 left
💡 Hint
Consider how to safely unwrap optionals and skip nils.