Bird
0
0

Consider this Swift function:

medium📝 Debug Q7 of 15
Swift - Optionals
Consider this Swift function:
func checkInput(_ input: String?) {
    guard let value = input else {
        print("No input provided")
        return
    }
    print("Input: \(value)")
}

What will happen if checkInput(nil) is called?
AIt prints "No input provided" and exits the function early.
BIt prints "Input: nil" and continues execution.
CIt causes a runtime error due to force unwrapping.
DIt skips the guard statement and prints nothing.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze guard let behavior

    If input is nil, the else block executes.
  2. Step 2: What does the else block do?

    It prints "No input provided" and returns, exiting the function early.
  3. Final Answer:

    It prints "No input provided" and exits the function early. -> Option A
  4. Quick Check:

    Guard else block runs on nil input [OK]
Quick Trick: Guard else runs and returns on nil [OK]
Common Mistakes:
  • Assuming it prints "Input: nil"
  • Expecting a runtime error from guard let
  • Thinking guard lets code continue after nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes