Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a library contract in blockchain development?
easy
A. To reuse code across multiple smart contracts
B. To store user data permanently
C. To mine new blocks
D. To create new tokens

Solution

  1. Step 1: Understand the role of library contracts

    Library contracts contain reusable code that can be called by other contracts to avoid duplication.
  2. Step 2: Compare options with library purpose

    Only To reuse code across multiple smart contracts correctly describes code reuse, while others describe unrelated blockchain functions.
  3. Final Answer:

    To reuse code across multiple smart contracts -> Option A
  4. Quick Check:

    Library = Code reuse [OK]
Hint: Library contracts are for sharing code, not storing data [OK]
Common Mistakes:
  • Confusing libraries with data storage contracts
  • Thinking libraries create tokens
  • Assuming libraries mine blocks
2. Which of the following is the correct way to declare a library in Solidity?
easy
A. library MyLib {}
B. contract MyLib {}
C. interface MyLib {}
D. struct MyLib {}

Solution

  1. Step 1: Recall Solidity keywords for libraries

    Solidity uses the keyword library to declare a library contract.
  2. Step 2: Check each option's keyword

    Only library MyLib {} uses library. Others declare contract, interface, or struct, which are different constructs.
  3. Final Answer:

    library MyLib {} -> Option A
  4. Quick Check:

    Keyword for library = library [OK]
Hint: Use 'library' keyword to declare libraries in Solidity [OK]
Common Mistakes:
  • Using 'contract' instead of 'library'
  • Confusing interfaces with libraries
  • Trying to declare libraries as structs
3. Given the following Solidity code, what will be the output of test.getDouble(5)?
library MathLib {
    function double(uint x) internal pure returns (uint) {
        return x * 2;
    }
}

contract Test {
    using MathLib for uint;
    function getDouble(uint value) public pure returns (uint) {
        return value.double();
    }
}
medium
A. 5
B. 10
C. Compilation error
D. 0

Solution

  1. Step 1: Understand the library function call

    The library MathLib defines a function double that multiplies input by 2.
  2. Step 2: Analyze the usage in contract Test

    Using using MathLib for uint; allows calling double() on uint variables. So value.double() returns 5 * 2 = 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Library functions called with 'using for' act on the type [OK]
Common Mistakes:
  • Expecting the original value instead of doubled
  • Thinking 'using for' is optional for calling library functions
  • Confusing internal and public function visibility
4. Identify the error in the following Solidity code using a library:
library Utils {
    function increment(uint x) public pure returns (uint) {
        return x + 1;
    }
}

contract Counter {
    using Utils for uint;
    uint count;
    function increase() public {
        count = count.increment();
    }
}
medium
A. Cannot use 'using for' with uint type
B. Missing constructor in contract
C. Library function must be internal, not public
D. Function increase should be view

Solution

  1. Step 1: Check library function visibility

    Library functions used with 'using for' must be internal, not public.
  2. Step 2: Verify other options

    No constructor is needed here, 'using for' works with uint, and increase modifies state so cannot be view.
  3. Final Answer:

    Library function must be internal, not public -> Option C
  4. Quick Check:

    Library function visibility = internal [OK]
Hint: Library functions called via 'using for' cannot be public [OK]
Common Mistakes:
  • Declaring library functions as public
  • Thinking 'using for' can't be used with uint
  • Marking state-changing functions as view
5. You want to create a library that provides a function to safely add two unsigned integers without overflow. Which of the following code snippets correctly implements this using Solidity 0.8+ features and can be used with using for in other contracts?
hard
A. library SafeMath { function safeAdd(uint a, uint b) internal view returns (uint) { return a + b; } }
B. library SafeMath { function safeAdd(uint a, uint b) public pure returns (uint) { return a + b; } }
C. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { unchecked { return a + b; } } }
D. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } }

Solution

  1. Step 1: Check function visibility and usage

    Library functions used with 'using for' must be internal. library SafeMath { function safeAdd(uint a, uint b) public pure returns (uint) { return a + b; } } uses public (invalid), D uses view which is incorrect for pure functions. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } uses internal pure.
  2. Step 2: Verify overflow check and Solidity 0.8+ behavior

    Solidity 0.8+ has built-in overflow checks, so unchecked block in C disables protection and returns wrapped value on overflow. library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } correctly relies on built-in checks with additional require guard and no unchecked block.
  3. Final Answer:

    library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } -> Option D
  4. Quick Check:

    Internal pure function with overflow check = library SafeMath { function safeAdd(uint a, uint b) internal pure returns (uint) { uint c = a + b; require(c >= a, "Overflow"); return c; } } [OK]
Hint: Use internal pure functions with require for safe math [OK]
Common Mistakes:
  • Using public visibility in library functions
  • Using view instead of pure for math functions
  • Unnecessary unchecked blocks in Solidity 0.8+
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard