1. What is protocol composition in Swift?
Protocol composition uses the & operator (for example `Dog & Pet`) to indicate that a type conforms to multiple protocols. This enables flexible type constraints and polymorphism by combining behaviors.
Get the Preplance app for a seamless learning experience. Practice offline, get daily streaks, and stay ahead with real-time interview updates.
Get it on
Google Play
4.9/5 Rating on Store
Swift · Question Set
Advanced Types & Protocols interview questions for placements and exams.
Questions
11
Included in this set
Subject
Swift
Explore more sets
Difficulty
Mixed
Level of this set
Go through each question and its explanation. Use this set as a focused practice pack for Swift.
Protocol composition uses the & operator (for example `Dog & Pet`) to indicate that a type conforms to multiple protocols. This enables flexible type constraints and polymorphism by combining behaviors.
For complete preparation, combine this set with full subject-wise practice for Swift. You can also explore other subjects and sets from the links below.
Generics in Swift let you write code that works with any type, but sometimes you need to restrict those types to those that meet certain requirements (protocol constraints). For example `func sort<T: Comparable>(_ array: [T]) -> [T]` means T must conform to Comparable. /n/n Protocol constraints combine generics and protocols: you define a generic placeholder T that must conform to a protocol (or multiple protocols) and then you can call methods or access properties required by the protocol on T. /n/n An example: `func makeCollection<C: Collection>(_ c: C) -> Int { return c.count }` Here `C` must conform to `Collection` so you know `.count` exists. This combination increases code reuse while preserving type safety.
Protocol extensions in Swift allow you to add default method or property implementations that all conforming types can use or override. This enables sharing behaviour across many types and enhances code reuse.
In Swift a protocol defines a set of methods and properties that a type must implement, but it does not specify how they are implemented. It allows different types (structs, classes, enums) to adopt the protocol and provide their own implementation. This promotes polymorphism and flexible design.
Using `associatedtype` in Swift protocols lets the protocol define a placeholder type (for example `Item`), so that each conforming type can specify a concrete type for that placeholder. It adds flexibility and is often used with generics and protocols.
Opaque return types (for example `func makeShape() -> some Shape`) let a function specify that it returns some type conforming to a protocol without revealing exactly which type. This helps encapsulate implementation details while preserving type safety.
Inside a protocol you can define a `typealias` to give a clearer or shorter name to an associatedtype or other type alias. This improves readability and maintainability of protocol requirements.
Protocol-Oriented Programming in Swift is a paradigm where protocols are used to define behaviours and types implement them, thereby favouring composition and default implementations rather than deep class hierarchies. This leads to more flexible, testable and reusable code.
An `associatedtype` in a protocol defines a placeholder type that the conforming type will specify. It allows the protocol to be generic over some type but hides the actual type until implementation. A generic placeholder (like `T`) in a generic type or function is defined when that code is declared. /n/n Use `associatedtype` when you want your protocol to work with one or more unspecified types but allow different conforming types to specify different underlying types. Use generics when defining functions or types that are parameterised by type. /n/n For example `protocol Container { associatedtype Item; func append(_ item: Item) }` vs `struct Stack<T> { var items: [T]; mutating func push(_ item: T) { items.append(item) } }`. Understanding when to use each demonstrates depth of Swift's type system.
Protocol extensions allow you to provide an implementation of protocol methods and properties that conforming types can use without implementing themselves. This encourages code reuse and cleaner design. /n/n However you must be aware of pitfalls: when a protocol requirement is defined but the default implementation is in an extension, if you call the method via a protocol-typed reference you might get the default implementation rather than the overridden one due to static dispatch. Also overuse of default implementations may blur the line between abstraction and concrete behaviour. /n/n Understanding dispatch rules (static vs dynamic) and when protocol extensions lead to unexpected behaviour is key for interviews.
Generics in Swift let you write flexible, reusable code for functions, classes, structs or enums that can operate on any type while still maintaining type safety. For example `func swap<T>(_ a: inout T, _ b: inout T)` works for any type `T`.