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.
Which keyword is used to declare a protocol in Swift that requires conforming types to implement certain properties or methods?
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.
How do you specify that a Swift class conforms to two protocols named Drawable and Touchable?
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.
Under which circumstance can a method in a protocol be declared as optional in Swift?
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.
What is the correct Swift syntax for declaring a generic function named swapValues that swaps the values of two variables of any type?
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.
What is a primary use of protocol extensions in Swift?
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.
Which of the following statements about Swift extensions is correct?
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.
Which generic function declaration correctly restricts T to be a subclass of Animal?
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.
If Vehicle is a protocol, which declaration correctly defines a property that holds any conforming type?
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.
How do you specify a placeholder type inside a protocol for conforming types to define later?
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.
Which statement about adding a computed property to all Optional types in Swift using extensions is correct?
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.