Bird
0
0

You want to create a Swift program that prints "Welcome!" only if a condition is true at startup. Using @main, which is the best way to implement this?

hard📝 Application Q15 of 15
Swift - Basics and Runtime
You want to create a Swift program that prints "Welcome!" only if a condition is true at startup. Using @main, which is the best way to implement this?
Astruct App { static func main() { if true { print("Welcome!") } } }
B@main struct App { func main() { if true { print("Welcome!") } } }
C@main struct App { static func main() { if true { print("Welcome!") } } }
D@main func main() { if true { print("Welcome!") } }
Step-by-Step Solution
Solution:
  1. Step 1: Use @main on a type with static main()

    The program must have a type marked with @main and a static main() method to run at startup.
  2. Step 2: Implement conditional print inside static main()

    Inside main(), use the if statement to print "Welcome!" only if the condition is true.
  3. Final Answer:

    @main struct App { static func main() { if true { print("Welcome!") } } } -> Option C
  4. Quick Check:

    @main type + static main() + condition inside main() [OK]
Quick Trick: Always put @main on type with static main() method [OK]
Common Mistakes:
  • Making main() an instance method
  • Forgetting @main attribute on the type
  • Trying to put @main on a function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes