A method is a function that is defined inside a class, struct, or enum. It describes behaviors or actions that instances of that type can perform.
Click to reveal answer
beginner
How do you define a method inside a Swift struct?
You write a function inside the struct's curly braces. For example:
struct Dog { func bark() { print("Woof!") } }
Click to reveal answer
intermediate
What keyword do you use to modify properties inside a method of a struct?
You use the mutating keyword before the method to allow it to change properties of the struct.
Click to reveal answer
beginner
Can methods in Swift have parameters and return values?
Yes! Methods can take parameters and return values just like regular functions. This lets them perform actions using input and give back results.
Click to reveal answer
intermediate
What is the difference between instance methods and type methods in Swift?
Instance methods work on an instance of a type (like a specific dog). Type methods work on the type itself (like all dogs). Type methods use the static keyword. Note: Classes can also use the class keyword for overridable type methods.
Click to reveal answer
How do you call a method named bark() on an instance dog of a struct Dog?
Acall bark on dog
BDog.bark()
Cbark(dog)
Ddog.bark()
✗ Incorrect
You call instance methods using dot notation on the instance: dog.bark()
Which keyword allows a method in a struct to change its properties?
Achanging
Bvar
Cmutating
Dstatic
✗ Incorrect
The mutating keyword lets methods modify properties inside structs.
Where do you define methods in Swift?
AInside classes, structs, or enums
BOnly inside classes
COnly outside any type
DOnly inside functions
✗ Incorrect
Methods are functions inside classes, structs, or enums.
What keyword do you use to define a type method in Swift?
Astatic
Bmutating
Cclass
Dfunc
✗ Incorrect
Type methods use the static keyword to belong to the type itself. Note: Classes can also use the class keyword for overridable type methods.
Can methods in Swift return values?
AOnly type methods can return values
BYes, methods can return values
CNo, methods cannot return values
DOnly instance methods can return values
✗ Incorrect
Methods can have return values just like regular functions.
Explain how to add a method to a Swift struct and how to call it on an instance.
Think about writing a function inside the struct and then using the instance name followed by a dot and the method name.
You got /3 concepts.
Describe the purpose of the mutating keyword in Swift methods.
Remember structs are like copies, so you need special permission to change them inside methods.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of adding methods to a struct or class in Swift?
easy
A. To store more data inside the type
B. To make the type immutable
C. To change the type to a different type
D. To add actions or behaviors related to the data inside the type
Solution
Step 1: Understand what methods do
Methods are functions inside a struct or class that perform actions related to that type's data.
Step 2: Identify the purpose of methods
Methods organize code by grouping behaviors with the data they act on, making code reusable and clear.
Final Answer:
To add actions or behaviors related to the data inside the type -> Option D
Quick Check:
Methods = actions on data [OK]
Hint: Methods are functions inside types that do things with data [OK]
Common Mistakes:
Thinking methods only store data
Confusing methods with properties
Believing methods change the type itself
2.
Which of the following is the correct way to define a method named greet inside a Swift struct?
struct Person {
// Your method here
}
easy
A. function greet() { print("Hello") }
B. method greet() { print("Hello") }
C. func greet() { print("Hello") }
D. def greet() { print("Hello") }
Solution
Step 1: Recall Swift method syntax
In Swift, methods inside structs or classes use the keyword func followed by the method name and parentheses.
Step 2: Check each option
func greet() { print("Hello") } uses func greet(), which is correct. The other options use incorrect keywords not valid in Swift.
Final Answer:
func greet() { print("Hello") } -> Option C
Quick Check:
Swift methods start with func [OK]
Hint: Use 'func' keyword to define methods in Swift [OK]
Common Mistakes:
Using 'method', 'def', or 'function' instead of 'func'
Missing parentheses after method name
Not placing method inside struct or class
3.
What will be the output of the following Swift code?
struct Counter {
var count = 0
mutating func increment() {
count += 1
}
}
var c = Counter()
c.increment()
print(c.count)
medium
A. 1
B. Error: Cannot mutate property in method
C. 0
D. nil
Solution
Step 1: Understand the struct and method
The struct has a variable count starting at 0. The method increment adds 1 to count. It is marked mutating to allow changing the struct's data.
Step 2: Trace the code execution
We create c as a Counter instance with count = 0. Calling c.increment() increases count to 1. Printing c.count outputs 1.
Final Answer:
1 -> Option A
Quick Check:
Mutating method changes count from 0 to 1 [OK]
Hint: Mutating methods can change struct properties [OK]
Common Mistakes:
Forgetting 'mutating' keyword causes error
Expecting count to stay 0 after increment
Confusing structs with classes (no mutating needed)
4.
Identify the error in this Swift code and choose the fix: