Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

Reference types behavior in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What are reference types in programming?
Reference types are variables that store the address of the actual data in memory, not the data itself. When you copy a reference type, you copy the address, so both variables point to the same data.
Click to reveal answer
beginner
How does changing a reference type variable affect other variables pointing to the same data?
Changing the data through one reference type variable affects all other variables pointing to the same data because they share the same memory address.
Click to reveal answer
intermediate
In blockchain smart contracts, why is understanding reference types important?
Because smart contracts often manipulate complex data structures, knowing that reference types share data helps avoid unintended changes and bugs in contract state.
Click to reveal answer
beginner
What is the difference between value types and reference types?
Value types store the actual data directly, so copying them duplicates the data. Reference types store a pointer to the data, so copying them duplicates the pointer, not the data.
Click to reveal answer
intermediate
Give an example of a reference type in a blockchain programming language like Solidity.
In Solidity, arrays and structs are reference types when used in storage. For example, a storage array variable points to the actual array in contract storage.
Click to reveal answer
What happens when you assign one reference type variable to another?
AThe data is deleted.
BA new copy of the data is created.
CBoth variables point to the same data in memory.
DThe variables become value types.
Which of the following is a reference type in Solidity?
Auint256
Bbool
Caddress
Dstruct stored in storage
If you modify a reference type variable, what happens to other variables referencing the same data?
AThey see the updated data.
BThey keep the old data.
CThey become invalid.
DThey get deleted.
Why should blockchain developers be careful with reference types?
ABecause reference types are slower.
BBecause changes can unintentionally affect contract state.
CBecause reference types cannot be used in smart contracts.
DBecause reference types use less memory.
Which statement is true about value types?
AThey store the actual data directly.
BThey store a pointer to data.
CThey always share data.
DThey cannot be copied.
Explain how reference types behave when assigned to another variable and how this affects data changes.
Think about how two friends sharing the same book see the same changes.
You got /3 concepts.
    Describe why understanding reference types is important when writing blockchain smart contracts.
    Consider how a small change in shared data can affect the whole contract.
    You got /4 concepts.

      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