Bird
0
0

Which of these variables can hold a nil value in Swift?

easy📝 Conceptual Q2 of 15
Swift - Optionals
Which of these variables can hold a nil value in Swift?
Alet isActive: Bool
Bvar name: String?
Clet score: Double
Dvar age: Int
Step-by-Step Solution
Solution:
  1. Step 1: Identify optional variables

    Only variables declared with a question mark ? can hold nil in Swift.
  2. Step 2: Check each option

    var name: String? is declared as String?, so it can hold nil. Others are non-optional and cannot hold nil.
  3. Final Answer:

    var name: String? -> Option A
  4. Quick Check:

    Optional variable = Can hold nil [OK]
Quick Trick: Only optionals (with ?) can be nil in Swift [OK]
Common Mistakes:
  • Assuming non-optional vars can be nil
  • Forgetting to add ? for optionals
  • Confusing let and var for nil capability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes