0
0
Swiftprogramming~20 mins

Protocol as types in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Protocol Mastery
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 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)
ACar
BBike
CVehicle
DCompilation error
Attempts:
2 left
💡 Hint
Remember that the variable is declared as the protocol type but holds a Car instance.
Predict Output
intermediate
2: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"
ANo error, but description() returns empty string
BRuntime error: Cannot cast String to Vehicle
CThe code compiles and prints "Not a vehicle"
DCompilation error: String does not conform to Vehicle
Attempts:
2 left
💡 Hint
Check if String implements the Vehicle protocol.
🔧 Debug
advanced
2: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())
ACompilation error: Bike not defined
BRuntime crash due to forced cast from Car to Bike failing
CPrints "Car" without error
DRuntime crash due to missing description() method
Attempts:
2 left
💡 Hint
Check the forced cast from one type to another unrelated type.
🧠 Conceptual
advanced
2: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?
AA protocol type variable can hold any instance of a type that conforms to that protocol, allowing different implementations to be used interchangeably.
BProtocols store the actual data of conforming types, so variables of protocol type contain all properties directly.
CUsing a protocol as a type disables dynamic dispatch and forces static method calls.
DProtocols can only be used as types if they have no methods or properties.
Attempts:
2 left
💡 Hint
Think about how different types can be treated the same way through a protocol.
📝 Syntax
expert
2: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"
    }
}
Alet myVehicle: Car = Vehicle()
Blet myVehicle = Vehicle(Car())
Clet myVehicle: Vehicle = Car()
Dlet myVehicle: Vehicle = Vehicle()
Attempts:
2 left
💡 Hint
Remember protocols cannot be instantiated directly.