Bird
0
0

What will be printed by this Swift code?

medium📝 Predict Output Q5 of 15
Swift - Collections
What will be printed by this Swift code?
var scores = ["Alice": 90, "Bob": 85]
scores["Charlie"] = 88
print(scores.count)
A3
B2
C1
D0
Step-by-Step Solution
Solution:
  1. Step 1: Add new key-value pair

    "Charlie" with score 88 is added to the dictionary, increasing its size.
  2. Step 2: Check dictionary count

    Initially 2 entries, after adding one more, count becomes 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Dictionary count reflects number of key-value pairs [OK]
Quick Trick: Dictionary count = number of key-value pairs [OK]
Common Mistakes:
  • Forgetting to add new key
  • Assuming count stays same
  • Confusing count with last value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes