0
0
Swiftprogramming~10 mins

Comparing structs vs classes decision in Swift - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a struct named Person.

Swift
struct [1] {
    var name: String
    var age: Int
}
Drag options to blanks, or click blank then click option'
APerson
Bclass
Cvar
Dfunc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of the struct name.
Forgetting to name the struct.
2fill in blank
medium

Complete the code to create an instance of the struct Person.

Swift
let person = [1](name: "Alice", age: 30)
Drag options to blanks, or click blank then click option'
Afunc
Bclass
Cstruct
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' or 'struct' keywords instead of the struct name.
Trying to call a function that does not exist.
3fill in blank
hard

Fix the error in the code to copy a struct instance correctly.

Swift
var person1 = Person(name: "Bob", age: 25)
var person2 = person1
person2.name = [1]
Drag options to blanks, or click blank then click option'
ACharlie
Bperson1
C"Charlie"
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Assigning a variable name instead of a string.
4fill in blank
hard

Fill both blanks to define a class and create its instance.

Swift
[1] PersonClass {
    var name: String
    init(name: String) {
        self.name = name
    }
}
let person = [2](name: "Dana")
Drag options to blanks, or click blank then click option'
Aclass
Bstruct
CPersonClass
DPersonStruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'struct' instead of 'class' to define a class.
Using the wrong name when creating an instance.
5fill in blank
hard

Fill all three blanks to compare struct and class behavior.

Swift
var struct1 = Person(name: "Eve", age: 40)
var struct2 = struct1
struct2.age [1] 41

var class1 = [2](name: "Frank")
var class2 = class1
class2.name [3] "George"
Drag options to blanks, or click blank then click option'
A=
BPersonClass
C!=
DPersonStruct
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=' for assignment.
Confusing struct and class names.