Consider the following Solidity contract snippet. What will be the value of arr[0] after calling modifyArray()?
pragma solidity ^0.8.0; contract Test { uint[] public arr = [1, 2, 3]; function modifyArray() public { uint[] memory temp = arr; temp[0] = 99; } }
Think about whether temp is a reference or a copy.
In Solidity, memory creates a copy of the array. Modifying temp does not affect the original arr. So, arr[0] remains 1.
Given this Solidity code, what will be the value of arr[0] after modifyArray() is called?
pragma solidity ^0.8.0; contract Test { uint[] public arr = [1, 2, 3]; function modifyArray() public { uint[] storage temp = arr; temp[0] = 99; } }
Consider what storage means for temp.
temp is a storage reference to arr. Modifying temp changes arr directly. So, arr[0] becomes 99.
Examine the code below. Why does calling updatePerson() cause a revert?
pragma solidity ^0.8.0; contract People { struct Person { string name; uint age; } Person public person = Person("Alice", 30); function updatePerson() public { Person memory p = person; p.age = 31; } }
Think about whether modifying p affects person.
The function modifies a memory copy of person. This does not affect the stored person. No revert happens; the change is simply lost.
Given the mapping mapping(address => uint) balances;, which declaration correctly creates a storage reference to it inside a function?
Remember that mappings can only be stored in storage or calldata, not memory.
Mappings cannot be stored in memory. Only storage or calldata are allowed. To create a reference, storage is required.
Consider this Solidity contract. After calling addItem(), how many elements does items contain?
pragma solidity ^0.8.0; contract Inventory { string[] public items; function addItem() public { string[] storage ref = items; ref.push("apple"); string[] memory copy = ref; // copy.push("banana"); // This line causes a compilation error } }
Think about which array is modified and which is a copy.
ref is a storage reference to items, so pushing "apple" adds to items. copy is a memory copy; pushing "banana" to it does not affect items. However, pushing to a memory array is not allowed and causes a compilation error.