0
0
Blockchain / Solidityprogramming~10 mins

Library contracts 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 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'
AMaths
BMathLibrary
CMathLib
DLibMath
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different library name than MathLib.
Forgetting the library keyword.
2fill in blank
medium

Complete 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'
ASafeMath
BUtils
CCalcLib
DMathLib
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong library name like SafeMath or Utils.
Forgetting to use the using keyword.
3fill in blank
hard

Fix 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'
Apure
Bview
Cpayable
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using view instead of pure.
Marking the function payable or external incorrectly.
4fill in blank
hard

Fill 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'
Apure
Bview
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using view instead of pure.
Using the wrong comparison operator >.
5fill in blank
hard

Fill 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'
Apure
B%
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using view instead of pure.
Using != instead of ==.
Using wrong operators for modulo.