0
0
Blockchain / Solidityprogramming~20 mins

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

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