Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Arrays (fixed and dynamic) in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of fixed-size array initialization
What is the output of this Solidity code snippet when the function getArray() is called?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint[3] fixedArray = [1, 2, 3];
    function getArray() public view returns (uint[3] memory) {
        return fixedArray;
    }
}
ACompilation error
B[0, 0, 0]
C[1, 2, 3]
D[1, 2]
Attempts:
2 left
💡 Hint
Fixed-size arrays in Solidity hold the exact number of elements declared.
❓ Predict Output
intermediate
2:00remaining
Dynamic array length after push operations
What is the length of the dynamic array dynamicArray after executing the addElements() function?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint[] public dynamicArray;
    function addElements() public {
        dynamicArray.push(10);
        dynamicArray.push(20);
        dynamicArray.push(30);
    }
    function getLength() public view returns (uint) {
        return dynamicArray.length;
    }
}
ACompilation error
B0
C2
D3
Attempts:
2 left
💡 Hint
Each push adds one element to the dynamic array.
🔧 Debug
advanced
2:00remaining
Identify the error in fixed-size array assignment
What error will this Solidity code produce when compiled?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint[2] fixedArray;
    function setArray() public {
        fixedArray = [1, 2, 3];
    }
}
ATypeError: Type uint256[3] memory is not implicitly convertible to expected type uint256[2] storage pointer
BRuntime error: Array index out of bounds
CNo error, compiles and runs fine
DSyntaxError: Missing semicolon
Attempts:
2 left
💡 Hint
Fixed-size arrays must match the exact length when assigned.
❓ Predict Output
advanced
2:00remaining
Output after deleting an element from dynamic array
What is the content of dynamicArray after calling removeFirst()?
Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint[] public dynamicArray;
    constructor() {
        dynamicArray.push(5);
        dynamicArray.push(10);
        dynamicArray.push(15);
    }
    function removeFirst() public {
        delete dynamicArray[0];
    }
    function getArray() public view returns (uint[] memory) {
        return dynamicArray;
    }
}
A[5, 10, 15]
B[0, 10, 15]
C[10, 15]
DRuntime error: Cannot delete element
Attempts:
2 left
💡 Hint
Deleting an element sets it to default value but does not change array length.
🧠 Conceptual
expert
2:00remaining
Gas cost behavior of fixed vs dynamic arrays
Which statement correctly describes the gas cost difference between fixed-size and dynamic arrays in Solidity when storing data on-chain?
AFixed-size arrays always cost less gas to store because their size is known at compile time.
BBoth fixed-size and dynamic arrays cost the same gas regardless of size.
CDynamic arrays always cost less gas because they can resize and use less storage.
DDynamic arrays cost more gas when resizing, but fixed-size arrays cost more gas when accessing elements.
Attempts:
2 left
💡 Hint
Knowing size at compile time helps optimize storage layout.

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