Challenge - 5 Problems
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code using protocol as type?
Consider the following Swift code where a protocol is used as a type. What will be printed when the code runs?
Swift
protocol Vehicle { func description() -> String } struct Car: Vehicle { func description() -> String { return "Car" } } struct Bike: Vehicle { func description() -> String { return "Bike" } } func printVehicle(_ v: Vehicle) { print(v.description()) } let myVehicle: Vehicle = Car() printVehicle(myVehicle)
Attempts:
2 left
💡 Hint
Remember that the variable is declared as the protocol type but holds a Car instance.
✗ Incorrect
The variable myVehicle is of type Vehicle but holds a Car instance. When calling description(), the Car's implementation is used, so it prints "Car".
❓ Predict Output
intermediate2:00remaining
What error occurs when assigning a non-conforming type to a protocol variable?
Given the protocol and struct below, what happens if you try to assign a String to a variable of type Vehicle?
Swift
protocol Vehicle { func description() -> String } struct Car: Vehicle { func description() -> String { return "Car" } } let myVehicle: Vehicle = "Not a vehicle"
Attempts:
2 left
💡 Hint
Check if String implements the Vehicle protocol.
✗ Incorrect
String does not conform to the Vehicle protocol, so assigning it to a Vehicle type variable causes a compilation error.
🔧 Debug
advanced2:30remaining
Why does this code cause a runtime error when using protocol as type?
Examine the code below. It compiles but crashes at runtime. What is the cause?
Swift
protocol Vehicle { func description() -> String } struct Car: Vehicle { func description() -> String { return "Car" } } struct Bike: Vehicle { func description() -> String { return "Bike" } } let myVehicle: Vehicle = Car() let bike = myVehicle as! Bike print(bike.description())
Attempts:
2 left
💡 Hint
Check the forced cast from one type to another unrelated type.
✗ Incorrect
The forced cast 'as!' tries to convert a Car instance to Bike, which is invalid and causes a runtime crash.
🧠 Conceptual
advanced2:00remaining
How does Swift use protocols as types to enable polymorphism?
Which statement best explains how using a protocol as a type enables polymorphism in Swift?
Attempts:
2 left
💡 Hint
Think about how different types can be treated the same way through a protocol.
✗ Incorrect
Protocols as types allow variables to hold any conforming instance, enabling polymorphism by calling methods defined in the protocol but implemented differently.
📝 Syntax
expert2:30remaining
Which option correctly declares a variable of protocol type with an instance conforming to it?
Given the protocol and struct below, which option correctly declares a variable of type Vehicle and assigns a Car instance?
Swift
protocol Vehicle { func description() -> String } struct Car: Vehicle { func description() -> String { return "Car" } }
Attempts:
2 left
💡 Hint
Remember protocols cannot be instantiated directly.
✗ Incorrect
Option C correctly declares a variable of protocol type and assigns a conforming instance. Option C tries to call protocol as a function, A assigns protocol to struct type, D tries to instantiate protocol directly.