0
0
Swiftprogramming~20 mins

Why Swift for Apple and beyond - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Mastery: Apple and Beyond
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 optionals?

Consider this Swift code that uses optionals and unwrapping. What will it print?

Swift
var name: String? = "Swift"
if let unwrappedName = name {
    print("Hello, \(unwrappedName)!")
} else {
    print("No name provided.")
}
ANo name provided.
BOptional("Swift")
CHello, Swift!
DCompilation error
Attempts:
2 left
💡 Hint

Think about what if let does with optionals in Swift.

🧠 Conceptual
intermediate
2:00remaining
Why does Swift use value types like structs over classes?

Swift encourages using structs (value types) instead of classes (reference types) in many cases. Why is this beneficial?

AValue types provide safer code by avoiding unintended shared state.
BValue types are slower but use less memory than classes.
CClasses cannot have methods in Swift.
DValue types allow inheritance which classes do not.
Attempts:
2 left
💡 Hint

Think about how copying works with value types versus reference types.

🔧 Debug
advanced
2:00remaining
What error does this Swift code produce?

Look at this Swift code snippet. What error will it cause when compiled?

Swift
let numbers = [1, 2, 3]
let first = numbers[3]
print(first)
ANo error, prints nil
BIndex out of range runtime error
CPrints 3
DCompilation error: Array index must be Int
Attempts:
2 left
💡 Hint

Remember how Swift arrays handle invalid indexes.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a Swift function with a default parameter?

Choose the correct Swift function declaration that has a default value for its parameter.

Afunc greet(name: String = "Guest") { print("Hello, \(name)!") }
Bfunc greet(name: String?) = "Guest" { print("Hello, \(name)!") }
Cfunc greet(name: String = Guest) { print("Hello, \(name)!") }
Dfunc greet(name: String default "Guest") { print("Hello, \(name)!") }
Attempts:
2 left
💡 Hint

Look for the correct syntax to assign a default value to a parameter.

🚀 Application
expert
2:00remaining
How many items are in the resulting dictionary after this Swift code runs?

Consider this Swift code that creates a dictionary using a dictionary literal with a condition. How many key-value pairs does the dictionary contain?

Swift
let dict = Dictionary(uniqueKeysWithValues: (1...5).map { ($0, $0 * 2) }.filter { $0.0 % 2 == 0 })
print(dict.count)
A4
B3
C5
D2
Attempts:
2 left
💡 Hint

Count how many numbers from 1 to 5 are even.