Bird
0
0

Which of the following is the correct way to define the main entry point in Swift using the @main attribute?

easy📝 Syntax Q3 of 15
Swift - Basics and Runtime
Which of the following is the correct way to define the main entry point in Swift using the @main attribute?
A<pre>struct App { @main func main() { print("Hello") } }</pre>
B<pre>@main struct App { static func main() { print("Hello") } }</pre>
C<pre>@main func main() { print("Hello") }</pre>
D<pre>@main class App { func main() { print("Hello") } }</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Recognize valid @main usage

    The @main attribute must be applied to a type (struct, class, or enum) that contains a static main() method.
  2. Step 2: Analyze options

    @main struct App { static func main() { print("Hello") } }
    correctly applies @main to a struct with a static main() method.
  3. Final Answer:

    It designates the type as the program's starting point where execution begins. -> Option B
  4. Quick Check:

    @main on type with static main() method [OK]
Quick Trick: @main must be on a type with static main() [OK]
Common Mistakes:
  • Applying @main to a function instead of a type
  • Declaring main() as instance method instead of static
  • Using @main on a method rather than a type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes