Swift Protocols, Generics u0026 Extensions Concepts Quiz Quiz

Explore key concepts in Swift, including protocols, generics, and extensions, through this targeted quiz. Improve your understanding of protocol compliance, generic functions, extension syntax, and related advanced Swift features for efficient and robust code in modern app development.

  1. Protocol Declaration Syntax

    Which keyword is used to declare a protocol in Swift that requires conforming types to implement certain properties or methods?

    1. contract
    2. interface
    3. protocol
    4. delegate

    Explanation: The correct answer is 'protocol'. In Swift, 'protocol' is the keyword used to define a set of requirements that conforming types must implement. Options like 'interface', 'delegate', and 'contract' may be familiar from other languages or design patterns, but they are not used as keywords for declaring protocols in Swift.

  2. Conformance to Multiple Protocols

    How do you specify that a Swift class conforms to two protocols named Drawable and Touchable?

    1. class Shape: Drawable, Touchable {}
    2. class Shape: Drawable; Touchable {}
    3. class Shape(Drawable Touchable) {}
    4. class Shapeu003CDrawable, Touchableu003E {}

    Explanation: The correct answer is 'class Shape: Drawable, Touchable {}'. In Swift, multiple protocol conformance is declared by listing the protocols after a colon, separated by commas. Parentheses, angle brackets, and semicolons as shown in the other options are not correct Swift syntax for protocol conformance.

  3. Optional Protocol Methods

    Under which circumstance can a method in a protocol be declared as optional in Swift?

    1. If the protocol declares a class-only constraint
    2. In any Swift protocol
    3. When the method returns Void
    4. Only if the protocol is marked @objc

    Explanation: Optional requirements in Swift protocols are allowed only if the protocol is marked with '@objc'. This is because optional requirements depend on Objective-C features. Listing 'in any Swift protocol', or based on class-only constraints or return type, does not make a method optional; these are incorrect contexts.

  4. Generic Function Syntax

    What is the correct Swift syntax for declaring a generic function named swapValues that swaps the values of two variables of any type?

    1. generic func swapValues(a: T, b: T)
    2. func swapValuesu003CTu003E(a: T, b: T)
    3. func swapValuesu003CTu003E(a: inout T, b: inout T)
    4. func swapValues(a, b: T)

    Explanation: The correct answer is 'func swapValuesu003CTu003E(a: inout T, b: inout T)'. The 'inout' keyword is necessary to allow a function to change its arguments. The other options either omit 'inout', use non-existent keywords like 'generic', or have syntax issues that do not define a valid generic function in Swift.

  5. Protocol Extension Purpose

    What is a primary use of protocol extensions in Swift?

    1. To declare associated types
    2. To provide default implementations for protocol methods
    3. To allocate memory for stored properties
    4. To prevent protocol inheritance

    Explanation: Protocol extensions enable you to provide default implementations for methods or computed properties required by a protocol. They do not prevent inheritance, declare associated types (which is done inside the protocol itself), or allocate memory for stored properties, which protocols cannot have.

  6. Limitations of Extensions

    Which of the following statements about Swift extensions is correct?

    1. Extensions can inherit from other extensions
    2. Extensions cannot add stored properties to an existing type
    3. Extensions can be used only with structs
    4. Extensions can override existing instance methods

    Explanation: Swift extensions cannot add stored properties to existing types; they can only add computed properties, methods, and protocol conformances. Extensions cannot override existing methods, don't support inheritance, and can be used with classes, enums, and protocols—not just structs.

  7. Generics with Type Constraints

    Which generic function declaration correctly restricts T to be a subclass of Animal?

    1. func adoptu003CTu003E(pet: Animal)
    2. func adopt(pet: T) where T: Animal
    3. func adoptu003CAnimalu003E(pet: Animal)
    4. func adoptu003CT: Animalu003E(pet: T)

    Explanation: The correct way to restrict a generic parameter T to be a subclass of Animal is 'func adoptu003CT: Animalu003E(pet: T)'. Using a 'where' clause is valid but not shown exactly as written in the provided options. Other options either omit generic constraints, misuse parameterization, or do not declare generics at all.

  8. Using Protocols as Types

    If Vehicle is a protocol, which declaration correctly defines a property that holds any conforming type?

    1. var myVehicle: Vehicle
    2. var myVehicle: VehicleProtocol
    3. var myVehicle = Vehicle()
    4. var myVehicle: AnyObjectu003CVehicleu003E

    Explanation: Protocols can be used as types in Swift by declaring a variable or property with the protocol name, as in 'var myVehicle: Vehicle'. Instantiating a protocol or using 'AnyObjectu003CVehicleu003E' and 'VehicleProtocol' don't use the correct Swift syntax or type naming.

  9. Associated Types in Protocols

    How do you specify a placeholder type inside a protocol for conforming types to define later?

    1. use the associatedtype keyword
    2. declare a generic parameter on the protocol
    3. use a class constraint
    4. define a typealias only

    Explanation: The correct method is to use the 'associatedtype' keyword inside a protocol. Declaring generics on protocols is not allowed in the same way as on functions or types, while typealias alone simply renames a type. Class constraints do not specify placeholder types for protocols.

  10. Extending Optional

    Which statement about adding a computed property to all Optional types in Swift using extensions is correct?

    1. It is only allowed for protocol types
    2. It is possible by writing an extension on Optional
    3. It cannot be done due to language restrictions
    4. It requires modifying the source of Optional

    Explanation: Extension of generic types like Optional is allowed in Swift, which permits adding computed properties with 'extension Optional'. Modifying source code is unnecessary and often impossible. There are no language barriers for adding computed properties via extensions. Limiting this capability to protocol types is incorrect.