OA. free
Free
Sirionlabs, Inc Core Computer Science Core Computer Science Medium

Which of the following options correctly denotes the output of the following Swift code...

Sirionlabs, Inc technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Which of the following options correctly denotes the output of the following Swift code snippet?

struct Square {
    var s: Int = 5
}

protocol MakeShape {
    func makeShape(_ s: Square)
}

class Polygon {
    var delegate: MakeShape?

    func makepolygon() {
        var side = Square()
        side.s = 6
        delegate?.makeShape(side)
    }
}

class PolygonMaker: MakeShape {
    func makeShape(_ s: Square) {
        print("Side of polygon is \(s.s)")
    }
}

let obj = Polygon()
let maker = PolygonMaker()
obj.delegate = maker
obj.makepolygon()
Choose one option.
Show answer & explanation
Answer: A. Side of polygon is 6

The delegate method receives the Square instance with side modified to 6, printing 'Side of polygon is 6'.

Step-by-step Derivation:
Step 1: makepolygon() sets side.s = 6.
Step 2: Calls delegate?.makeShape(side).
Step 3: PolygonMaker prints 'Side of polygon is 6'.