Solidity Solidity Data Types and Storage 2 — Questions and Answers
Question 1: What data location must be explicitly specified for reference type parameters in Solidity functions?
- heap or stack
- local or global
- memory, storage, or calldata (Correct answer)
- mutable or immutable
Correct answer: memory, storage, or calldata
Reference type parameters must declare a data location — memory, storage, or calldata — so the compiler knows where the data resides.
Question 2: What is the default data location for state variables in a Solidity contract?
- memory
- calldata
- stack
- storage (Correct answer)
Correct answer: storage
State variables are always stored in storage (on-chain persistent storage) by default, which is why they persist between function calls.
Question 3: Which of the following correctly declares a dynamic array of uint256 as a state variable?
- uint256[dynamic] public myArray;
- array<uint256> public myArray;
- dynamic uint256[] public myArray;
- uint256[] public myArray; (Correct answer)
Correct answer: uint256[] public myArray;
Dynamic arrays in Solidity are declared with empty square brackets [], omitting a size to allow the array to grow at runtime.
Question 4: What does a Solidity mapping return for a key that has never been set?
- An error is thrown
- null
- The default zero value for the value type (Correct answer)
- undefined
Correct answer: The default zero value for the value type
Mappings return the default zero value of their value type (e.g., 0 for uint, false for bool) for any key that has not been explicitly set.
Question 5: Which statement about structs in Solidity is correct?
- Structs can only contain value types
- Structs cannot be used as mapping values
- Structs can contain other structs but not directly recursive self-references (Correct answer)
- Structs are always stored in memory
Correct answer: Structs can contain other structs but not directly recursive self-references
Structs can nest other struct types but cannot contain a direct recursive reference to themselves, as that would create an infinitely-sized type.
Question 6: What is the key difference between a fixed-size array and a dynamic array in Solidity?
- Fixed-size arrays can only hold value types
- Dynamic arrays are always cheaper to use
- Fixed-size arrays have their length set at compile time while dynamic arrays can grow (Correct answer)
- Fixed-size arrays are stored in memory while dynamic arrays go to storage
Correct answer: Fixed-size arrays have their length set at compile time while dynamic arrays can grow
Fixed-size arrays have a length determined at compile time, while dynamic arrays can have elements pushed or popped at runtime.
Question 7: Which built-in array operations are available for a dynamic storage array in Solidity?
- remove(index) and insert(index, value)
- splice(start, count) and concat(array)
- push(value) and pop() (Correct answer)
- append(value) and trim()
Correct answer: push(value) and pop()
Dynamic storage arrays in Solidity support push() to append an element and pop() to remove the last element; there is no built-in remove-by-index.
What data location must be explicitly specified for reference type parameters in Solidity functions?