Challenge - 5 Problems
Library Contract Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a Solidity library function call
What is the output of this Solidity code when calling
TestLibrary.getDouble(5)?Blockchain / Solidity
library TestLibrary {
function getDouble(uint x) internal pure returns (uint) {
return x * 2;
}
}
contract Test {
function callDouble(uint val) public pure returns (uint) {
return TestLibrary.getDouble(val);
}
}Attempts:
2 left
💡 Hint
Remember that the library function multiplies the input by 2.
✗ Incorrect
The library function
getDouble returns the input multiplied by 2. So input 5 returns 10.🧠 Conceptual
intermediate1:30remaining
Why use library contracts in Solidity?
Which of the following is the main reason to use library contracts in Solidity?
Attempts:
2 left
💡 Hint
Think about how libraries help reduce contract size and gas costs.
✗ Incorrect
Libraries allow code reuse by deploying once and linking, saving gas and avoiding code duplication.
🔧 Debug
advanced2:30remaining
Identify the error in this library usage
What error will this Solidity code produce when compiled?
Blockchain / Solidity
library MathLib {
function add(uint a, uint b) public pure returns (uint) {
return a + b;
}
}
contract Calc {
function sum(uint x, uint y) public pure returns (uint) {
return MathLib.add(x, y);
}
}Attempts:
2 left
💡 Hint
Check the visibility of library functions in Solidity.
✗ Incorrect
Library functions must be internal or external, not public. Public visibility causes a compilation error.
📝 Syntax
advanced2:00remaining
Correct syntax for using a library with 'using for'
Which option correctly applies the library
ArrayLib to type uint[] using using for syntax?Blockchain / Solidity
library ArrayLib {
function sum(uint[] storage arr) internal view returns (uint) {
uint s = 0;
for (uint i = 0; i < arr.length; i++) {
s += arr[i];
}
return s;
}
}
contract Test {
uint[] data;
// Which line correctly applies the library?
}Attempts:
2 left
💡 Hint
The correct keyword is 'using' followed by the library name and 'for' the type.
✗ Incorrect
The correct syntax is
using ArrayLib for uint[]; to attach library functions to the type.🚀 Application
expert3:00remaining
How many deployed contracts exist after deploying this code?
Given this Solidity code, how many contracts are deployed on the blockchain after deploying
MainContract?Blockchain / Solidity
library HelperLib {
function multiply(uint a, uint b) internal pure returns (uint) {
return a * b;
}
}
contract MainContract {
function calc(uint x, uint y) public pure returns (uint) {
return HelperLib.multiply(x, y);
}
}Attempts:
2 left
💡 Hint
Consider if the library code is deployed separately or embedded.
✗ Incorrect
Since the library function is internal, its code is embedded in the contract, so only one contract is deployed.