0
0
Swiftprogramming~20 mins

Comments and documentation markup in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Swift documentation comment usage
What will be the output when this Swift code is run?
Swift
/// Returns the sum of two integers.
/// - Parameters:
///   - a: The first integer
///   - b: The second integer
/// - Returns: The sum of a and b
func add(_ a: Int, _ b: Int) -> Int {
    return a + b
}

print(add(3, 4))
ACompilation error due to comments
BReturns the sum of two integers.
C3 4
D7
Attempts:
2 left
💡 Hint
Documentation comments do not affect runtime output.
Predict Output
intermediate
2:00remaining
Effect of nested multiline comments in Swift
What will be the output of this Swift code snippet?
Swift
/* Outer comment start
/* Inner comment */
Outer comment end */
print("Hello")
AHello
BCompilation error due to nested comments
C/* Inner comment */ Hello
DNo output, program crashes
Attempts:
2 left
💡 Hint
Swift supports nested multiline comments.
Predict Output
advanced
2:00remaining
Output of code with single-line and multiline comments
What is printed when this Swift code runs?
Swift
// This is a single-line comment
let x = 5 /* This is a
multiline comment */
print(x)
ACompilation error due to comment placement
B5
C
let x = 5
5
D0
Attempts:
2 left
💡 Hint
Comments do not affect variable values.
🧠 Conceptual
advanced
1:30remaining
Purpose of documentation markup in Swift
Which option best describes the purpose of documentation markup comments (///) in Swift?
AThey are used to disable code execution temporarily.
BThey cause the compiler to optimize the code better.
CThey generate documentation and help tools show info about code elements.
DThey convert comments into runtime print statements.
Attempts:
2 left
💡 Hint
Think about how IDEs show help for functions.
🔧 Debug
expert
2:30remaining
Identify the error caused by comment misuse
What error will this Swift code produce?
Swift
/* Start comment
// Single line comment inside
print("Hello")
/* Nested comment start
print("World")
*/
End comment */
ANo error, prints nothing
BPrints Hello and World
CSyntax error: unterminated comment
DPrints Hello only
Attempts:
2 left
💡 Hint
Check if all multiline comments are properly closed.