Protocols & Extensions Questions with Answers

16 practice questions on Protocols & Extensions (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 16 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

struct P: Equatable { var x: Int }
print(P(x: 1) == P(x: 1))
  1. false
  2. true
  3. Compile error
  4. nil

Answer: B. true

Explanation

Swift auto-synthesises == when all stored properties are Equatable.

Step-by-step solution

  1. P: Equatable.
  2. Same x.
  3. 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. 1 2
  2. 2 2
  3. Compile error
  4. 2 1

Answer: D. 2 1

Explanation

The generic function swaps two variables of the same type.

Step-by-step solution

  1. t = 1.
  2. a = 2.
  3. 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?

  1. A blueprint of methods and properties that types can adopt
  2. A concrete class that cannot be subclassed by other types
  3. A type that stores and caches network data for an app
  4. 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

  1. A protocol lists required methods and properties.
  2. Types adopt the protocol and provide the implementations.
  3. 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())
  1. G
  2. hi
  3. Compile error
  4. nil

Answer: B. hi

Explanation

E conforms to G by implementing hi().

Step-by-step solution

  1. E implements G.
  2. hi() returns "hi".
  3. 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())
  1. Compile error
  2. nil
  3. def
  4. S

Answer: C. def

Explanation

Protocol extensions can provide default implementations to all conforming types.

Step-by-step solution

  1. S conforms to P.
  2. P extension gives d().
  3. 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?

  1. Comparable
  2. Codable
  3. Hashable
  4. Identifiable

Answer: C. Hashable

Explanation

Dictionary keys and Set elements must conform to Hashable.

Step-by-step solution

  1. Dictionary keys and Set elements must be hashable.
  2. Hashable provides the hash function and equality needed for that.
  3. 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. [1, 3]
  2. [3, 1]
  3. Compile error
  4. [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

  1. Sort by n.
  2. 1 before 3.
  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?

  1. Adding stored properties to a type
  2. Overriding the type's private members
  3. Deleting existing methods of a type
  4. 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

  1. An extension adds methods, computed properties, initialisers and more to an existing type.
  2. It cannot add stored properties or override existing members.
  3. 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?

  1. To restrict it to class types, e.g. so a delegate property can be weak
  2. To make it faster
  3. To allow structs
  4. 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

  1. weak needs a class.
  2. AnyObject = class-only.
  3. 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?

  1. some is meant for classes only while any is meant for structs
  2. some returns one specific hidden concrete type; any is an existential box of varying types
  3. They are identical aliases that the compiler treats the same
  4. 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

  1. Some View is an opaque type: one specific concrete type, hidden from the caller.
  2. Any View is an existential box that can hold different view types.
  3. 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 →

Related topics

See all Swift topics · All topics