📖 Scenario: Imagine you are keeping track of the number of apples you have in a basket. Sometimes you add apples, and sometimes you eat some. You want to write a program that can change the number of apples as you go.
🎯 Goal: You will create a variable to hold the number of apples, change its value, and then print the final number of apples.
📋 What You'll Learn
Create a variable called appleCount and set it to 10
Change the value of appleCount to 15
Print the value of appleCount
💡 Why This Matters
🌍 Real World
Variables like <code>appleCount</code> help keep track of changing information, like inventory or scores in games.
💼 Career
Understanding mutable variables is key for programming tasks where data changes over time, such as in app development or data processing.
Progress0 / 4 steps
1
Create a variable with initial value
Create a variable called appleCount and set it to 10 using var.
Swift
Hint
Use var to create a variable that can change later.
2
Change the variable value
Change the value of the variable appleCount to 15.
Swift
Hint
Just write appleCount = 15 to update the variable.
3
Use the variable in a calculation
Add 5 to the current value of appleCount and store the result back in appleCount.
Swift
Hint
Use appleCount = appleCount + 5 to add 5 to the current value.
4
Print the final value
Print the value of appleCount using print().
Swift
Hint
Use print(appleCount) to show the number of apples.
Practice
(1/5)
1. What does the var keyword do in Swift?
easy
A. It creates a variable whose value can change later.
B. It creates a constant value that cannot change.
C. It defines a function.
D. It declares a type alias.
Solution
Step 1: Understand the purpose of var
The var keyword is used to declare variables that can be changed after they are set.
Step 2: Compare with constants
Unlike let, which creates constants, var allows the value to be updated.
Final Answer:
It creates a variable whose value can change later. -> Option A
Quick Check:
var means mutable variable [OK]
Hint: Remember: var means variable, value can change [OK]
Common Mistakes:
Confusing var with let (constant)
Thinking var creates constants
Mixing var with function declaration
2. Which of the following is the correct way to declare a mutable variable named score with initial value 10 in Swift?
easy
A. let score = 10
B. variable score = 10
C. const score = 10
D. var score = 10
Solution
Step 1: Identify the keyword for mutable variables
In Swift, var declares a variable that can change, so use var score = 10.
Step 2: Check other options
let creates a constant, variable and const are not valid Swift keywords.
Final Answer:
var score = 10 -> Option D
Quick Check:
Mutable variable uses var [OK]
Hint: Use var for variables, let for constants [OK]
Common Mistakes:
Using let instead of var for mutable variables
Using invalid keywords like variable or const
Missing the equals sign
3. What will be the output of this Swift code?
var count = 5
count = 10
print(count)
medium
A. 10
B. 5
C. Error: Cannot assign to constant
D. nil
Solution
Step 1: Understand variable assignment
The variable count is declared with var and initially set to 5, so it can be changed.
Step 2: Update and print value
The value is changed to 10 before printing, so print(count) outputs 10.
Final Answer:
10 -> Option A
Quick Check:
Mutable var updates value correctly [OK]
Hint: var allows value change, so print updated value [OK]
Common Mistakes:
Thinking var acts like let (constant)
Expecting original value instead of updated
Confusing syntax causing errors
4. Identify the error in this Swift code:
var name = "Alice"
name = 42
print(name)
medium
A. Syntax error on variable declaration
B. No error, prints 42
C. Type mismatch error when assigning 42 to name
D. Runtime error when printing
Solution
Step 1: Check variable type inference
Variable name is inferred as String because of initial value "Alice".
Step 2: Assign incompatible type
Assigning an integer 42 to a String variable causes a type mismatch error.
Final Answer:
Type mismatch error when assigning 42 to name -> Option C
Quick Check:
Swift variables keep type, cannot assign different type [OK]
Hint: Variable type fixed by first value, cannot assign different type [OK]
Common Mistakes:
Ignoring type mismatch errors
Thinking var allows any type changes
Confusing syntax error with type error
5. You want to keep track of a player's score that changes during the game. Which Swift code correctly declares and updates this score using var?
1. let score = 0
2. score = 10
3. var score = 0
4. score = 10
5. var score: String
6. score = 10
7. let score: Int
8. score = 10
hard
A. Lines 1 and 2
B. Lines 3 and 4
C. Lines 5 and 6
D. Lines 7 and 8
Solution
Step 1: Identify mutable variable declaration
Lines 3 and 4 declare score with var and assign 0, then update to 10, which is correct.
Step 2: Check other options
Lines 1 and 2 use let (constant), so line 2 causes error. Lines 5 and 6 declare score as String but assign integer 10, causing type mismatch error. Lines 7 and 8 use let constant and then try to assign, causing error.
Final Answer:
Lines 3 and 4 -> Option B
Quick Check:
Use var to declare and update variables [OK]
Hint: Declare with var and assign before update [OK]
Common Mistakes:
Using let for variables that change
Type mismatch with wrong type annotation
Confusing var and let usage
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.