Bird
Raised Fist0
Swiftprogramming~20 mins

Protocol declaration syntax in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Protocol Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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())
ANo output
B"Hello!"
C"Hi!"
DError: Protocol method not implemented
Attempts:
2 left
💡 Hint
Check how the greet() method is implemented in Person.
❓ Predict Output
intermediate
2: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)
Anil
BError: Missing property implementation
C"id"
D"abc123"
Attempts:
2 left
💡 Hint
Look at how id is defined and initialized in User.
🔧 Debug
advanced
2: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()
}
AThe property <code>speed</code> must specify { get } or { get set }
BThe function <code>drive()</code> must have a return type
CProtocols cannot have properties
DThe protocol keyword is misspelled
Attempts:
2 left
💡 Hint
Check how properties are declared in protocols.
🧠 Conceptual
advanced
2:00remaining
Protocol inheritance syntax
Which option correctly declares a protocol FlyingCar that inherits from protocols Car and Flyable?
Aprotocol FlyingCar: Car, Flyable {}
Bprotocol FlyingCar inherits Car, Flyable {}
Cprotocol FlyingCar <- Car, Flyable {}
Dprotocol FlyingCar extends Car & Flyable {}
Attempts:
2 left
💡 Hint
Look at how Swift declares protocol inheritance using colons and commas.
❓ Predict Output
expert
3: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())
A"Hello from Robot"\nCompilation error
BCompilation error: Alien does not implement speak()
C"Hello from Robot"\n"Hello from extension"
D"Hello from extension"\n"Hello from extension"
Attempts:
2 left
💡 Hint
Check how protocol extensions provide default implementations.

Practice

(1/5)
1. What keyword is used in Swift to declare a protocol?
easy
A. interface
B. class
C. protocol
D. struct

Solution

  1. Step 1: Recall Swift syntax for protocols

    In Swift, protocols are declared using the keyword protocol.
  2. Step 2: Compare options with known syntax

    Options like interface are from other languages, and class or struct declare types, not protocols.
  3. Final Answer:

    protocol -> Option C
  4. Quick Check:

    Keyword for protocol declaration = protocol [OK]
Hint: Remember: Swift uses 'protocol' keyword for protocols [OK]
Common Mistakes:
  • Confusing 'protocol' with 'interface' from other languages
  • Using 'class' or 'struct' instead of 'protocol'
  • Misspelling the keyword
2. Which of the following is the correct syntax to declare a protocol named Vehicle in Swift?
easy
A. protocol Vehicle {}
B. protocol Vehicle() {}
C. protocol Vehicle[] {}
D. protocol Vehicle<> {}

Solution

  1. Step 1: Understand protocol declaration syntax

    Protocols are declared with the keyword protocol, followed by the name and curly braces without parentheses or brackets.
  2. Step 2: Check each option

    protocol Vehicle {} uses correct syntax: protocol Vehicle {}. Options B, C, and D incorrectly add parentheses or brackets.
  3. Final Answer:

    protocol Vehicle {} -> Option A
  4. Quick Check:

    Protocol declaration uses curly braces only [OK]
Hint: Protocols use curly braces, no parentheses or brackets [OK]
Common Mistakes:
  • Adding parentheses after protocol name
  • Using square or angle brackets incorrectly
  • Omitting curly braces
3. What will be the output of this Swift code?
protocol Greetable {
    func greet() -> String
}

struct Person: Greetable {
    func greet() -> String {
        return "Hello!"
    }
}

let p = Person()
print(p.greet())
medium
A. Hello!
B. greet()
C. Error: Missing protocol method implementation
D. nil

Solution

  1. Step 1: Understand protocol and struct implementation

    The protocol Greetable requires a method greet() returning a String. The struct Person implements this method returning "Hello!".
  2. Step 2: Analyze the print statement

    Creating an instance p of Person and calling p.greet() returns "Hello!", which is printed.
  3. Final Answer:

    Hello! -> Option A
  4. Quick Check:

    Implemented protocol method returns "Hello!" [OK]
Hint: Protocol methods must be implemented; output is returned string [OK]
Common Mistakes:
  • Assuming protocol methods run automatically without implementation
  • Expecting error due to protocol conformance
  • Confusing method name with output
4. Identify the error in this protocol declaration:
protocol Drawable {
    func draw()
    var color: String
}
medium
A. func draw() must have a return type
B. Protocol properties must specify { get } or { get set }
C. Protocol name must start with uppercase letter
D. Missing parentheses after func draw

Solution

  1. Step 1: Check protocol property declaration rules

    In Swift, protocol properties must declare if they are readable or writable using { get } or { get set }.
  2. Step 2: Analyze the given property

    The property color: String lacks { get } or { get set }, causing a syntax error.
  3. Final Answer:

    Protocol properties must specify { get } or { get set } -> Option B
  4. Quick Check:

    Protocol property syntax requires access specifiers [OK]
Hint: Always add { get } or { get set } to protocol properties [OK]
Common Mistakes:
  • Omitting { get } or { get set } in protocol properties
  • Adding parentheses to property declarations
  • Thinking protocol names must be uppercase
5. You want to declare a protocol Identifiable that requires a read-only id property of type String and a method identify() that returns a String. Which is the correct declaration?
hard
A. protocol Identifiable { let id: String func identify() -> String }
B. protocol Identifiable { var id: String func identify() }
C. protocol Identifiable { var id: String { get set } func identify() -> String }
D. protocol Identifiable { var id: String { get } func identify() -> String }

Solution

  1. Step 1: Define read-only property in protocol

    A read-only property in a protocol must be declared with { get } to indicate it can be read but not written.
  2. Step 2: Check method signature

    The method identify() must specify return type String as required.
  3. Step 3: Evaluate options

    protocol Identifiable { var id: String { get } func identify() -> String } correctly declares var id: String { get } and func identify() -> String. protocol Identifiable { var id: String func identify() } misses { get } and return type. protocol Identifiable { let id: String func identify() -> String } uses let which is invalid in protocols. protocol Identifiable { var id: String { get set } func identify() -> String } allows writing with { get set }, not read-only.
  4. Final Answer:

    protocol Identifiable { var id: String { get } func identify() -> String } -> Option D
  5. Quick Check:

    Read-only property uses { get } and method return type matches [OK]
Hint: Use { get } for read-only properties in protocols [OK]
Common Mistakes:
  • Using 'let' instead of 'var' in protocol
  • Omitting { get } for read-only properties
  • Missing return type in method declaration
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard