DevPrep

All topics › What does it print? Interview puzzles

Dispatch: what does the protocol extension print?

Senior PRO

Task

protocol MyProtocol { }

extension MyProtocol {
    func method() { print("In Protocol") }
}

struct MyStruct: MyProtocol {
    func method() { print("In Struct") }
}

let myStruct = MyStruct()
let myProtocol: MyProtocol = myStruct

myStruct.method()
myProtocol.method()

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store