Hashtable — how do you check if a specific state name (key) like "Kerala" exists in the table of state→capital entries?

C# Programming Collection Classes Difficulty: Easy
Choose an option
  • A
    t.ContainsKey("Kerala")
  • B
    t.HasValue("Kerala")
  • C
    t.HasKey("Kerala")
  • D
    t.ContainsState("Kerala")
  • E
    t.ContainsValue("Kerala")

Answer

Correct Answer: t.ContainsKey("Kerala")

Explanation

Introduction / Context:Hashtable stores key/value pairs. Efficient membership checks rely on keys, not values.

Concept / Approach:Use ContainsKey to test the presence of a key. Use ContainsValue to search values (less efficient and not appropriate for checking key membership).

Why Other Options Are Wrong:HasValue/HasKey/ContainsState are not Hashtable methods; ContainsValue checks values, not keys.

Final Answer:t.ContainsKey("Kerala")

Discussion & Comments
No comments yet. Be the first to comment!
Join Discussion