Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Collections
What will be the output of this Swift code?
let capitals = ["France": "Paris", "Japan": "Tokyo"]
print(capitals["Japan"] ?? "Unknown")
AUnknown
BJapan
Cnil
DTokyo
Step-by-Step Solution
Solution:
  1. Step 1: Access value for key "Japan"

    The dictionary has "Japan" as a key with value "Tokyo".
  2. Step 2: Use nil-coalescing operator

    Since the key exists, capitals["Japan"] returns "Tokyo", so "Unknown" is ignored.
  3. Final Answer:

    Tokyo -> Option D
  4. Quick Check:

    Dictionary access returns value or default [OK]
Quick Trick: Use ?? to provide default if key missing [OK]
Common Mistakes:
  • Confusing key and value
  • Ignoring nil-coalescing operator
  • Expecting nil instead of default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes