Semicolons in Swift are used to separate multiple statements on the same line, but they are optional at the end of a line if there is only one statement.
Click to reveal answer
beginner
Can you write two Swift statements on the same line without semicolons?
No, when writing two or more statements on the same line, you must separate them with semicolons.
Click to reveal answer
beginner
Is this Swift code valid? <br> <code>let a = 5<br>let b = 10</code>
Yes, this code is valid because each statement is on its own line, so semicolons are not needed.
Click to reveal answer
intermediate
How do semicolons affect readability in Swift code?
Using semicolons to put multiple statements on one line can make code harder to read. Swift encourages writing one statement per line without semicolons for clarity.
Click to reveal answer
beginner
Write a Swift line with two statements using semicolons.
Example: <br> <code>let x = 3; let y = 4</code> <br> Semicolons separate the two statements on the same line.
Click to reveal answer
In Swift, when are semicolons required?
AOnly inside functions
BAt the end of every line
CWhen writing multiple statements on the same line
DNever required
✗ Incorrect
Semicolons are required only when you put more than one statement on the same line.
Is this Swift code valid? let a = 1; let b = 2
ANo, semicolons are not allowed
BYes, semicolons separate statements on the same line
CNo, statements must be on separate lines
DYes, but semicolons are optional here
✗ Incorrect
This code is valid because semicolons separate two statements on the same line.
What happens if you omit semicolons when writing multiple statements on the same line in Swift?
AThe statements are combined into one
BThe code compiles fine
CThe second statement is ignored
DThe compiler throws an error
✗ Incorrect
Omitting semicolons between multiple statements on the same line causes a compiler error.
Which style does Swift encourage for better readability?
AOne statement per line without semicolons
BUsing semicolons at the end of every line
CNo semicolons and multiple statements per line
DMultiple statements per line with semicolons
✗ Incorrect
Swift encourages writing one statement per line without semicolons for clarity.
Can you write this Swift code without semicolons? let x = 10; let y = 20
AYes, by putting each statement on its own line
BNo, semicolons are mandatory here
CYes, by removing the second statement
DNo, semicolons are always required
✗ Incorrect
You can remove semicolons by placing each statement on a separate line.
Explain when semicolons are needed in Swift and why they are usually optional.
Think about how Swift treats statements on separate lines versus same line.
You got /3 concepts.
Write an example of Swift code with two statements on the same line using semicolons, and then rewrite it without semicolons on separate lines.
Use let statements for simple variables.
You got /2 concepts.
Practice
(1/5)
1. In Swift, when are semicolons required at the end of statements?
easy
A. Never, semicolons are not used in Swift
B. Always at the end of every statement
C. Only after function declarations
D. Only when multiple statements are on the same line
Solution
Step 1: Understand Swift statement endings
Swift allows statements to end without semicolons if each is on its own line.
Step 2: Identify when semicolons are necessary
Semicolons are needed only to separate multiple statements on the same line.
Final Answer:
Only when multiple statements are on the same line -> Option D
Quick Check:
Semicolons required = multiple statements on one line [OK]
Hint: Semicolons separate statements on the same line only [OK]
Common Mistakes:
Thinking semicolons are always required
Believing semicolons are needed after every statement
Assuming semicolons are never used
2. Which of the following Swift code snippets is syntactically correct?
easy
A. let a = 5; let b = 10;
B. let a = 5 let b = 10;
C. let a = 5; let b = '10'
D. let a = 5 let b = 10
Solution
Step 1: Check semicolon usage on same line
When two statements are on the same line, they must be separated by a semicolon.
Step 2: Verify each option
let a = 5; let b = 10; correctly uses a semicolon between two statements on the same line.
Final Answer:
let a = 5; let b = 10; -> Option A
Quick Check:
Multiple statements same line = semicolon needed [OK]
Hint: Use semicolons only to separate statements on the same line [OK]
Common Mistakes:
Omitting semicolon between statements on the same line
Adding semicolons unnecessarily at line ends
Confusing semicolon placement with other languages
3. What is the output of this Swift code?
let x = 3; let y = 4
print(x + y)
medium
A. Syntax error
B. 34
C. 7
D. Runtime error
Solution
Step 1: Analyze semicolon usage
The semicolon separates two statements on the same line, which is valid in Swift.
Step 2: Calculate the print output
x is 3 and y is 4, so x + y equals 7, which is printed.
Final Answer:
7 -> Option C
Quick Check:
3 + 4 = 7 [OK]
Hint: Semicolons separate statements; addition works normally [OK]
Common Mistakes:
Thinking semicolon causes syntax error
Confusing concatenation with addition
Expecting runtime error due to semicolon
4. Identify the error in this Swift code:
let a = 10 let b = 20
print(a + b)
medium
A. Variable names are invalid
B. Missing semicolon between statements on the same line
C. Missing parentheses in print statement
D. No error, code runs fine
Solution
Step 1: Check statement separation
Two statements are on the same line without a semicolon separating them, which is invalid in Swift.
Step 2: Confirm print syntax and variable names
Print statement and variable names are correct; only missing semicolon causes error.
Final Answer:
Missing semicolon between statements on the same line -> Option B
Quick Check:
Multiple statements same line need semicolon [OK]
Hint: Add semicolon between statements on the same line [OK]
Common Mistakes:
Ignoring missing semicolon error
Assuming print needs no parentheses
Thinking variable names cause error
5. You want to write two Swift statements on the same line: let name = "Alice" and print(name). Which is the correct way to write this?
hard
A. let name = "Alice"; print(name)
B. let name = 'Alice'; print(name);
C. let name = "Alice" print(name);
D. let name = "Alice" print(name)
Solution
Step 1: Understand semicolon use on same line
When writing multiple statements on the same line, separate them with a semicolon.
Step 2: Check each option for correctness
let name = "Alice"; print(name) correctly separates the two statements with a semicolon and omits unnecessary semicolon at the end.
Final Answer:
let name = "Alice"; print(name) -> Option A
Quick Check:
Separate statements on one line with semicolon [OK]
Hint: Use one semicolon between statements on the same line [OK]
Common Mistakes:
Omitting semicolon between statements
Adding extra semicolon after last statement unnecessarily
Writing statements without separation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.