A struct is a way to group related data and functions together. It is a custom data type that can hold properties and methods.
Click to reveal answer
beginner
How do you declare a simple struct named Person with a name property of type String?
You write:
struct Person {
var name: String
}
Click to reveal answer
intermediate
Can structs in Swift have functions inside them? How?
Yes, structs can have functions called methods. You define them inside the struct like this:
struct Person {
var name: String
func greet() {
print("Hello, \(name)!")
}
}
Click to reveal answer
beginner
What keyword do you use to create a new instance of a struct?
You use the struct's name followed by parentheses with any required parameters. For example:<br><pre>let person = Person(name: "Alice")</pre>
Click to reveal answer
intermediate
Are structs in Swift value types or reference types? What does that mean?
Structs are value types. This means when you assign or pass a struct, you get a copy, not a reference to the original. Changes to the copy do not affect the original.
Click to reveal answer
Which keyword is used to declare a struct in Swift?
Astruct
Bclass
Cenum
Dtype
✗ Incorrect
The keyword struct is used to declare a struct in Swift.
How do you define a property inside a struct?
Avar propertyName: Type
BpropertyName = Type
Clet propertyName Type
Ddef propertyName: Type
✗ Incorrect
Properties inside structs are declared with var or let followed by the property name and type, like var name: String.
What happens when you assign one struct instance to another variable?
AThe original struct is deleted
BBoth variables point to the same instance
CAn error occurs
DA copy of the struct is created
✗ Incorrect
Structs are value types, so assigning them creates a copy.
Which of these can a Swift struct contain?
AOnly properties
BProperties and methods
COnly methods
DNeither properties nor methods
✗ Incorrect
Structs can contain both properties (data) and methods (functions).
How do you create a new instance of a struct named Car with a property model?
Avar car = Car.model("Tesla")
Blet car = new Car("Tesla")
Clet car = Car(model: "Tesla")
DCar car = Car("Tesla")
✗ Incorrect
You create a new instance by calling the struct's initializer with the property name and value.
Explain how to declare a struct in Swift and include a property and a method.
Think about grouping data and behavior inside one block.
You got /4 concepts.
Describe the difference between structs and classes in Swift regarding value and reference types.
Focus on how data is copied or shared.
You got /4 concepts.
Practice
(1/5)
1. What keyword is used to declare a struct in Swift?
easy
A. struct
B. class
C. function
D. var
Solution
Step 1: Identify the keyword for struct declaration
In Swift, the keyword struct is used to declare a struct type.
Step 2: Differentiate from other keywords
class declares classes, function is not a Swift keyword, and var declares variables.
2. Which of the following is the correct syntax to declare a struct named Person with a property name of type String?
easy
A. struct Person = { var name: String }
B. struct Person (var name: String)
C. struct Person { var name: String }
D. struct Person: var name String
Solution
Step 1: Check struct declaration syntax
Structs use curly braces {} to enclose properties. Properties are declared with var or let inside braces.
Step 2: Validate each option
struct Person { var name: String } uses correct syntax: struct Person { var name: String }. Others misuse parentheses, equal sign, or colons incorrectly.
Final Answer:
struct Person { var name: String } -> Option C
Quick Check:
Struct syntax uses braces and var for properties [OK]
Hint: Use braces {} and var for properties inside struct [OK]
Common Mistakes:
Using parentheses instead of braces
Adding equal sign after struct name
Incorrect colon usage
Missing var keyword
3. What will be the output of this Swift code?
struct Car {
var model: String
func description() -> String {
return "Car model is \(model)"
}
}
let myCar = Car(model: "Tesla")
print(myCar.description())
medium
A. Car model is Tesla
B. Car model is model
C. Tesla
D. Error: Missing initializer
Solution
Step 1: Understand struct with property and method
The struct Car has a property model and a method description() that returns a string including the model.
Step 2: Analyze code execution
Creating myCar with model "Tesla" and calling description() returns "Car model is Tesla" which is printed.
Final Answer:
Car model is Tesla -> Option A
Quick Check:
Method returns string with model value [OK]
Hint: Methods can use properties with \(property) inside strings [OK]
Common Mistakes:
Expecting property name printed literally
Forgetting to call method
Assuming error due to no explicit initializer
Confusing output with just model string
4. Identify the error in this struct declaration:
struct Book {
var title String
var pages: Int
}
medium
A. Missing var keyword before pages
B. Curly braces missing
C. Struct keyword misspelled
D. Missing colon after title
Solution
Step 1: Check property declarations
Each property must have a colon between name and type, e.g., var title: String.
Step 2: Identify the error
The line var title String is missing the colon after title. Other parts are correct.
Final Answer:
Missing colon after title -> Option D
Quick Check:
Property syntax requires colon between name and type [OK]
Hint: Always put colon between property name and type [OK]
Common Mistakes:
Omitting colon between name and type
Forgetting var keyword
Misspelling struct
Missing braces
5. You want to create a struct Rectangle with properties width and height (both Int), and a method area() that returns the area as Int. Which is the correct complete declaration?
hard
A. struct Rectangle {
var width Int
var height Int
func area() -> Int {
return width + height
}
}
B. struct Rectangle {
var width: Int
var height: Int
func area() -> Int {
return width * height
}
}
C. struct Rectangle {
var width: Int
var height: Int
func area() {
width * height
}
}
D. struct Rectangle {
width: Int
height: Int
func area() -> Int {
return width * height
}
}
Solution
Step 1: Check property declarations
Properties must have var keyword and colon between name and type. struct Rectangle {
var width: Int
var height: Int
func area() -> Int {
return width * height
}
} correctly uses var width: Int and var height: Int.
Step 2: Check method syntax and logic
Method area() returns Int and correctly calculates width * height. struct Rectangle {
var width: Int
var height: Int
func area() -> Int {
return width * height
}
} matches this.
Final Answer:
Complete struct with correct properties and method -> Option B
Quick Check:
Properties need var and colon; method returns correct calculation [OK]
Hint: Use var with colon for properties; return correct calculation [OK]
Common Mistakes:
Omitting var keyword
Missing colon after property name
Method missing return type
Incorrect calculation in method
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.