Bird
0
0

Find the problem in this Swift code:

medium📝 Debug Q7 of 15
Swift - Optionals
Find the problem in this Swift code:
func printName(_ name: String?) {
    print(name!)
}
printName(nil)
AFunction parameter cannot be optional
BForce unwrapping nil causes runtime crash
Cprint statement syntax is wrong
DNo problem, prints nil
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameter type

    The function accepts an optional string which can be nil.
  2. Step 2: Force unwrap inside function

    Force unwrapping nil inside print causes a runtime crash.
  3. Final Answer:

    Force unwrapping nil causes runtime crash -> Option B
  4. Quick Check:

    Force unwrap nil in function = crash [OK]
Quick Trick: Never force unwrap parameters without nil check [OK]
Common Mistakes:
  • Assuming optional parameters can't be nil
  • Ignoring force unwrap risk inside functions
  • Expecting print to handle nil gracefully

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes