A function is a reusable block of code that performs a specific task. It can take inputs, called parameters, and can return a value.
Click to reveal answer
beginner
What is a closure in Swift?
A closure is a self-contained block of code that can be passed around and used in your code. It can capture and store references to variables and constants from the surrounding context.
Click to reveal answer
beginner
How do you declare a simple function that adds two numbers in Swift?
func add(a: Int, b: Int) -> Int {
return a + b
}
Click to reveal answer
intermediate
What does it mean that closures can "capture" values?
Closures can remember and use constants and variables from the place where they were created, even if that place no longer exists when the closure is called.
Click to reveal answer
intermediate
How can you write a closure that multiplies two numbers and assign it to a variable?
let multiply = { (a: Int, b: Int) -> Int in
return a * b
}
Click to reveal answer
Which keyword is used to define a function in Swift?
Adef
Bfunction
Cmethod
Dfunc
✗ Incorrect
In Swift, the keyword to define a function is func.
What is a closure in Swift?
AA block of code that can be passed around and used later
BA type of variable
CA class method
DA Swift keyword
✗ Incorrect
A closure is a block of code that can be passed around and used later.
How do closures capture values?
ABy copying variables from outside their scope
BBy referencing variables from their surrounding context
CClosures cannot capture values
DBy passing variables as parameters only
✗ Incorrect
Closures capture values by referencing variables from their surrounding context.
Which of the following is the correct syntax for a closure that adds two Ints?
Alet add = { (a: Int, b: Int) -> Int in return a + b }
Bclosure add(a: Int, b: Int) { return a + b }
Cfunc add(a: Int, b: Int) -> Int { return a + b }
Dlet add = (a: Int, b: Int) -> Int { return a + b }
✗ Incorrect
The correct closure syntax uses curly braces and the 'in' keyword: let add = { (a: Int, b: Int) -> Int in return a + b }.
What keyword is used inside a closure to separate parameters from the body?
Athen
Bdo
Cin
Dwhere
✗ Incorrect
The keyword in separates the closure's parameters and return type from its body.
Explain in your own words what a closure is and how it differs from a function in Swift.
Think about how closures can remember variables from where they were created.
You got /3 concepts.
Write a simple Swift function and a closure that both add two numbers. Describe their syntax differences.
Remember functions use 'func' keyword; closures use curly braces and 'in'.
You got /4 concepts.
Practice
(1/5)
1. What is a closure in Swift? func greet() { print("Hello") }
easy
A. An unnamed block of code that can be stored and passed around
B. A named function that cannot be reused
C. A variable that holds a number
D. A type of loop
Solution
Step 1: Understand closure definition
A closure is a block of code without a name that can be saved and used later.
Step 2: Compare with function
Functions have names, closures do not but both can be reused.
Final Answer:
An unnamed block of code that can be stored and passed around -> Option A
Quick Check:
Closure = unnamed reusable code block [OK]
Hint: Closures are like functions without names [OK]
Common Mistakes:
Confusing closures with variables
Thinking closures must have names
Mixing closures with loops
2. Which of the following is the correct syntax to declare a closure that takes no parameters and returns an Int?
easy
A. let closure = (Int) -> { return 5 }
B. let closure = { () -> Int in return 5 }
C. let closure = func() -> Int { return 5 }
D. func closure() -> Int { return 5 }
Solution
Step 1: Identify closure syntax
Closures use curly braces with parameters and return type before 'in'.
Step 2: Check each option
'let closure = { () -> Int in return 5 }' matches closure syntax. 'func closure() -> Int { return 5 }' declares a named function. 'let closure = func() -> Int { return 5 }' uses invalid syntax. 'let closure = (Int) -> { return 5 }' lacks 'in' keyword and has wrong parameter placement.
Final Answer:
let closure = { () -> Int in return 5 } -> Option B
Quick Check:
Closure syntax = curly braces + 'in' [OK]
Hint: Closures use braces and 'in' keyword [OK]
Common Mistakes:
Using func keyword inside closure
Missing 'in' keyword
Wrong parameter syntax
3. What will be printed by this Swift code?
let multiply = { (a: Int, b: Int) -> Int in
return a * b
}
print(multiply(3, 4))
medium
A. 34
B. 7
C. 12
D. Error
Solution
Step 1: Understand closure parameters and return
The closure takes two Ints and returns their product.
Step 2: Calculate multiply(3, 4)
3 * 4 equals 12, so print outputs 12.
Final Answer:
12 -> Option C
Quick Check:
3 times 4 = 12 [OK]
Hint: Closures return calculated values like functions [OK]
Common Mistakes:
Adding instead of multiplying
Concatenating numbers as strings
Syntax errors in closure call
4. Identify the error in this closure declaration:
let add = { (a: Int, b: Int) Int in
return a + b
}
medium
A. Missing '->' before return type
B. Missing 'in' keyword
C. Parameters should not have types
D. Closure cannot return Int
Solution
Step 1: Check closure signature syntax
Return type must be preceded by '->' in closure signature.
Step 2: Identify missing arrow
Code has '(a: Int, b: Int) Int' but should be '(a: Int, b: Int) -> Int'.
Final Answer:
Missing '->' before return type -> Option A
Quick Check:
Closure return type needs '->' [OK]
Hint: Return type needs '->' before it in closures [OK]
Common Mistakes:
Omitting '->' in closure signature
Placing 'in' incorrectly
Removing parameter types
5. You want to sort an array of names alphabetically using a closure. Which code correctly uses a closure with the sorted(by:) method?
hard
A. let sortedNames = names.sorted(by: { s1, s2 -> Bool { return s1 > s2 } })
B. let sortedNames = names.sorted(by: { s1, s2 in s1 + s2 })
C. let sortedNames = names.sorted(by: (s1, s2) -> s1 < s2)
D. let sortedNames = names.sorted(by: { s1, s2 in s1 < s2 })
Solution
Step 1: Understand sorted(by:) closure
It expects a closure that returns true if first element should come before second.
Step 2: Check each option
'let sortedNames = names.sorted(by: { s1, s2 in s1 < s2 })' returns true when s1 < s2 (correct for ascending alphabetical order). The version with s1 > s2 sorts descending. The version with (s1, s2) -> s1 < s2 uses invalid syntax without braces or 'in'. The version with s1 + s2 returns String, not Bool.
Final Answer:
let sortedNames = names.sorted(by: { s1, s2 in s1 < s2 }) -> Option D