0
0
Swiftprogramming~10 mins

Class declaration syntax in Swift - Interactive Code Practice

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

Complete the code to declare a simple class named Car.

Swift
class [1] {
}
Drag options to blanks, or click blank then click option'
AAuto
BCar
Cvehicle
Dcar
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for the class.
Using a different class name than requested.
2fill in blank
medium

Complete the code to add a property color of type String inside the class.

Swift
class Vehicle {
    var [1]: String
}
Drag options to blanks, or click blank then click option'
Acolor
Bsize
Cspeed
Dweight
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different property name than color.
Forgetting the colon after the property name.
3fill in blank
hard

Fix the error in the class declaration by completing the missing keyword.

Swift
[1] Car {
    var model: String
}
Drag options to blanks, or click blank then click option'
Aclass
Bfunc
Cstruct
Denum
Attempts:
3 left
💡 Hint
Common Mistakes
Using struct or func instead of class.
Omitting the keyword entirely.
4fill in blank
hard

Fill both blanks to declare a class Person with a method greet() that prints a greeting.

Swift
class [1] {
    func [2]() {
        print("Hello!")
    }
}
Drag options to blanks, or click blank then click option'
APerson
Bgreet
CsayHello
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class or method names.
Forgetting parentheses after method name.
5fill in blank
hard

Fill all three blanks to declare a class Animal with a property name of type String and an initializer that sets the name.

Swift
class [1] {
    var [2]: String
    init([3]: String) {
        self.name = [3]
    }
}
Drag options to blanks, or click blank then click option'
AAnimal
Bname
Dspecies
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for property and initializer parameter.
Forgetting to use self to assign the property.