What is a fixed-size array in blockchain programming?
A fixed-size array is an array with a set number of elements that cannot change after declaration. Its size is known at compile time.
Click to reveal answer
beginner
How does a dynamic array differ from a fixed array?
A dynamic array can grow or shrink during runtime, allowing flexible storage of elements, unlike fixed arrays which have a constant size.
Click to reveal answer
beginner
In Solidity, how do you declare a fixed-size array of 5 unsigned integers?
You declare it like this: uint[5] fixedArray; This creates an array that holds exactly 5 unsigned integers.
Click to reveal answer
beginner
How do you add an element to a dynamic array in Solidity?
You use the push() method. For example: dynamicArray.push(10); adds the value 10 to the end of the array.
Click to reveal answer
intermediate
Why are fixed-size arrays more gas efficient than dynamic arrays in blockchain?
Fixed-size arrays have a known size at compile time, so they use less storage and gas. Dynamic arrays require extra gas to manage resizing and storage.
Click to reveal answer
Which of the following is a correct way to declare a dynamic array of addresses in Solidity?
Aaddress[] dynamicAddresses;
Baddress[5] dynamicAddresses;
Cdynamic address[];
Daddress dynamic[];
✗ Incorrect
Option A declares a dynamic array of addresses correctly using address[].
What happens if you try to add an element beyond the fixed size of a fixed array?
AThe element is added successfully.
BIt overwrites the last element.
CIt causes a compile-time error.
DIt automatically resizes the array.
✗ Incorrect
Fixed-size arrays cannot change size, so adding beyond their size causes a compile-time error.
Which method is used to remove the last element from a dynamic array in Solidity?
Adelete()
Bremove()
Cclear()
Dpop()
✗ Incorrect
The pop() method removes the last element from a dynamic array.
Why might you choose a fixed-size array over a dynamic array in a smart contract?
ATo allow flexible data storage.
BTo reduce gas costs and improve performance.
CBecause fixed arrays can grow automatically.
DBecause dynamic arrays are not supported.
✗ Incorrect
Fixed-size arrays use less gas and are more efficient when the size is known and constant.
How do you access the third element in a Solidity array named myArray?
AmyArray[2]
BmyArray[3]
CmyArray.get(3)
DmyArray.element(3)
✗ Incorrect
Array indexing starts at 0, so the third element is at index 2.
Explain the difference between fixed-size and dynamic arrays in blockchain smart contracts.
Think about how the array size changes and gas costs.
You got /4 concepts.
Describe how you would declare and use a dynamic array in Solidity.
Focus on declaration, adding, removing, and accessing elements.
You got /4 concepts.
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
Step 1: Understand fixed array size
Fixed arrays have a size defined when declared and cannot change during contract execution.
Step 2: Understand dynamic array size
Dynamic arrays can grow or shrink by adding or removing elements during contract execution.
Final Answer:
Fixed arrays have a set size, dynamic arrays can change size during execution. -> Option A
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
Step 1: Recall Solidity array syntax
Dynamic arrays are declared with the type followed by empty square brackets: uint[].
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.
Final Answer:
uint[] numbers; -> Option B
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
Step 1: Understand array after push calls
After calling addNumbers(), the array numbers contains [10, 20, 30].
Step 2: Access the element at index 1
Arrays in Solidity are zero-indexed, so numbers[1] is the second element, which is 20.
Final Answer:
20 -> Option D
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
Step 1: Check pop() method usage
In Solidity, pop() removes the last element and takes no arguments.
Step 2: Identify incorrect call
The code calls data.pop(1); which is invalid syntax and causes a compilation error.
Final Answer:
pop() does not take any arguments; calling pop(1) causes an error. -> Option C
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
Step 1: Understand array assignment rules
In Solidity, fixed-size arrays cannot be directly assigned to dynamic arrays.
Step 2: Copy elements using a loop
Using a for loop to push each element from fixedArr into dynamicArr is the correct way.
Final Answer:
for (uint i = 0; i < fixedArr.length; i++) { dynamicArr.push(fixedArr[i]); } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.