0
0
Blockchain / Solidityprogramming~20 mins

Library contracts in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Library Contract Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A0
B5
CCompilation error
D10
Attempts:
2 left
💡 Hint
Remember that the library function multiplies the input by 2.
🧠 Conceptual
intermediate
1:30remaining
Why use library contracts in Solidity?
Which of the following is the main reason to use library contracts in Solidity?
ATo manage private keys securely
BTo store large amounts of data persistently
CTo reuse code without deploying multiple copies
DTo create user interfaces
Attempts:
2 left
💡 Hint
Think about how libraries help reduce contract size and gas costs.
🔧 Debug
advanced
2: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);
    }
}
ATypeError: Library function cannot be public
BNo error, compiles successfully
CWarning: Function should be external
DRuntime error: Division by zero
Attempts:
2 left
💡 Hint
Check the visibility of library functions in Solidity.
📝 Syntax
advanced
2: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?
}
Ausing ArrayLib on uint[];
Busing ArrayLib for uint[];
Cimport ArrayLib for uint[];
Dapply ArrayLib to uint[];
Attempts:
2 left
💡 Hint
The correct keyword is 'using' followed by the library name and 'for' the type.
🚀 Application
expert
3: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);
    }
}
A1
B2
C0
D3
Attempts:
2 left
💡 Hint
Consider if the library code is deployed separately or embedded.