0
0
Swiftprogramming~5 mins

Why classes exist alongside structs in Swift - Quick Recap

Choose your learning style9 modes available
Recall & Review
beginner
What is a key difference between classes and structs in Swift?
Classes support inheritance and reference semantics, while structs do not. Structs have value semantics, meaning each copy is independent.
Click to reveal answer
beginner
Why do classes exist alongside structs in Swift?
Classes exist to provide features like inheritance, reference sharing, and deinitializers, which structs do not support.
Click to reveal answer
beginner
What does it mean that structs have value semantics?
Value semantics means when you assign or pass a struct, a copy is made. Changes to one copy do not affect others.
Click to reveal answer
beginner
What is a real-life example to understand reference semantics of classes?
Think of a class instance like a shared remote control. Multiple people can use the same remote (reference), so changes affect all users.
Click to reveal answer
beginner
Can structs in Swift have methods and properties?
Yes, structs can have methods and properties, but they cannot inherit from other types or have deinitializers.
Click to reveal answer
Which feature is unique to classes in Swift?
AHaving properties
BInheritance
CBeing able to create instances
DHaving methods
What happens when you assign a struct to a new variable?
AThe struct becomes immutable
BBoth variables point to the same instance
CThe original struct is deleted
DA new copy is created
Which of these is true about classes in Swift?
AThey have reference semantics
BThey always create copies on assignment
CThey cannot have methods
DThey cannot have properties
Why might you choose a struct over a class?
AYou want independent copies
BYou need inheritance
CYou want shared state
DYou need deinitializers
Which feature do structs NOT support?
AMethods
BProperties
CDeinitializers
DInitializers
Explain why Swift has both classes and structs. What are the main differences and when would you use each?
Think about how data is shared or copied in your program.
You got /6 concepts.
    Describe a real-life example that helps understand the difference between reference and value semantics in Swift.
    Use everyday objects to explain how data sharing works.
    You got /4 concepts.