A. Length is unchanged from original someArray length.
B. Length increases by 1 due to push on arr2.
C. Length decreases by 1 due to push on arr2.
D. Code causes a compilation error.
Solution
Step 1: Understand reference assignment
arr2 is assigned as a storage reference to arr1, so both point to the same array.
Step 2: Effect of push on arr2
Pushing 10 to arr2 modifies the shared array, increasing its length by 1.
Final Answer:
Length increases by 1 due to push on arr2. -> Option B
Quick Check:
Shared reference means push affects both = C [OK]
Hint: Changes via one reference affect all references [OK]
Common Mistakes:
Thinking arr1 length stays same
Assuming arr2 is a copy
Ignoring reference sharing behavior
4. Identify the error in this Solidity code snippet:
function updateArray(uint[] memory arr) public { uint[] storage ref = arr; ref.push(5); }
medium
A. Cannot assign memory array to storage reference directly.
B. Cannot push to a storage array.
C. Function must be declared view.
D. No error; code runs fine.
Solution
Step 1: Check assignment between memory and storage
In Solidity, you cannot assign a memory array directly to a storage reference variable.
Step 2: Understand push operation
Push is allowed on storage arrays, but assignment error occurs before push.
Final Answer:
Cannot assign memory array to storage reference directly. -> Option A
Quick Check:
Memory to storage assignment invalid = A [OK]
Hint: Memory and storage types cannot be assigned directly [OK]
Common Mistakes:
Assuming memory can be assigned to storage
Thinking push is disallowed on storage
Ignoring data location mismatch
5. You want to create a function in Solidity that returns a modified copy of a storage array without changing the original. Which approach correctly uses reference types to achieve this?
hard
A. Create a new memory array, copy elements from storage, modify it, then return.
B. Assign storage array to a storage reference, modify it, then return.
C. Return the storage array directly after modifying it.
D. Use a global variable to store the modified array.
Solution
Step 1: Understand data locations for copying
To avoid changing original storage array, create a new memory array and copy elements.
Step 2: Modify and return the memory array
Modify the memory array safely and return it without affecting storage data.
Final Answer:
Create a new memory array, copy elements from storage, modify it, then return. -> Option A
Quick Check:
Use memory copy to avoid changing storage = D [OK]
Hint: Copy to memory, modify, then return to keep original safe [OK]
Common Mistakes:
Modifying storage reference directly
Returning storage array after modification
Using global variables unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.