0
0
Swiftprogramming~20 mins

Guard for early exit pattern in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Guard Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift code using guard?
Consider the following Swift function that uses a guard statement. What will be printed when calling checkNumber(5)?
Swift
func checkNumber(_ num: Int) {
    guard num > 10 else {
        print("Number is too small")
        return
    }
    print("Number is big enough")
}

checkNumber(5)
ANumber is big enough
BNumber is too small
CNo output
DCompilation error
Attempts:
2 left
💡 Hint
Remember guard checks a condition and exits early if false.
Predict Output
intermediate
2:00remaining
What will this Swift function print when called with checkName(nil)?
Analyze the function below that uses guard to unwrap an optional String. What is the output of checkName(nil)?
Swift
func checkName(_ name: String?) {
    guard let unwrappedName = name else {
        print("No name provided")
        return
    }
    print("Hello, \(unwrappedName)!")
}

checkName(nil)
ANo name provided
BHello, !
CRuntime error
DHello, nil!
Attempts:
2 left
💡 Hint
Guard unwraps optionals safely.
🔧 Debug
advanced
2:00remaining
Why does this Swift code cause a compile error?
Examine the code below. Why does it fail to compile?
Swift
func validateAge(_ age: Int?) {
    guard age != nil else {
        print("Age is missing")
        return
    }
    print("Age is \(age!)")
}

validateAge(nil)
AMissing return statement inside guard else block
BCannot force unwrap optional age
Cguard condition must be a boolean expression
Dprint statement syntax error
Attempts:
2 left
💡 Hint
Remember guard requires exiting the current scope in else.
Predict Output
advanced
2:00remaining
What is the output of this Swift function using multiple guard statements?
Look at the function below. What will it print when called with processData(5, nil)?
Swift
func processData(_ x: Int?, _ y: Int?) {
    guard let a = x else {
        print("x is nil")
        return
    }
    guard let b = y else {
        print("y is nil")
        return
    }
    print("Sum is \(a + b)")
}

processData(5, nil)
ASum is 5
Bx is nil
CNo output
Dy is nil
Attempts:
2 left
💡 Hint
Each guard checks one optional and exits early if nil.
🧠 Conceptual
expert
2:00remaining
Why is the guard statement preferred for early exit in Swift functions?
Choose the best explanation for why Swift developers use guard statements for early exit.
AGuard statements allow variables to be mutable inside the else block only.
BGuard statements automatically catch runtime errors without needing explicit error handling.
CGuard statements improve code readability by handling error cases early and reducing nested code blocks.
DGuard statements replace the need for if-else statements in all cases.
Attempts:
2 left
💡 Hint
Think about how guard affects code structure and clarity.