Challenge - 5 Problems
Swift Protocol Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple protocol conformance
What is the output of this Swift code when calling
greet() on person?Swift
protocol Greeter { func greet() -> String } struct Person: Greeter { func greet() -> String { return "Hello!" } } let person = Person() print(person.greet())
Attempts:
2 left
💡 Hint
Check how the
greet() method is implemented in Person.✗ Incorrect
The
Person struct conforms to the Greeter protocol by implementing greet() returning "Hello!". So calling greet() prints "Hello!".❓ Predict Output
intermediate2:00remaining
Protocol with property requirement output
What will be printed by this Swift code?
Swift
protocol Identifiable { var id: String { get } } struct User: Identifiable { var id: String } let user = User(id: "abc123") print(user.id)
Attempts:
2 left
💡 Hint
Look at how
id is defined and initialized in User.✗ Incorrect
The
User struct implements the id property required by Identifiable. The instance is created with id = "abc123", so printing user.id outputs "abc123".🔧 Debug
advanced2:00remaining
Identify the syntax error in protocol declaration
Which option contains the syntax error in this protocol declaration?
Swift
protocol Vehicle { var speed: Int func drive() }
Attempts:
2 left
💡 Hint
Check how properties are declared in protocols.
✗ Incorrect
In Swift protocols, properties must specify whether they are readable (
{ get }) or readable and writable ({ get set }). Missing this causes a syntax error.🧠 Conceptual
advanced2:00remaining
Protocol inheritance syntax
Which option correctly declares a protocol
FlyingCar that inherits from protocols Car and Flyable?Attempts:
2 left
💡 Hint
Look at how Swift declares protocol inheritance using colons and commas.
✗ Incorrect
Swift uses a colon after the protocol name followed by a comma-separated list of inherited protocols.
❓ Predict Output
expert3:00remaining
Output of protocol extension method call
What is the output of this Swift code?
Swift
protocol Speaker { func speak() -> String } extension Speaker { func speak() -> String { return "Hello from extension" } } struct Robot: Speaker { func speak() -> String { return "Hello from Robot" } } struct Alien: Speaker {} let r = Robot() let a = Alien() print(r.speak()) print(a.speak())
Attempts:
2 left
💡 Hint
Check how protocol extensions provide default implementations.
✗ Incorrect
The
Robot struct implements speak(), so its version is called. The Alien struct does not implement speak(), so it uses the default from the protocol extension.