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?@main, which implementation is correct?@main and contain a static main() method.@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.@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.@main struct AccessControl {
static let isAllowed = true
static func main() {
if isAllowed {
print("Access Granted")
}
}
} is correct.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions