Sample questions
Question 1Easy
What is the output?
protocol G { func hi() }
extension G { func hi() { print("default") } }
struct A: G {}
A().hi()
Adefault- Compile error:
A does not implement hi() - Nothing is printed
Answer: B. default
Explanation
The protocol extension provides a default implementation of hi(), so A conforms without writing its own.
Step-by-step solution
- Protocol G requires
hi() function. - Extension adds default
hi() that prints 'default'. - Struct A adopts G, gets default
hi() automatically. - Calling
A().hi() uses the default implementation. - So it prints 'default'.
Why the other options are wrong
- A. A is just a struct name; the call prints the default text, not the name A.
- C. No compile error: the extension supplies hi(), so A satisfies G without writing it.
- D. Nothing is printed is wrong; the call runs the extension body, which prints default.
Tip: Default methods in protocols are inherited by conforming types.
Question 2Easy
What does a protocol define in Swift?
- A namespace that groups global functions together
- A concrete type that can be created with an initializer
- A set of requirements that conforming types must implement
- A base class whose stored properties are inherited
Answer: C. A set of requirements that conforming types must implement
Explanation
A protocol is a blueprint of methods, properties and other requirements. Structs, classes and enums can adopt it.
Step-by-step solution
- A protocol lists requirements.
- Conforming types must implement them.
Why the other options are wrong
- A. A namespace groups names, not requirements.
- B. A protocol is not concrete.
- D. Protocols are not base classes.
Tip: Protocol = contract.
Question 3Easy
What is Codable in Swift?
- A protocol only for converting types to strings
- A typealias for Encodable & Decodable
- A class that all JSON models must inherit from
- A macro that writes networking code
Answer: B. A typealias for Encodable & Decodable
Explanation
Codable combines Encodable and Decodable, so a type can be converted to and from formats like JSON with JSONEncoder and JSONDecoder.
Step-by-step solution
- Codable is a typealias.
- It equals Encodable & Decodable.
- So a type can be encoded and decoded.
Why the other options are wrong
- A. It is not just for strings.
- C. No base class is needed.
- D. It is a protocol composition, not a macro.
Tip: Codable = Encodable + Decodable.
Question 4Easy
What is the output?
extension Int {
var squared: Int { self * self }
}
print(5.squared)
- 10
- 25
- 5
- Compile error: extensions cannot add properties
Answer: B. 25
Explanation
Extensions can add computed properties. squared returns self * self, which is 25 for 5.
Step-by-step solution
- The extension adds a computed property
squared that multiplies the value by itself. - For 5.squared, it calculates 5 * 5 = 25.
- The print statement outputs 25.
- So the output is 25.
Why the other options are wrong
- A. 10 is 5 doubled, not 5 squared; the property multiplies self by itself.
- C. 5 would appear only if the property did nothing, but it multiplies self.
- D. Extensions can add computed properties to Int, so this compiles and prints 25.
Question 5Medium
How does any Shape differ from some Shape as a parameter or variable type?
- any and some are spellings of exactly the same thing
- any only accepts classes, while some only accepts structs
- any is a box that can hold different conforming types
- any is resolved at compile time; some is resolved at runtime
Answer: C. any is a box that can hold different conforming types
Explanation
any Shape is an existential container that can hold any conforming value, at the cost of dynamic dispatch. some Shape stands for one fixed concrete type known to the compiler.
Step-by-step solution
- Any Shape is an existential box that can hold any type conforming to Shape.
- Different values of any Shape can hold different conforming types.
- Some Shape fixes one concrete type at compile time.
- So option C is correct.
Why the other options are wrong
- A. Any and some are different keywords with different meanings.
- B. Both work with structs and classes; the restriction is not about that.
- D. It is reversed: some is resolved at compile time, and any is a runtime box.
Tip: Use any when the concrete type may change, and some when it is always the same.
Question 6Medium
A protocol requirement func reset() will be implemented by structs that change their own properties. How should it be declared?
mutating func reset()func reset() throwsstatic func reset()final func reset()
Answer: A. mutating func reset()
Explanation
Marking the requirement mutating lets value types change self in their implementation. Classes implement it without the keyword.
Step-by-step solution
- Structs need
mutating to modify properties inside functions. - Protocol requires
mutating func reset() for value types. - Classes don't need
mutating as they can change self by default. - Option A uses
mutating func reset() correctly for structs. - Final answer:
mutating func reset().
Why the other options are wrong
- B. throws means the function can fail with an error; it does not let a struct change.
- C. static makes it a type method; it cannot change instance properties, so it is the wrong form.
- D. final stops overriding in classes; it does not let a struct change its own properties.
Tip: Use mutating in protocol functions for structs to alter self.
Question 7Medium
What does a return type of some View mean?
- An optional View that can be nil when there is no content
- One fixed concrete type, hidden from the caller
- A type-erased box whose concrete type is checked at runtime
- Any type conforming to View, which may differ on every call
Answer: B. One fixed concrete type, hidden from the caller
Explanation
some declares an opaque type: the function always returns the same concrete type, but callers see only that it is a View. This keeps static type information for the compiler.
Step-by-step solution
- Some View means the function returns one specific concrete type.
- The caller sees only that it is a View, not which type it is.
- The type is fixed at compile time and does not change between calls.
- So option B is correct.
Why the other options are wrong
- A. Some View is not optional; it always returns a value.
- C. Some is not a type-erased box; any is.
- D. A type that differs on each call describes any, not some.
Tip: Some means one hidden concrete type; any means a box that can hold different types.
Question 8Medium
struct Point: Equatable { var x: Int; var y: Int } has no == written. What happens?
== compares the memory addresses of the two points- The compiler synthesizes
== comparing x and y - Compile error:
Equatable requires a custom == function - It compiles, but
== always returns false
Answer: B. The compiler synthesizes == comparing x and y
Explanation
When all stored properties are Equatable, declaring the conformance is enough for Swift to synthesize a memberwise ==. Hashable and Codable work the same way.
Step-by-step solution
- Struct Point conforms to Equatable protocol.
- All stored properties x and y are Int, which is Equatable.
- Swift synthesizes == operator comparing x and y values.
- Synthesized == checks if both x and y are equal.
- This allows == to work correctly for Point instances.
Why the other options are wrong
- A. == does not compare addresses for a struct; the synthesized version compares stored values.
- C. No custom == is needed; Swift synthesizes it when all stored properties are Equatable.
- D. It compiles, but == returns true for equal points, so always false is wrong.
Tip: Always check if all properties are Equatable before conforming to Equatable protocol.
Question 9Hard
Why is a delegate protocol often declared as protocol ListDelegate: AnyObject?
- So its methods are dispatched faster at runtime
- So a delegate property of that type can be marked weak
- So the delegate can be stored in a Codable model
- So structs can adopt it as well as classes
Answer: B. So a delegate property of that type can be marked weak
Explanation
weak only applies to class references. Restricting the protocol to classes with AnyObject lets you write weak var delegate: ListDelegate?, which avoids retain cycles.
Step-by-step solution
- Weak only applies to class references.
- Restricting the protocol to classes with AnyObject allows weak properties.
- This prevents retain cycles in delegate patterns.
- So a delegate property of that type can be marked weak.
- Final answer: So a delegate property of that type can be marked weak.
Why the other options are wrong
- A. AnyObject does not speed up dispatch; its purpose is to allow weak references.
- C. AnyObject has nothing to do with Codable; it limits conformance to classes.
- D. AnyObject makes the protocol class-only, so structs cannot adopt it, the opposite of this option.
Tip: Always use AnyObject for delegates to prevent retain cycles.
Question 10Hard
What is the output?
protocol P {}
extension P { func name() -> String { "P" } }
struct S: P { func name() -> String { "S" } }
let x: P = S()
print(x.name(), S().name())
- P P
- S P
- P S
- S S
Answer: C. P S
Explanation
name() is not a protocol requirement, only an extension method, so it is statically dispatched on the declared type. Through x: P the extension version runs; on an S value S's own method runs.
Step-by-step solution
- Name is not a requirement of P, so it exists only in the extension.
- X is declared as P, so x.name() uses the extension version and returns 'P'.
- S().name() calls S's own method, which returns 'S'.
- Output is P S, so the answer is option C.
Why the other options are wrong
- A. S().name() uses S's own method, which returns S.
- B. x.name() uses the static type P, so it returns P.
- D. x.name() does not use S's method because name is not a requirement of P.
Tip: A method that exists only in a protocol extension is chosen by the static type, not the runtime type.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →