0
0
Blockchain / Solidityprogramming~10 mins

Memory allocation in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to allocate memory for a new variable in a smart contract.

Blockchain / Solidity
uint256 public value = [1];
Drag options to blanks, or click blank then click option'
Anone
Bnull
Cundefined
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null' or 'undefined' which are not valid in Solidity for uint256 initialization.
2fill in blank
medium

Complete the code to allocate a dynamic array in memory inside a function.

Blockchain / Solidity
function allocateArray() public pure returns (uint256[] memory) {
    uint256[] memory arr = new uint256[]([1]);
    return arr;
}
Drag options to blanks, or click blank then click option'
A5
Bsize
Clength
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names like 'size' or 'length' without defining them.
3fill in blank
hard

Fix the error in the code to correctly allocate memory for a struct array.

Blockchain / Solidity
struct Data {
    uint256 id;
    string name;
}

function createData() public pure returns (Data[] memory) {
    Data[] memory dataArray = new Data[]([1]);
    return dataArray;
}
Drag options to blanks, or click blank then click option'
Asize
BData
C0
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using the struct name or undefined variables as size.
4fill in blank
hard

Fill both blanks to allocate memory for a bytes array and set its length.

Blockchain / Solidity
function allocateBytes() public pure returns (bytes memory) {
    bytes memory b = new bytes([1]);
    b.length = [2];
    return b;
}
Drag options to blanks, or click blank then click option'
A10
B20
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Setting length to a different value than allocated size.
Using invalid sizes like 20 or 15 when allocation is 10.
5fill in blank
hard

Fill all three blanks to allocate memory for a fixed-size array, initialize it, and return the first element.

Blockchain / Solidity
function fixedArray() public pure returns (uint256) {
    uint256[[1]] memory arr;
    arr[[2]] = [3];
    return arr[0];
}
Drag options to blanks, or click blank then click option'
A3
B0
C42
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 or size 1 which is too small.
Assigning a value other than 42.