Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In blockchain programming, what happens when you assign one reference type variable to another?
easy
A. The variables become independent and unrelated.
B. A new copy of the data is created.
C. The original data is deleted.
D. Both variables point to the same data location.

Solution

  1. Step 1: Understand reference assignment

    Assigning one reference type variable to another makes both point to the same data location in memory.
  2. Step 2: Effect of changes through references

    Changes made through either variable affect the same original data because they share the reference.
  3. Final Answer:

    Both variables point to the same data location. -> Option D
  4. Quick Check:

    Reference assignment shares data location = A [OK]
Hint: Reference types share data location, not copy data [OK]
Common Mistakes:
  • Thinking assignment copies data
  • Assuming variables become independent
  • Believing original data is deleted
2. Which of the following is the correct syntax to copy a reference type variable in Solidity (blockchain language)?
easy
A. uint[] memory newArray = oldArray;
B. uint[] newArray = oldArray.copy();
C. uint[] storage newArray = oldArray;
D. uint[] newArray = copy(oldArray);

Solution

  1. Step 1: Understand storage vs memory in Solidity

    Storage variables hold data permanently on blockchain, memory variables are temporary copies.
  2. Step 2: Correct way to copy reference

    Assigning a storage reference variable to another storage variable copies the reference, not the data.
  3. Final Answer:

    uint[] storage newArray = oldArray; -> Option C
  4. Quick Check:

    Storage assignment copies reference = B [OK]
Hint: Use 'storage' keyword to copy reference, not 'memory' [OK]
Common Mistakes:
  • Using 'memory' copies data, not reference
  • Assuming .copy() method exists
  • Trying to use undefined copy functions
3. Consider this Solidity code snippet:
uint[] storage arr1 = someArray;
uint[] storage arr2 = arr1;
arr2.push(10);
uint length = arr1.length;

What is the value of length after execution?
medium
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

  1. Step 1: Understand reference assignment

    arr2 is assigned as a storage reference to arr1, so both point to the same array.
  2. Step 2: Effect of push on arr2

    Pushing 10 to arr2 modifies the shared array, increasing its length by 1.
  3. Final Answer:

    Length increases by 1 due to push on arr2. -> Option B
  4. 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

  1. Step 1: Check assignment between memory and storage

    In Solidity, you cannot assign a memory array directly to a storage reference variable.
  2. Step 2: Understand push operation

    Push is allowed on storage arrays, but assignment error occurs before push.
  3. Final Answer:

    Cannot assign memory array to storage reference directly. -> Option A
  4. 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

  1. Step 1: Understand data locations for copying

    To avoid changing original storage array, create a new memory array and copy elements.
  2. Step 2: Modify and return the memory array

    Modify the memory array safely and return it without affecting storage data.
  3. Final Answer:

    Create a new memory array, copy elements from storage, modify it, then return. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard