Swift Arrays Quiz

Topic: Swift Arrays

Swift Quiz #05: Swift Arrays

7 mins read

1. What is the output?

 var numbers = [1, 2, 3, 4, 5]
let transformedNumbers = numbers.map { $0 * 2 }
print(transformedNumbers)

2. Which method is used to find the first index of a specific element in an array?

3. What is the output?

 let names = ["Alice", "Bob", "Charlie"]
let result = names.allSatisfy { $0.count >= 3 }
print(result)

4. What is the output?

 let numbers = [1, 2, 3, 4, 5]
let result = numbers.reduce(3) { $0 + $1 }
print(result)

5. Which method can be used to check if an array contains a specific element?

6. What is the output?

 let numbers = [1, 2, 3, 4, 5]
let result = numbers.filter { $0 % 2 == 0 }
.flatMap { Array(repeating: $0, count: $0) }
print(result)

7. What is the output?

 let names = ["Alice", "Bob", "Charlie", "David"]
let result = names.compactMap { $0.count > 4 ? $0 : nil }
print(result)

8. What is the difference between map and flatMap when dealing with optionals?

9. What is the output?

 let words = ["Hello", "World", "Swift"]
let result = words.joined(separator: " ")
print(result)