0
0
Swiftprogramming~5 mins

Comparing structs vs classes decision in Swift - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is a key difference between structs and classes in Swift regarding memory management?
Structs are value types, so they are copied when assigned or passed around. Classes are reference types, so they share a single instance in memory.
Click to reveal answer
beginner
When should you choose a struct over a class in Swift?
Choose a struct when you want simple data containers that are copied on assignment, have no inheritance, and are thread-safe by default.
Click to reveal answer
beginner
What feature do classes have that structs do not in Swift?
Classes support inheritance, allowing one class to inherit properties and methods from another. Structs do not support inheritance.
Click to reveal answer
intermediate
How does mutability differ between structs and classes in Swift?
Structs require the 'mutating' keyword to change properties inside methods, and instances must be declared 'var' to be mutable. Classes allow property changes without 'mutating' and can be mutable even if declared with 'let'.
Click to reveal answer
intermediate
Why might you prefer classes when working with shared, mutable state?
Because classes are reference types, multiple parts of your code can share and modify the same instance, which is useful for shared mutable state.
Click to reveal answer
Which of the following is true about Swift structs?
AThey support inheritance.
BThey are copied when assigned to a new variable.
CThey are reference types.
DThey can be mutated even if declared with let.
What keyword is required to modify properties inside a struct method?
Amutating
Bvar
Cchange
Dmutate
Why might you choose a class over a struct?
AYou want value semantics.
BYou want automatic copying.
CYou need inheritance.
DYou want thread safety by default.
If you want multiple parts of your app to share and modify the same data instance, which should you use?
AClass
BStruct
CEnum
DTuple
Which statement about mutability is correct?
AClasses require mutating keyword for property changes.
BClass instances declared with let cannot have their properties changed.
CStruct instances declared with let can have their properties changed.
DStruct methods that modify properties must be marked mutating.
Explain when you would choose a struct versus a class in Swift and why.
Think about how data is shared or copied and if you need inheritance.
You got /5 concepts.
    Describe the impact of using structs on thread safety compared to classes.
    Consider how copying versus sharing affects concurrent access.
    You got /5 concepts.