Comments explain the code to humans. They help others (and yourself) understand what the code does without affecting how the program runs.
Click to reveal answer
beginner
How do you write a single-line comment in Swift?
Use two slashes // before the comment text. Everything after // on that line is ignored by the compiler.
Click to reveal answer
beginner
How do you write a multi-line comment in Swift?
Start with /* and end with */. Everything between is treated as a comment, even if it spans many lines.
Click to reveal answer
intermediate
What is documentation markup in Swift and why is it useful?
Documentation markup uses special comment styles (like ///) to create structured notes. These notes can be shown in code editors to explain functions, parameters, and return values clearly.
Click to reveal answer
intermediate
Give an example of a documentation comment for a Swift function.
/// Adds two numbers and returns the result.
/// - Parameters:
/// - a: The first number.
/// - b: The second number.
/// - Returns: The sum of a and b.
Click to reveal answer
Which symbol starts a single-line comment in Swift?
A#
B//
C/*
D<!--
✗ Incorrect
Single-line comments in Swift start with //. Multi-line comments use /* ... */.
What does the following Swift comment style /// indicate?
AA compiler directive
BA single-line comment
CA multi-line comment start
DA documentation comment for code explanation
✗ Incorrect
/// is used for documentation comments that provide structured information about code elements.
How do multi-line comments behave in Swift?
AThey start with <code>/*</code> and end with <code>*/</code>
BThey start with <code>//</code> and end with a blank line
CThey start with <code>///</code> and end with <code>///</code>
DThey start with <code>#</code> and end with <code>#</code>
✗ Incorrect
Multi-line comments in Swift are enclosed between /* and */.
Why should you use documentation comments in your Swift code?
ATo help others understand your code with clear explanations
BTo make the code run faster
CTo hide code from the compiler
DTo change the code's logic
✗ Incorrect
Documentation comments improve code readability and help others (and yourself) understand how to use your code.
Which of these is NOT a valid Swift comment style?
A/// This is a documentation comment
B/* This is a comment */
C<!-- This is a comment -->
D// This is a comment
✗ Incorrect
is an HTML comment style, not valid in Swift.
Explain the difference between single-line, multi-line, and documentation comments in Swift.
Think about how each comment type looks and what it is used for.
You got /3 concepts.
Write a documentation comment for a Swift function that multiplies two numbers and returns the result.
Follow the Swift documentation comment format with clear sections.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of comments in Swift code?
easy
A. To create new variables
B. To make the code run faster
C. To explain the code for better understanding
D. To store user data
Solution
Step 1: Understand what comments do
Comments are notes in the code that do not affect how the program runs but help explain what the code does.
Step 2: Identify the purpose of comments
Comments make the code easier to read and understand for yourself and others who read the code later.
Final Answer:
To explain the code for better understanding -> Option C
Quick Check:
Comments explain code [OK]
Hint: Comments explain code, not run it or store data [OK]
Common Mistakes:
Thinking comments speed up code
Confusing comments with variables
Using comments to store data
2. Which of the following is the correct syntax for a single-line comment in Swift?
easy
A. # This is a comment
B. /* This is a comment */
C.
D. // This is a comment
Solution
Step 1: Recall Swift comment syntax
Swift uses // for single-line comments and /* */ for multi-line comments.
Step 2: Match the correct syntax
// This is a comment uses // which is the correct single-line comment syntax in Swift.
Final Answer:
// This is a comment -> Option D
Quick Check:
Single-line comment = // [OK]
Hint: Single-line comments start with // in Swift [OK]
Common Mistakes:
Using /* */ for single-line comments
Using HTML style comments
Using # which is not a comment in Swift
3. What will the following Swift code output?
/// Adds two numbers
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
print(add(3, 4))
medium
A. 7
B. add(3, 4)
C. /// Adds two numbers
D. Error: Unexpected documentation comment
Solution
Step 1: Understand documentation comments effect
The line starting with /// is a documentation comment and does not affect the code execution.
Step 2: Execute the function call
The function add returns the sum of 3 and 4, which is 7, and print outputs it.
Final Answer:
7 -> Option A
Quick Check:
Documentation comment ignored, output = 7 [OK]
Hint: Documentation comments don't change output, only explain [OK]
Common Mistakes:
Thinking documentation comments print text
Expecting error from documentation comments
Confusing comments with code
4. Identify the error in this Swift code snippet:
/* This is a multi-line comment
func greet() {
print("Hello")
}
// Missing closing comment delimiter
medium
A. Using // inside multi-line comment is invalid
B. Missing closing */ for multi-line comment
C. Function greet() is not declared properly
D. print statement syntax error
Solution
Step 1: Check multi-line comment syntax
Multi-line comments start with /* and must end with */ to close properly.
Step 2: Identify missing closing delimiter
The code has /* but no closing */, so the comment never ends, causing a syntax error.
Final Answer:
Missing closing */ for multi-line comment -> Option B
Quick Check:
Multi-line comment must close with */ [OK]
Hint: Multi-line comments need /* and */ to open and close [OK]
Common Mistakes:
Ignoring missing closing comment delimiter
Thinking // can't be inside multi-line comments
Assuming function syntax is wrong
5. You want to document a Swift function so that Xcode shows its description and parameters clearly. Which comment style should you use?
func multiply(_ x: Int, _ y: Int) -> Int {
return x * y
}
hard
A. /// Multiplies two numbers
/// - Parameters:
/// - x: The first number
/// - y: The second number
/// - Returns: The product of x and y
B. /* Multiplies two numbers */
C. // This function multiplies two numbers
D.
Solution
Step 1: Understand documentation comment usage
Documentation comments in Swift use /// and special markup to describe functions and parameters for tools like Xcode.
Step 2: Identify the detailed documentation style
/// Multiplies two numbers
/// - Parameters:
/// - x: The first number
/// - y: The second number
/// - Returns: The product of x and y uses /// with - Parameters and - Returns tags, which is the correct way to document for Xcode's quick help.
Final Answer:
/// Multiplies two numbers
/// - Parameters:
/// - x: The first number
/// - y: The second number
/// - Returns: The product of x and y -> Option A
Quick Check:
Use /// with parameter tags for documentation [OK]
Hint: Use /// with - Parameters and - Returns for Xcode docs [OK]
Common Mistakes:
Using single-line or multi-line comments without markup
Using HTML style comments
Not including parameter descriptions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.