Bird
0
0

What will this Swift function print when called with checkNumber(5)?

medium📝 Predict Output Q5 of 15
Swift - Control Flow
What will this Swift function print when called with checkNumber(5)?
func checkNumber(_ number: Int?) {
    guard let num = number, num > 10 else {
        print("Number is too small or nil")
        return
    }
    print("Number is \(num)")
}
ANumber is 5
BNumber is 10
CNumber is too small or nil
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate guard condition with input 5

    Guard checks if number is non-nil and greater than 10. 5 is less than 10, so condition fails.
  2. Step 2: Understand guard failure consequences

    Since condition fails, the else block runs, printing "Number is too small or nil" and returning early.
  3. Final Answer:

    Number is too small or nil -> Option C
  4. Quick Check:

    Guard with multiple conditions exits early if any fail [OK]
Quick Trick: Guard with commas means AND conditions must all pass [OK]
Common Mistakes:
  • Thinking guard passes if only one condition is true
  • Ignoring early return on guard failure
  • Assuming output prints number regardless

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes