Challenge - 5 Problems
Array Mastery in Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Fixed-size arrays in Solidity hold the exact number of elements declared.
✗ Incorrect
The fixed-size array
fixedArray is initialized with three elements: 1, 2, and 3. The function returns this array as is.❓ Predict Output
intermediate2: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; } }
Attempts:
2 left
💡 Hint
Each push adds one element to the dynamic array.
✗ Incorrect
The
addElements() function pushes three elements, so the length becomes 3.🔧 Debug
advanced2: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]; } }
Attempts:
2 left
💡 Hint
Fixed-size arrays must match the exact length when assigned.
✗ Incorrect
The code tries to assign an array of length 3 to a fixed-size array of length 2, which causes a type mismatch error.
❓ Predict Output
advanced2: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; } }
Attempts:
2 left
💡 Hint
Deleting an element sets it to default value but does not change array length.
✗ Incorrect
The
delete keyword sets the first element to zero but keeps the array length unchanged.🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Knowing size at compile time helps optimize storage layout.
✗ Incorrect
Fixed-size arrays have a known size, allowing Solidity to optimize storage and reduce gas costs compared to dynamic arrays which require extra gas for resizing and length management.