Prepare for the Blockchain Developer Certification. Master blockchain concepts with flashcards and multiple choice questions. Explanation-based learning for your success!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What must all keys in a mapping have in common in Solidity?

  1. They must be of different types

  2. They must be of the same type

  3. They must be indexed

  4. They must be unique

The correct answer is: They must be of the same type

In Solidity, when you define a mapping, all keys must be of the same type. Mappings are fundamentally understood as a way to associate a unique key with a value, much like hash tables or dictionaries in other programming languages. Using key types that are consistent allows Solidity to efficiently manage and access data within the mapping. For example, if a mapping is defined with an integer type as the key, every entry in that mapping must use integers as keys. This uniformity is crucial because it establishes the structure and allows proper access and manipulation of the data associated with that mapping. In addition to the requirement for key types to be the same, it's important to note that mappings can only be indexed by value types and not by reference types, reinforcing the necessity of consistent types. This design choice optimally supports Solidity's goals of performance and security in smart contract development.