Bird
0
0

What is the issue with this Swift program?

medium📝 Debug Q6 of 15
Swift - Basics and Runtime
What is the issue with this Swift program?
@main struct Sample {
    func main() {
        print("Hello World")
    }
}
AThe program is missing an import statement.
BThe struct cannot be marked with @main.
CThe print statement is invalid inside main().
DThe main() method must be static when using @main.
Step-by-Step Solution
Solution:
  1. Step 1: Check main() method signature

    When using @main, the main() method must be static.
  2. Step 2: Identify error

    Here, main() is an instance method, which is invalid.
  3. Final Answer:

    The main() method must be static when using @main. -> Option D
  4. Quick Check:

    main() must be static with @main [OK]
Quick Trick: main() must be static in @main type [OK]
Common Mistakes:
  • Declaring main() as instance method
  • Assuming @main cannot be on struct
  • Thinking print() is invalid inside main()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes