A closure expression is a short, unnamed block of code that can be passed around and used in your code. It can capture values from its surrounding context.
Click to reveal answer
beginner
How do you write a simple closure expression that adds two numbers in Swift?
You can write it like this:
{ (a: Int, b: Int) -> Int in return a + b }
Click to reveal answer
beginner
What does the 'in' keyword do in a closure expression?
The 'in' keyword separates the closure's parameters and return type from the body of the closure.
Click to reveal answer
intermediate
How can you simplify a closure expression in Swift?
You can omit parameter types, return type, and even the 'return' keyword if the closure has a single expression. Swift can infer these automatically.
Click to reveal answer
intermediate
What are shorthand argument names in closure expressions?
Shorthand argument names like $0, $1 let you refer to closure parameters without naming them explicitly, making the closure shorter and cleaner.
Click to reveal answer
What keyword separates parameters from the body in a Swift closure expression?
Alet
Bin
Creturn
Dfunc
✗ Incorrect
The 'in' keyword separates the closure's parameters and return type from its body.
Which of these is a valid way to write a closure that adds two Ints in Swift?
Aclosure add(a: Int, b: Int) { return a + b }
Bfunc add(a: Int, b: Int) { a + b }
Clet add = (a, b) => a + b
D{ (a: Int, b: Int) -> Int in return a + b }
✗ Incorrect
Option D shows the correct closure expression syntax in Swift.
How can you refer to the first parameter in a closure without naming it?
A$0
Bparam1
Cfirst
Darg1
✗ Incorrect
Shorthand argument names like $0 refer to the first parameter in a closure.
What can Swift infer in a closure expression to simplify the syntax?
AFunction names
BVariable names
CParameter types and return type
DClass names
✗ Incorrect
Swift can infer parameter types and return type in closure expressions, allowing you to omit them.
Which of the following is NOT true about closure expressions in Swift?
AThey must always have explicit parameter types
BThey can capture values from their surrounding context
CThey can be assigned to variables
DThey can be passed as arguments to functions
✗ Incorrect
Closure expressions do NOT always need explicit parameter types; Swift can infer them.
Explain the basic syntax of a closure expression in Swift and the role of the 'in' keyword.
Think about how you separate inputs from the code inside.
You got /4 concepts.
Describe how you can simplify closure expressions using Swift's type inference and shorthand argument names.
Swift helps you write less code by guessing types and letting you skip names.
You got /4 concepts.
Practice
(1/5)
1. What is a closure in Swift?
easy
A. A special kind of loop for repeating tasks
B. A block of code that can be passed around and executed later
C. A type of variable that stores numbers only
D. A keyword used to declare functions
Solution
Step 1: Understand the definition of closure
A closure is a block of code that can be saved and run later, similar to a function but more flexible.
Step 2: Compare options with closure meaning
Only 'A block of code that can be passed around and executed later' correctly describes a closure.
Final Answer:
A block of code that can be passed around and executed later -> Option B
Quick Check:
Closure = block of code passed and run later [OK]
Hint: Closures are like saved code blocks you run later [OK]
Common Mistakes:
Confusing closures with variables
Thinking closures are loops
Mixing closures with function keywords
2. Which of the following is the correct syntax for a closure that takes two Ints and returns their sum?
easy
A. { a, b in a + b }
B. { (a, b) -> Int { a + b } }
C. func sum(a: Int, b: Int) -> Int { return a + b }
D. { (a: Int, b: Int) -> Int in return a + b }
Solution
Step 1: Identify closure syntax components
A closure with parameters and return type uses parentheses for parameters, '->' for return type, and 'in' before the code.
Step 2: Check each option
{ (a: Int, b: Int) -> Int in return a + b } correctly uses (a: Int, b: Int) -> Int in return a + b. { a, b in a + b } omits types and requires context for type inference to compile. func sum(a: Int, b: Int) -> Int { return a + b } is a function, not a closure. { (a, b) -> Int { a + b } } has wrong braces placement and misses 'in'.
Final Answer:
{ (a: Int, b: Int) -> Int in return a + b } -> Option D
Quick Check:
Closure syntax = parameters, return type, in keyword [OK]
Hint: Closure syntax: parameters, arrow, return type, then 'in' [OK]
Common Mistakes:
Confusing functions with closures
Missing 'in' keyword
Incorrect braces placement
3. What is the output of this Swift code?
let multiply = { (x: Int, y: Int) -> Int in x * y }
print(multiply(3, 4))
medium
A. 12
B. 7
C. 34
D. Error
Solution
Step 1: Understand closure definition and call
The closure multiply takes two Ints and returns their product (x * y). It is called with 3 and 4.
Step 2: Calculate the result
3 * 4 = 12, so print outputs 12.
Final Answer:
12 -> Option A
Quick Check:
3 times 4 = 12 [OK]
Hint: Multiply inputs inside closure to get output [OK]
Common Mistakes:
Adding instead of multiplying
Concatenating numbers as strings
Syntax errors in closure call
4. Find the error in this closure syntax:
let greet = { name: String in
print("Hello, \(name)!")
}
medium
A. Parameters must be in parentheses
B. Missing 'in' keyword after parameters
C. Closure cannot print inside its body
D. Missing parameter types in closure
Solution
Step 1: Check closure parameter syntax
Closure parameters must be enclosed in parentheses, like (name: String).
Step 2: Identify the error in given code
The code uses 'name: String' without parentheses, which is invalid syntax.
Final Answer:
Parameters must be in parentheses -> Option A
Quick Check:
Closure parameters need parentheses [OK]
Hint: Always put closure parameters inside parentheses [OK]
Common Mistakes:
Omitting parentheses around parameters
Forgetting 'in' keyword
Thinking closures can't print
5. How can you simplify this closure expression?
let numbers = [1, 2, 3, 4]
let doubled = numbers.map({ (num: Int) -> Int in return num * 2 })
print(doubled)
hard
A. Use a for loop instead of map
B. Remove 'return' and keep full syntax: numbers.map { (num: Int) -> Int in num * 2 }
C. Use shorthand argument names: numbers.map { $0 * 2 }
D. Cannot simplify closure expressions
Solution
Step 1: Understand closure simplification rules
Swift allows removing parameter types and 'return' when the closure is a single expression, and using shorthand $0 for first parameter.
Step 2: Apply shorthand argument names
Replacing full closure with numbers.map { $0 * 2 } simplifies code and is correct.
Final Answer:
Use shorthand argument names: numbers.map { $0 * 2 } -> Option C
Quick Check:
Shorthand $0 replaces explicit parameters [OK]
Hint: Use $0, $1 for closure parameters to shorten code [OK]
Common Mistakes:
Keeping full syntax unnecessarily
Thinking closures can't be simplified
Replacing map with loops unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.