struct Point {
var x: Int
var y: Int
}
class Location {
var x: Int
var y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
}
var p1 = Point(x: 1, y: 1)
var p2 = p1
p2.x = 5
var l1 = Location(x: 1, y: 1)
var l2 = l1
l2.x = 5
print(p1.x, l1.x)Structs are value types, so p2 is a copy of p1. Changing p2.x does not affect p1.x.
Classes are reference types, so l2 and l1 refer to the same object. Changing l2.x changes l1.x.
struct CounterStruct {
var count = 0
mutating func change() {
count += 1
}
}
class CounterClass {
var count = 0
func change() {
count += 1
}
}
var cs = CounterStruct()
var cc = CounterClass()
cs.change()
cc.change()
print(cs.count, cc.count)Struct's change() method is marked mutating, so it can modify count.
Class methods can modify properties without mutating keyword.
Both increase count by 1.
Only classes support deinitializers because they are reference types and need cleanup when instances are destroyed.
Structs are value types and do not have deinitializers.
Structs are copied when passed, so modifying the copy does not affect the original.
Classes are passed by reference, so changes affect the original.
Option A correctly defines a computed property with type Int and a getter returning width * height.
Option A defines a method, not a computed property.
Option A has invalid syntax with Int() after property name.
Option A misses the type declaration for area.