Challenge your understanding of Swift's object-oriented programming principles with a focus on classes and structs, key differences, basic syntax, and common use cases. Perfect for beginners aiming to strengthen their grasp of Swift's foundational types and modeling techniques.
When you assign a struct instance to a new variable in Swift, what happens by default?
Explanation: Structs in Swift are value types, so assigning a struct to another variable creates a new independent copy. The distractors are incorrect because reference semantics (both variables referring to the same instance) only apply to classes, not structs. No partial copying or assignment errors occur in this scenario.
If you assign a class instance to another variable, what does both variable refer to in Swift?
Explanation: Classes are reference types in Swift, so both variables point to the same instance. Therefore, changing a property using one variable affects the other. The distractors are incorrect because only structs, not classes, result in unique independent copies, and simple assignment does not delete variables.
Which of the following can inherit properties and methods from another type in Swift?
Explanation: In Swift, only classes support inheritance, letting you create a subclass from a superclass. Structs do not support inheritance, so the option 'Both classes and structs' is incorrect. 'Only structs' and 'Neither classes nor structs' are also incorrect because these statements misrepresent Swift's type system.
If you define a struct with stored properties and do not provide an init() method, what does Swift automatically provide?
Explanation: Swift automatically gives structs a memberwise initializer when you do not provide one. No error occurs, and there is no default constructor that sets all values to zero. Classes do not get this automatic memberwise initializer, and 'No initializer' is incorrect for structs.
Which statement correctly describes when you can modify properties of a struct instance in Swift?
Explanation: You can only modify a struct's properties if the struct is declared using var, making it mutable. If declared with let, the struct is immutable. The other options are incorrect because properties cannot always be changed regardless of the declaration, property access control does not affect this, and modifying structs within a class is unrelated.
Which keyword is used to declare a new class type in Swift?
Explanation: Swift uses the 'class' keyword to declare a class type. The distractors 'structure', 'type', and 'object' are not valid keywords for declaring classes; 'struct' would be the keyword for structs, but 'structure' is not used.
Which Swift type can implement a deinitializer (deinit)?
Explanation: Only classes can have deinitializers (deinit blocks) in Swift, which are called when an instance is deallocated. Structs, being value types, do not support deinitialization. 'Both classes and structs' is incorrect, and the other options misrepresent Swift's memory management.
Which type allows you to use the === operator to check if two variables reference the same instance?
Explanation: The === operator in Swift checks for reference identity and can only be used with class instances. Structs are value types and do not have identity, so the operator does not apply. Protocol types do not have identity unless the protocol is class-constrained; thus, the option 'Class types only' is correct.
What happens if you define stored properties with default values in a Swift class and do not write an init method?
Explanation: When all class properties have default values, Swift provides a default initializer. The program compiles successfully, so the option 'The program fails to compile' is incorrect. Properties only become nil if optional, and writing an init method is not mandatory in this scenario.
If you change a property on a struct that was assigned to two variables, which variable(s) is affected?
Explanation: Structs are value types in Swift, so modifying a property affects only the variable you change. The other variable remains unchanged, as it holds its own copy. The ideas that both are updated or all structs are changed globally are incorrect and describe reference semantics or nonexistent behaviors.