0
0
Blockchain / Solidityprogramming~20 mins

Reference types behavior in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reference Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Solidity code snippet?

Consider the following Solidity contract snippet. What will be the value of arr[0] after calling modifyArray()?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public arr = [1, 2, 3];

    function modifyArray() public {
        uint[] memory temp = arr;
        temp[0] = 99;
    }
}
A1
BCompilation error
C0
D99
Attempts:
2 left
💡 Hint

Think about whether temp is a reference or a copy.

Predict Output
intermediate
2:00remaining
What happens when modifying a storage reference in Solidity?

Given this Solidity code, what will be the value of arr[0] after modifyArray() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public arr = [1, 2, 3];

    function modifyArray() public {
        uint[] storage temp = arr;
        temp[0] = 99;
    }
}
ACompilation error
B0
C1
D99
Attempts:
2 left
💡 Hint

Consider what storage means for temp.

🔧 Debug
advanced
2:30remaining
Why does this Solidity function revert when modifying a struct?

Examine the code below. Why does calling updatePerson() cause a revert?

Blockchain / Solidity
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;
    }
}
ABecause <code>person</code> is not updated, so revert occurs
BBecause the function tries to modify a copy, not the original, no revert occurs
CBecause <code>memory</code> structs cannot be modified
DBecause <code>string</code> in <code>memory</code> cannot be assigned
Attempts:
2 left
💡 Hint

Think about whether modifying p affects person.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a storage reference to a mapping in Solidity?

Given the mapping mapping(address => uint) balances;, which declaration correctly creates a storage reference to it inside a function?

Amapping(address => uint) memory ref = balances;
Bmapping(address => uint) ref = balances;
Cmapping(address => uint) storage ref = balances;
Dmapping(address => uint) calldata ref = balances;
Attempts:
2 left
💡 Hint

Remember that mappings can only be stored in storage or calldata, not memory.

🚀 Application
expert
3:00remaining
How many items are in the array after this Solidity function runs?

Consider this Solidity contract. After calling addItem(), how many elements does items contain?

Blockchain / Solidity
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
    }
}
ACompilation error
B2
C0
D1
Attempts:
2 left
💡 Hint

Think about which array is modified and which is a copy.