Imagine you have a large list of customer records. Why is using collection algorithms like filtering and sorting important when querying this data?
Think about how sorting and filtering affect the amount of data you work with.
Collection algorithms reduce the workload by selecting only needed data and arranging it, which speeds up queries.
Given an array of user ages: [18, 22, 15, 30, 25], what is the result of filtering ages greater than 20?
let ages = [18, 22, 15, 30, 25] let filtered = ages.filter { $0 > 20 } print(filtered)
Filter keeps only items where the condition is true.
The filter keeps ages greater than 20, so 22, 30, and 25 remain.
Choose the option that sorts an array of records by the 'name' property in ascending order.
struct Record { let name: String } let records = [Record(name: "Zoe"), Record(name: "Anna"), Record(name: "Mike")]
Remember that sorted returns a new array and uses a closure comparing properties.
Option A correctly uses sorted with a closure comparing names ascending.
Which option best explains how collection algorithms help reduce the load on a database server?
Think about where filtering and sorting happen and how that affects server work.
Filtering and sorting data before sending it to the server reduces the amount of data processed and transferred, lowering server load.
Consider this code that tries to find the first user older than 25:
let users = [("Alice", 24), ("Bob", 30), ("Carol", 22)]
let user = users.first { age > 25 }
print(user)What error will this code produce?
Look at the closure syntax and variable usage inside it.
The closure uses 'age' without defining it, causing a syntax error for an unexpected token.