Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Integer overflow and underflow in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Integer Overflow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Solidity code snippet?

Consider the following Solidity function that uses uint8 type. What will be the value of result after calling testUnderflow()?

Blockchain / Solidity
pragma solidity ^0.8.0;
contract Test {
    uint8 public result;
    function testUnderflow() public {
        uint8 x = 0;
        unchecked {
            x = x - 1;
        }
        result = x;
    }
}
ACompilation error due to underflow
Bresult will be 0
Cresult will be 255
Dresult will be 1
Attempts:
2 left
💡 Hint

Think about how unchecked blocks affect arithmetic in Solidity.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes integer overflow in blockchain smart contracts?

Choose the most accurate description of integer overflow in the context of blockchain smart contracts.

AInteger overflow is a syntax error detected during contract compilation.
BInteger overflow occurs when a number exceeds the maximum value the data type can hold, causing it to wrap around to zero or a small number.
CInteger overflow happens when a number becomes negative in an unsigned integer type.
DInteger overflow only happens in floating-point numbers, not integers.
Attempts:
2 left
💡 Hint

Think about what happens when a number is too big for its type.

🔧 Debug
advanced
2:30remaining
Identify the cause of unexpected behavior in this Solidity function

The following Solidity function is intended to add two uint8 numbers safely. However, it sometimes returns incorrect results without errors. What is the cause?

Blockchain / Solidity
pragma solidity ^0.8.0;
contract SafeAdd {
    function add(uint8 a, uint8 b) public pure returns (uint8) {
        unchecked {
            return a + b;
        }
    }
}
AThe <code>unchecked</code> block disables overflow checks, so addition can overflow silently.
BThe function should use <code>int8</code> instead of <code>uint8</code> to avoid overflow.
CThe function lacks a return statement causing runtime error.
DThe compiler version is too old to support <code>unchecked</code> blocks.
Attempts:
2 left
💡 Hint

Consider what unchecked does to arithmetic operations.

📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error due to integer overflow in Solidity 0.8+?

Given Solidity 0.8.0 or newer, which code snippet will cause a compile-time or runtime error due to integer overflow?

Auint8 x = 0; unchecked { x = x - 1; }
Buint8 x = 255; unchecked { x = x + 1; }
Cuint8 x = 0; x = x - 1;
Duint8 x = 255; x = x + 1;
Attempts:
2 left
💡 Hint

Remember how Solidity 0.8+ handles overflow by default.

🚀 Application
expert
3:00remaining
How many items are in the resulting mapping after this Solidity code runs?

Consider the following Solidity contract snippet. After calling populate(), how many keys will the balances mapping contain?

Blockchain / Solidity
pragma solidity ^0.8.0;
contract MappingTest {
    mapping(uint256 => uint256) public balances;
    function populate() public {
        for (uint8 i = 0; i <= 255; i++) {
            unchecked {
                balances[i] = i + 1;
            }
        }
    }
}
A0
B257
C255
D256
Attempts:
2 left
💡 Hint

Consider the effect of uint8 overflow during the for loop increment.

Practice

(1/5)
1. What happens in blockchain when an unsigned integer variable exceeds its maximum value?
easy
A. It wraps around to zero (integer overflow).
B. It throws an error automatically.
C. It becomes negative.
D. It stays at the maximum value.

Solution

  1. Step 1: Understand unsigned integer limits

    Unsigned integers start at 0 and go up to a max value (like 2^256-1 in Solidity).
  2. Step 2: What happens when max is exceeded?

    When the value goes above max, it wraps around to 0, causing integer overflow.
  3. Final Answer:

    It wraps around to zero (integer overflow). -> Option A
  4. Quick Check:

    Unsigned integer overflow = wrap to zero [OK]
Hint: Unsigned integers wrap to zero on overflow [OK]
Common Mistakes:
  • Thinking overflow throws an error automatically
  • Assuming value becomes negative
  • Believing value stays at max
2. Which of the following Solidity code snippets correctly prevents integer underflow when subtracting 1 from a uint variable balance?
easy
A. balance -= 1; require(balance >= 0);
B. balance = balance - 1;
C. if (balance > 0) { balance -= 1; }
D. balance = balance + 1;

Solution

  1. Step 1: Identify safe subtraction

    To avoid underflow, subtract only if balance is greater than zero.
  2. Step 2: Check each option

    if (balance > 0) { balance -= 1; } checks balance > 0 before subtracting, preventing underflow. The other options either subtract without checking first (allowing underflow) or add instead of subtracting.
  3. Final Answer:

    if (balance > 0) { balance -= 1; } -> Option C
  4. Quick Check:

    Check before subtracting prevents underflow [OK]
Hint: Check value > 0 before subtracting to avoid underflow [OK]
Common Mistakes:
  • Not checking before subtracting
  • Using require after subtraction (too late)
  • Adding instead of subtracting
3. Consider this Solidity code snippet:
uint8 x = 255;
x += 1;

What is the value of x after execution?
medium
A. 0
B. 1
C. 255
D. 256

Solution

  1. Step 1: Understand uint8 max value

    uint8 can hold values from 0 to 255. 255 is the max.
  2. Step 2: Add 1 to max value

    Adding 1 causes overflow, wrapping value back to 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    uint8 overflow wraps to 0 [OK]
Hint: uint8 max is 255; adding 1 wraps to 0 [OK]
Common Mistakes:
  • Expecting 256 (out of range)
  • Expecting value to stay at 255
  • Confusing with signed integers
4. This Solidity function tries to subtract 1 from count. What is the bug?
function decrement(uint count) public pure returns (uint) {
    count -= 1;
    return count;
}
medium
A. No bug; works fine.
B. Underflow if count is 0, wrapping to max uint.
C. Syntax error due to missing semicolon.
D. Returns negative number which is invalid.

Solution

  1. Step 1: Analyze subtraction without check

    The function subtracts 1 from count without checking if count is zero.
  2. Step 2: Understand underflow risk

    If count is 0, subtracting 1 causes underflow, wrapping to max uint value.
  3. Final Answer:

    Underflow if count is 0, wrapping to max uint. -> Option B
  4. Quick Check:

    Subtracting 1 from 0 causes underflow [OK]
Hint: Check before subtracting to avoid underflow [OK]
Common Mistakes:
  • Assuming no bug without checks
  • Thinking Solidity throws error automatically
  • Confusing underflow with negative return
5. You have a smart contract token balance stored as uint256. You want to safely add tokens without overflow. Which approach is best?
hard
A. Convert to int256 and add.
B. Use unchecked addition: balance += amount;
C. Subtract amount first, then add balance.
D. Use SafeMath library's add function to check overflow.

Solution

  1. Step 1: Understand overflow risk in addition

    Adding tokens can overflow uint256 max value, causing wraparound bugs.
  2. Step 2: Use SafeMath library

    SafeMath's add function checks for overflow and reverts if it happens, preventing bugs.
  3. Step 3: Evaluate other options

    Unchecked addition risks overflow. Subtracting first or converting to int256 does not prevent overflow safely.
  4. Final Answer:

    Use SafeMath library's add function to check overflow. -> Option D
  5. Quick Check:

    SafeMath add prevents overflow [OK]
Hint: Use SafeMath add to prevent overflow safely [OK]
Common Mistakes:
  • Using unchecked addition risking overflow
  • Trying to subtract before adding
  • Converting to signed int without checks
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