Bird
0
0

What does the Dictionary's removeValue(forKey:) method return if the key exists?

easy📝 Conceptual Q1 of 15
Swift - Collections
What does the Dictionary's removeValue(forKey:) method return if the key exists?
AThe value associated with the removed key
BThe key that was removed
CA Boolean indicating success
DNothing, it returns Void
Step-by-Step Solution
Solution:
  1. Step 1: Understand removeValue(forKey:) behavior

    This method removes the key-value pair and returns the value if the key existed.
  2. Step 2: Check return type

    The method returns an optional value of the removed key, or nil if the key was not found.
  3. Final Answer:

    The value associated with the removed key -> Option A
  4. Quick Check:

    removeValue(forKey:) returns removed value [OK]
Quick Trick: removeValue returns the removed value, not the key or Bool [OK]
Common Mistakes:
  • Thinking it returns the key instead of the value
  • Assuming it returns a Boolean
  • Believing it returns nothing (Void)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes