Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a struct named Point.
Swift
struct [1] {
var x: Int
var y: Int
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Class' instead of a struct name.
Choosing a shape name unrelated to the example.
✗ Incorrect
The keyword struct defines a value type named Point.
2fill in blank
mediumComplete the code to assign one struct instance to another.
Swift
var p1 = Point(x: 5, y: 10) var p2 = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Creating a new instance instead of copying.
Assigning the variable to itself.
✗ Incorrect
Assigning p1 to p2 copies the value because structs are value types.
3fill in blank
hardFix the error in copying and modifying a struct instance.
Swift
var p1 = Point(x: 3, y: 4) var p2 = p1 p2.[1] = 10
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name not defined in the struct.
Trying to modify the original instance instead of the copy.
✗ Incorrect
The struct Point has properties x and y. Modifying x on the copy is valid.
4fill in blank
hardFill both blanks to create a copy of a struct and change its property.
Swift
var original = Point(x: 1, y: 2) var copy = [1] copy.[2] = 5
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying the original instead of the copy.
Using the wrong property name.
✗ Incorrect
Assigning original to copy creates a value copy. Then modifying x changes only the copy.
5fill in blank
hardFill all three blanks to demonstrate that modifying a copy does not affect the original struct.
Swift
var a = Point(x: 7, y: 8) var b = [1] b.[2] = 0 print(a.[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Modifying the original struct instead of the copy.
Printing the wrong property.
✗ Incorrect
Copy a into b. Change b.x. Printing a.y shows the original value unchanged.