Bird
0
0

You want a Swift program that prints "Access Granted" only if a Boolean flag is true at startup. Using @main, which implementation is correct?

hard📝 Application Q8 of 15
Swift - Basics and Runtime
You want a Swift program that prints "Access Granted" only if a Boolean flag is true at startup. Using @main, which implementation is correct?
A<pre>@main struct AccessControl { func main() { if isAllowed { print("Access Granted") } } let isAllowed = true }</pre>
B<pre>@main struct AccessControl { static let isAllowed = true static func main() { if isAllowed { print("Access Granted") } } }</pre>
C<pre>@main class AccessControl { static func main() { print("Access Granted") } }</pre>
D<pre>struct AccessControl { static func main() { if isAllowed { print("Access Granted") } } static let isAllowed = true }</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Confirm @main usage

    The type must be marked with @main and contain a static main() method.
  2. Step 2: Check condition and print

    @main struct AccessControl {
        static let isAllowed = true
        static func main() {
            if isAllowed {
                print("Access Granted")
            }
        }
    }
    correctly uses a static property and static main() to conditionally print.
  3. Step 3: Eliminate incorrect options

    @main struct AccessControl {
        func main() {
            if isAllowed {
                print("Access Granted")
            }
        }
        let isAllowed = true
    }
    uses instance main(), invalid with @main.
    @main class AccessControl {
        static func main() {
            print("Access Granted")
        }
    }
    always prints unconditionally.
    struct AccessControl {
        static func main() {
            if isAllowed {
                print("Access Granted")
            }
        }
        static let isAllowed = true
    }
    lacks @main attribute.
  4. Final Answer:

    @main struct AccessControl {
        static let isAllowed = true
        static func main() {
            if isAllowed {
                print("Access Granted")
            }
        }
    }
    is correct.
  5. Quick Check:

    @main type with static main() and static flag [OK]
Quick Trick: Use static main() and static flag inside @main type [OK]
Common Mistakes:
  • Declaring main() as instance method
  • Omitting @main attribute
  • Ignoring conditional logic in main()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes