Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

Arrays (fixed and dynamic) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Arrays (fixed and dynamic)
Start
↓
Declare Array
↓
Fixed Size?
Yes→Allocate fixed space
↓
Use fixed array
↓
No
Dynamic→Allocate flexible space
↓
Use dynamic array
↓
Access/Modify
↓
End
This flow shows how arrays are declared as fixed or dynamic, allocated accordingly, then used for access or modification.
Execution Sample
Blockchain / Solidity
fixedArray = [10, 20, 30]
dynamicArray = []
dynamicArray.push(40)
dynamicArray.push(50)
print(fixedArray[1])
print(dynamicArray[0])
This code creates a fixed array with 3 elements and a dynamic array where elements are added, then prints specific elements.
Execution Table
StepActionArray StateOperation DetailOutput
1Declare fixedArray[10, 20, 30]Fixed size array with 3 elements allocated
2Declare dynamicArray[]Empty dynamic array created
3dynamicArray.push(40)[40]Added 40 to dynamicArray
4dynamicArray.push(50)[40, 50]Added 50 to dynamicArray
5print(fixedArray[1])[10, 20, 30]Access element at index 120
6print(dynamicArray[0])[40, 50]Access element at index 040
7End[10, 20, 30], [40, 50]Program finished
💡 Program ends after printing requested elements from both arrays.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
fixedArrayundefined[10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30][10, 20, 30]
dynamicArrayundefinedundefined[][40][40, 50][40, 50]
Key Moments - 3 Insights
Why can't we add more elements to fixedArray after declaration?
Because fixedArray is a fixed size array allocated with a set length (3 elements). The execution_table rows 1 and 3 show dynamicArray can grow, but fixedArray stays the same size.
How does dynamicArray grow when we push elements?
dynamicArray starts empty (row 2) and each push adds an element (rows 3 and 4), increasing its size dynamically.
What happens if we try to access an index outside the array length?
Accessing an invalid index would cause an error or return undefined. In the execution_table, only valid indices are accessed (rows 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of dynamicArray after step 3?
A[10, 20, 30]
B[40]
C[]
D[40, 50]
💡 Hint
Check the 'Array State' column at step 3 in execution_table.
At which step does fixedArray get its values assigned?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column for fixedArray declaration in execution_table.
If we try to push an element to fixedArray, what would happen?
AIt will cause an error or fail because fixedArray size is fixed.
BIt will replace the last element.
CIt will add the element successfully.
DIt will convert fixedArray to dynamic.
💡 Hint
Refer to key_moments about fixedArray size and execution_table rows showing dynamicArray growing but fixedArray not.
Concept Snapshot
Arrays store multiple values in order.
Fixed arrays have a set size and cannot grow.
Dynamic arrays can grow by adding elements.
Access elements by index starting at 0.
Fixed arrays allocate fixed space; dynamic arrays allocate flexible space.
Use push() to add elements to dynamic arrays.
Full Transcript
This lesson shows how arrays work in blockchain programming. We start by declaring a fixed array with three numbers. This array cannot change size. Then we create a dynamic array that starts empty. We add two numbers to it using push. We print the second element of the fixed array and the first element of the dynamic array. The fixed array stays the same size, while the dynamic array grows as we add elements. Accessing elements uses their position number, starting at zero. Trying to add elements to a fixed array will cause an error because its size is fixed.

Practice

(1/5)
1.

What is the main difference between a fixed array and a dynamic array in blockchain smart contracts?

easy
A. Fixed arrays have a set size, dynamic arrays can change size during execution.
B. Fixed arrays can only store numbers, dynamic arrays can store any data type.
C. Fixed arrays are faster to access, dynamic arrays cannot be accessed directly.
D. Fixed arrays are stored off-chain, dynamic arrays are stored on-chain.

Solution

  1. Step 1: Understand fixed array size

    Fixed arrays have a size defined when declared and cannot change during contract execution.
  2. Step 2: Understand dynamic array size

    Dynamic arrays can grow or shrink by adding or removing elements during contract execution.
  3. Final Answer:

    Fixed arrays have a set size, dynamic arrays can change size during execution. -> Option A
  4. Quick Check:

    Fixed vs dynamic size = A [OK]
Hint: Remember fixed size means no change, dynamic means flexible size [OK]
Common Mistakes:
  • Thinking fixed arrays can change size
  • Confusing data types with array size
  • Assuming dynamic arrays are slower to access
2.

Which of the following is the correct syntax to declare a dynamic array of uint in Solidity?

?
easy
A. uint numbers[];
B. uint[] numbers;
C. uint numbers();
D. uint[] numbers = new uint[];

Solution

  1. Step 1: Recall Solidity array syntax

    Dynamic arrays are declared with the type followed by empty square brackets: uint[].
  2. Step 2: Check each option

    uint[] numbers; uses correct syntax: uint[] numbers;. uint numbers[]; places brackets after variable name, which is invalid. uint numbers(); uses parentheses, which is wrong. uint[] numbers = new uint[]; tries to initialize without size, which is invalid syntax.
  3. Final Answer:

    uint[] numbers; -> Option B
  4. Quick Check:

    Dynamic array syntax = A [OK]
Hint: Type[] name; is the correct dynamic array syntax [OK]
Common Mistakes:
  • Placing brackets after variable name
  • Using parentheses instead of brackets
  • Trying to initialize without size incorrectly
3.

What will be the output of the following Solidity code snippet?

pragma solidity ^0.8.0;
contract Test {
    uint[] public numbers;
    function addNumbers() public {
        numbers.push(10);
        numbers.push(20);
        numbers.push(30);
    }
    function getSecond() public view returns (uint) {
        return numbers[1];
    }
}

Assuming addNumbers() is called once before getSecond(), what does getSecond() return?

medium
A. 10
B. Index out of range error
C. 30
D. 20

Solution

  1. Step 1: Understand array after push calls

    After calling addNumbers(), the array numbers contains [10, 20, 30].
  2. Step 2: Access the element at index 1

    Arrays in Solidity are zero-indexed, so numbers[1] is the second element, which is 20.
  3. Final Answer:

    20 -> Option D
  4. Quick Check:

    numbers[1] = 20 [OK]
Hint: Remember arrays start at index 0 in Solidity [OK]
Common Mistakes:
  • Confusing index 1 with first element
  • Expecting an error for valid index
  • Mixing up push order
4.

Identify the error in this Solidity code snippet that tries to remove the last element from a dynamic array:

pragma solidity ^0.8.0;
contract RemoveLast {
    uint[] public data = [1, 2, 3];
    function removeLast() public {
        data.pop(1);
    }
}
medium
A. pop() removes the first element, not the last.
B. Array cannot be initialized with values like [1, 2, 3].
C. pop() does not take any arguments; calling pop(1) causes an error.
D. pop() is not available for dynamic arrays.

Solution

  1. Step 1: Check pop() method usage

    In Solidity, pop() removes the last element and takes no arguments.
  2. Step 2: Identify incorrect call

    The code calls data.pop(1); which is invalid syntax and causes a compilation error.
  3. Final Answer:

    pop() does not take any arguments; calling pop(1) causes an error. -> Option C
  4. Quick Check:

    pop() no args = C [OK]
Hint: pop() never takes parameters in Solidity [OK]
Common Mistakes:
  • Passing arguments to pop()
  • Thinking pop() removes first element
  • Believing pop() is unavailable for dynamic arrays
5.

Given a fixed array uint[3] fixedArr = [5, 10, 15]; and a dynamic array uint[] dynamicArr;, which code snippet correctly copies all elements from fixedArr to dynamicArr in Solidity?

hard
A. for (uint i = 0; i < fixedArr.length; i++) { dynamicArr.push(fixedArr[i]); }
B. dynamicArr = fixedArr;
C. dynamicArr = new uint[](fixedArr.length); dynamicArr = fixedArr;
D. dynamicArr.push(fixedArr);

Solution

  1. Step 1: Understand array assignment rules

    In Solidity, fixed-size arrays cannot be directly assigned to dynamic arrays.
  2. Step 2: Copy elements using a loop

    Using a for loop to push each element from fixedArr into dynamicArr is the correct way.
  3. Final Answer:

    for (uint i = 0; i < fixedArr.length; i++) { dynamicArr.push(fixedArr[i]); } -> Option A
  4. Quick Check:

    Copy fixed to dynamic with loop = D [OK]
Hint: Use a loop to push fixed array elements into dynamic array [OK]
Common Mistakes:
  • Trying to assign fixed array directly to dynamic array
  • Using push with whole array instead of elements
  • Reinitializing dynamic array incorrectly
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