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.


In Solidity, which data structure is used to store a collection of key-value pairs with potentially differing types?

  1. Mapping

  2. Fixed array

  3. Dynamic array

  4. Struct

The correct answer is: Struct

In Solidity, a mapping is specifically designed to store key-value pairs where the keys can be of one type and the values can be of a different type. Each key in a mapping must be unique, allowing for efficient data retrieval based on the key. This structure is particularly useful when you want to associate a specific value with a distinct identifier, such as user addresses linked to balances or other relevant data in a contract. Though structs can combine multiple data types by grouping them together, they do not inherently support key-value pair storage. Instead, they are intended for creating composite types that can hold multiple variables of potentially differing types together into a single entity. This makes mappings the ideal choice for storing collections of key-value pairs with varying types in Solidity. Fixed arrays and dynamic arrays, on the other hand, are linear data structures designed primarily for storing elements of the same type, which does not support the functionality required for key-value pair storage. Therefore, mapping stands out as the appropriate data structure for this scenario.