Sample questions
Question 1Easy
What is the output?
struct P: Equatable { var x: Int }
print(P(x: 1) == P(x: 1))
- false
- true
- Compile error
- nil
Answer: B. true
Explanation
Swift auto-synthesises == when all stored properties are Equatable.
Step-by-step solution
- P: Equatable.
- Same x.
- true.
Why the other options are wrong
- A. Equatable is synthesised for stored properties.
- C. Swift synthesises ==.
- D. == returns a Bool.
Question 2Easy
What is the output?
func swap2<T>(_ a: inout T, _ b: inout T) {
let t = a; a = b; b = t
}
var x = 1, y = 2
swap2(&x, &y)
print(x, y)
- 1 2
- 2 2
- Compile error
- 2 1
Answer: D. 2 1
Explanation
The generic function swaps two variables of the same type.
Step-by-step solution
- t = 1.
- a = 2.
- b = 1.
Why the other options are wrong
- A. The values are swapped.
- B. t keeps the original a.
- C. Generics work with inout.
Question 3Easy
What is a protocol in Swift?
- A blueprint of methods and properties that types can adopt
- A concrete class that cannot be subclassed by other types
- A type that stores and caches network data for an app
- A keyword that marks a declaration as private to a file
Answer: A. A blueprint of methods and properties that types can adopt
Explanation
A protocol defines requirements that conforming types must implement.
Step-by-step solution
- A protocol lists required methods and properties.
- Types adopt the protocol and provide the implementations.
- So option A is correct.
Why the other options are wrong
- B. A concrete class is a class, not a protocol.
- C. A network cache is not what a protocol is.
- D. Private is an access level keyword, not a protocol.
Tip: A protocol is a contract; types conform to it.
Question 4Easy
What is the output?
protocol G { func hi() -> String }
struct E: G { func hi() -> String { "hi" } }
print(E().hi())
- G
- hi
- Compile error
- nil
Answer: B. hi
Explanation
E conforms to G by implementing hi().
Step-by-step solution
- E implements G.
- hi() returns "hi".
- Prints hi.
Why the other options are wrong
- A. The struct's method is called.
- C. E satisfies the requirement.
- D. hi returns a String.
Question 5Medium
What is the output?
protocol P {}
extension P { func d() -> String { "def" } }
struct S: P {}
print(S().d())
- Compile error
- nil
- def
- S
Answer: C. def
Explanation
Protocol extensions can provide default implementations to all conforming types.
Step-by-step solution
- S conforms to P.
- P extension gives d().
- def.
Why the other options are wrong
- A. Protocol extensions supply default implementations.
- B. The method returns a String.
- D. The default returns "def".
Question 6Medium
Which protocol makes a type usable as a Dictionary key or in a Set?
- Comparable
- Codable
- Hashable
- Identifiable
Answer: C. Hashable
Explanation
Dictionary keys and Set elements must conform to Hashable.
Step-by-step solution
- Dictionary keys and Set elements must be hashable.
- Hashable provides the hash function and equality needed for that.
- So Hashable is the protocol (option C).
Why the other options are wrong
- A. Comparable is for ordering values, not for hashing them.
- B. Codable is for encoding and decoding data.
- D. Identifiable is used by SwiftUI lists to tell items apart.
Tip: Hashable makes a type work as a Dictionary key or Set element.
Question 7Medium
What is the output?
struct V: Comparable {
var n: Int
static func < (a: V, b: V) -> Bool { a.n < b.n }
}
print([V(n: 3), V(n: 1)].sorted().map(\.n))
- [1, 3]
- [3, 1]
- Compile error
- [V(n: 1), V(n: 3)]
Answer: A. [1, 3]
Explanation
Comparable needs only <; sorted uses it and map extracts n.
Step-by-step solution
- Sort by n.
- 1 before 3.
- [1, 3].
Why the other options are wrong
- B. sorted uses <.
- C. Only < is required.
- D. map(\.n) extracts the numbers.
Question 8Medium
What does an extension allow?
- Adding stored properties to a type
- Overriding the type's private members
- Deleting existing methods of a type
- Adding methods or computed properties
Answer: D. Adding methods or computed properties
Explanation
Extensions add functionality but cannot add stored properties.
Step-by-step solution
- An extension adds methods, computed properties, initialisers and more to an existing type.
- It cannot add stored properties or override existing members.
- So option D is correct.
Why the other options are wrong
- A. Stored properties cannot be added in an extension.
- B. Extensions cannot override existing members.
- C. Extensions cannot delete methods.
Tip: Use extensions to organise code and add behaviour to types you do not own.
Question 9Hard
Why would you declare a protocol as protocol P: AnyObject?
- To restrict it to class types, e.g. so a delegate property can be
weak - To make it faster
- To allow structs
- To make every method optional
Answer: A. To restrict it to class types, e.g. so a delegate property can be weak
Explanation
Class-only protocols let you hold references weakly, which avoids retain cycles in delegate patterns.
Step-by-step solution
- weak needs a class.
- AnyObject = class-only.
- Use it for delegates.
Why the other options are wrong
- B. It is about type restrictions.
- C. It excludes structs.
- D. Optional methods need @objc.
Question 10Hard
What is the difference between some View and any View?
- some is meant for classes only while any is meant for structs
- some returns one specific hidden concrete type; any is an existential box of varying types
- They are identical aliases that the compiler treats the same
- any is always faster than some because it avoids generics
Answer: B. some returns one specific hidden concrete type; any is an existential box of varying types
Explanation
Opaque some keeps the concrete type known to the compiler, while any allows heterogeneous values at some cost.
Step-by-step solution
- Some View is an opaque type: one specific concrete type, hidden from the caller.
- Any View is an existential box that can hold different view types.
- So option B is the difference.
Why the other options are wrong
- A. Both forms work with structs and classes; the difference is about type identity.
- C. They are not identical; some keeps the concrete type, any boxes it.
- D. Any is not always faster; it can cost more at runtime.
Tip: Prefer some for performance; use any when the value types vary.
Practise all 16 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 →