0
0
Swiftprogramming~5 mins

Main entry point and @main attribute in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the main entry point in a Swift program?
The main entry point is where the program starts running. In Swift, this is usually the main.swift file with top-level code or a type marked with the @main attribute.
Click to reveal answer
beginner
What does the @main attribute do in Swift?
The @main attribute tells Swift which type contains the program's entry point. Swift runs the code inside that type's static func main() method first.
Click to reveal answer
intermediate
How do you define a custom main entry point using @main in Swift?
You create a struct, class, or enum with the @main attribute and add a static func main() method. Swift runs this method when the program starts.
Click to reveal answer
beginner
Can a Swift program have more than one @main attribute?
No, a Swift program can only have one @main attribute. It marks the single entry point for the program.
Click to reveal answer
intermediate
What happens if you don’t use @main in a Swift program?
If you don’t use @main, Swift looks for a top-level main.swift file with code outside any type to start running the program.
Click to reveal answer
What is the purpose of the @main attribute in Swift?
ATo import a module
BTo mark the program's entry point
CTo declare a variable
DTo define a class
Which method must be implemented inside a type marked with @main?
Astatic func start()
Bfunc start()
Cfunc main()
Dstatic func main()
Can a Swift program have multiple @main attributes?
ANo, only one is allowed
BOnly if they are in different modules
COnly if they are in different files
DYes, as many as needed
If no @main attribute is used, how does Swift find the entry point?
AIt looks for a <code>main.swift</code> file with top-level code
BIt uses the first class declared
CIt uses the first function declared
DIt throws an error
Which of these can be marked with @main in Swift?
AOnly classes
BOnly structs
CStructs, classes, or enums
DOnly enums
Explain how the @main attribute works in Swift and how it defines the program's entry point.
Think about how Swift knows where to start running your code.
You got /4 concepts.
    Describe what happens if you write a Swift program without using the @main attribute.
    Consider how Swift finds the start point without @main.
    You got /4 concepts.