Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a library named MathLib.
Blockchain / Solidity
library [1] { function add(uint a, uint b) internal pure returns (uint) { return a + b; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different library name than
MathLib.Forgetting the
library keyword.✗ Incorrect
The library name must be MathLib as declared in the code.
2fill in blank
mediumComplete the code to use the library function add for uint variables.
Blockchain / Solidity
using [1] for uint; contract Calculator { function sum(uint a, uint b) public pure returns (uint) { return a.add(b); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong library name like
SafeMath or Utils.Forgetting to use the
using keyword.✗ Incorrect
The using directive must refer to the library MathLib to use its add function.
3fill in blank
hardFix the error in the library function declaration to make it a pure function.
Blockchain / Solidity
library MathLib {
function multiply(uint a, uint b) internal [1] returns (uint) {
return a * b;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
view instead of pure.Marking the function
payable or external incorrectly.✗ Incorrect
The function should be marked pure because it does not read or modify state.
4fill in blank
hardFill both blanks to declare a library function that returns the minimum of two uints.
Blockchain / Solidity
library MathLib {
function min(uint a, uint b) internal [1] returns (uint) {
return a [2] b ? a : b;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
view instead of pure.Using the wrong comparison operator
>.✗ Incorrect
The function is pure and uses the less than operator < to find the minimum.
5fill in blank
hardFill all three blanks to create a library function that checks if a number is even.
Blockchain / Solidity
library MathLib {
function isEven(uint num) internal [1] returns (bool) {
return num [2] 2 [3] 0;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
view instead of pure.Using
!= instead of ==.Using wrong operators for modulo.
✗ Incorrect
The function is pure. It uses the modulo operator % to check if the remainder is equal to zero.