Hint: Use 'memory' keyword before variable name for memory arrays [OK]
Common Mistakes:
Using 'storage' instead of 'memory' for temporary arrays
Placing 'memory' after variable name incorrectly
Omitting 'new' keyword for dynamic arrays
3. What will be the output of this Solidity function snippet?
function test() public pure returns (uint) {
uint[] memory arr = new uint[](3);
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
return arr[1];
}
medium
A. 10
B. 20
C. 30
D. 0
Solution
Step 1: Analyze array assignments
Array elements are set as arr[0]=10, arr[1]=20, arr[2]=30.
Step 2: Identify returned value
The function returns arr[1], which is 20.
Final Answer:
20 -> Option B
Quick Check:
arr[1] = 20 [OK]
Hint: Remember array indexing starts at 0 [OK]
Common Mistakes:
Confusing index 1 with index 0
Assuming default zero value is returned
Misreading array initialization
4. Identify the error in this Solidity function that tries to copy a storage array to memory:
uint[] public data;
function copyArray() public {
uint[] memory temp = data;
}
medium
A. Cannot assign storage array directly to memory array
B. Missing visibility keyword for storage array
C. Memory arrays cannot be declared inside functions
D. Function must be marked as pure
Solution
Step 1: Understand storage to memory assignment rules
Direct assignment from storage array to memory array is not allowed in Solidity.
Step 2: Identify correct error
The code tries to assign data (storage) directly to temp (memory), causing a compilation error.
Final Answer:
Cannot assign storage array directly to memory array -> Option A
Quick Check:
Storage to memory direct assignment = Error [OK]
Hint: Copy elements manually from storage to memory [OK]
Common Mistakes:
Assuming direct assignment copies array
Ignoring visibility requirements
Marking function pure incorrectly
5. You want to create a function that returns a new memory array containing only the even numbers from a given storage array. Which approach correctly uses memory allocation?
hard
A. Declare a memory array with length zero, push evens dynamically, then return it
B. Declare a storage array, copy evens, then return it
C. Declare a memory array with max length, copy evens manually, then return a sliced array
D. Declare a memory array with the same length as storage, copy evens, then resize the array
Solution
Step 1: Understand memory array limitations
Memory arrays in Solidity have fixed size; cannot push dynamically.
Step 2: Choose correct approach for filtering
Declare a memory array with max length, copy even numbers manually, then return a sliced array with correct size.
Final Answer:
Declare a memory array with max length, copy evens manually, then return a sliced array -> Option C
Quick Check:
Memory arrays fixed size = Declare a memory array with max length, copy evens manually, then return a sliced array [OK]
Hint: Memory arrays fixed size; slice after copying [OK]
Common Mistakes:
Trying to push to memory arrays
Using storage arrays for temporary filtering
Not resizing array before returning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.