Bird
0
0

You want to create a dictionary where keys are strings and values are functions that take an Int and return a Bool. Which declaration is correct?

hard📝 Application Q8 of 15
Swift - Functions
You want to create a dictionary where keys are strings and values are functions that take an Int and return a Bool. Which declaration is correct?
Avar dict: [String: (Int) -> Bool]
Bvar dict: (String) -> [Int: Bool]
Cvar dict: [Int: (String) -> Bool]
Dvar dict: [String: Bool -> Int]
Step-by-Step Solution
Solution:
  1. Step 1: Understand dictionary key-value types

    Keys are String, values are functions from Int to Bool.
  2. Step 2: Match dictionary syntax

    Dictionary type is [KeyType: ValueType], so [String: (Int) -> Bool] is correct.
  3. Final Answer:

    var dict: [String: (Int) -> Bool] -> Option A
  4. Quick Check:

    Dictionary with function values = [String: (Int) -> Bool] [OK]
Quick Trick: Dictionary values can be function types like any other type [OK]
Common Mistakes:
  • Confusing dictionary and function type syntax
  • Reversing key and value types
  • Incorrect arrow placement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes