Challenge - 5 Problems
Swift Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Documentation comments do not affect runtime output.
✗ Incorrect
The triple slash comments (///) are used for documentation and do not affect the program execution. The function adds two numbers and prints the result.
❓ Predict Output
intermediate2: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")
Attempts:
2 left
💡 Hint
Swift supports nested multiline comments.
✗ Incorrect
Swift allows nested multiline comments, so the entire comment block is ignored and the print statement runs normally.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Comments do not affect variable values.
✗ Incorrect
Comments are ignored by the compiler. The variable x is assigned 5 and printed.
🧠 Conceptual
advanced1:30remaining
Purpose of documentation markup in Swift
Which option best describes the purpose of documentation markup comments (///) in Swift?
Attempts:
2 left
💡 Hint
Think about how IDEs show help for functions.
✗ Incorrect
Documentation comments provide structured info that tools use to generate docs and show quick help, but do not affect code execution.
🔧 Debug
expert2: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 */Attempts:
2 left
💡 Hint
Check if all multiline comments are properly closed.
✗ Incorrect
Swift allows nesting of multiline comments. The outer comment properly closes at the final */. The entire code is commented out, so it compiles with no error and produces no output.