Swift OOP Essentials: Classes vs. Structs Quiz Quiz

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.

  1. Value or Reference?

    When you assign a struct instance to a new variable in Swift, what happens by default?

    1. Both variables refer to the same instance.
    2. The data is only partially copied.
    3. A new independent copy is created.
    4. An error occurs unless you use a class.

    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.

  2. Class Instance Reference

    If you assign a class instance to another variable, what does both variable refer to in Swift?

    1. The variables are completely independent.
    2. Both refer to the same instance.
    3. The original variable is deleted.
    4. A new unique instance is created.

    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.

  3. Inheritance Support

    Which of the following can inherit properties and methods from another type in Swift?

    1. Only classes
    2. Only structs
    3. Neither classes nor structs
    4. Both classes and structs

    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.

  4. Initializer Requirement

    If you define a struct with stored properties and do not provide an init() method, what does Swift automatically provide?

    1. A default constructor that sets all values to zero
    2. An error is thrown
    3. A memberwise initializer
    4. No initializer

    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.

  5. Mutability Differences

    Which statement correctly describes when you can modify properties of a struct instance in Swift?

    1. Only if the property is marked as private
    2. Only if the instance is declared with var
    3. Always, regardless of how the instance is declared
    4. Only within a class

    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.

  6. Keyword Usage

    Which keyword is used to declare a new class type in Swift?

    1. structure
    2. object
    3. class
    4. type

    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.

  7. Deinitialization

    Which Swift type can implement a deinitializer (deinit)?

    1. Only classes
    2. Both classes and structs
    3. Only structs
    4. Neither classes nor structs

    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.

  8. Identity Operators

    Which type allows you to use the === operator to check if two variables reference the same instance?

    1. Struct types only
    2. Protocol types only
    3. Class types only
    4. Both class and struct types

    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.

  9. Default Initializers for Classes

    What happens if you define stored properties with default values in a Swift class and do not write an init method?

    1. All properties get the value nil
    2. Swift provides a default initializer
    3. The program fails to compile
    4. You must always 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.

  10. Value Semantics Example

    If you change a property on a struct that was assigned to two variables, which variable(s) is affected?

    1. All structs change globally
    2. Both variables are updated
    3. Neither variable can be changed
    4. Only the variable you changed

    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.