Swift programs can start with top-level statements in a single file—no boilerplate class required for CLI scripts. Output uses print(), similar to println in Kotlin or print() in JavaScript (console).
Minimal program
print("Hello, World")
print writes a line to stdout with a trailing newline. Semicolons are optional; Swift infers types and prefers clear, readable expressions.
Compared to Java and C#
Java requires public class Main with public static void main. C# often uses top-level statements in modern projects. Swift CLI scripts favor top-level code or an @main attribute on a struct.
Important interview questions and answers
- Q: What prints text in Swift?
A:print(),debugPrint(), and string interpolation with\(value). - Q: Must every Swift file declare a class?
A: No—top-level functions, types, and statements are idiomatic in scripts and playgrounds.
Self-check
- Which function prints to stdout in lessons?
- Are semicolons required at line ends?
Tip: Top-level print() needs no class wrapper—unlike Java public class Main.
Interview prep
- What prints text?
print(), debugPrint(), and string interpolation with \(value).
- Entry point?
Top-level statements or @main on a struct.