Sign in

Solidity types - Test

01.

Caller identity at deployment time

A contract has the following constructor:

solidity
contract Vault {
    address public deployer;

    constructor() {
        deployer = msg.sender;
    }
}

Alice's EOA deploys this contract directly from her wallet. After deployment, what does deployer hold?

02.

You want to check whether two string memory variables hold the same text. Which expression compiles and works?

03.

Consider this contract:

solidity
contract Order {
    enum Status { Approved, Pending, Rejected, Shipped }
    Status public state;
}

Immediately after deployment, what is the value of state?

04.

What is the result of compiling this function?

solidity
function build() public pure returns (uint256[] memory) {
    uint256[] memory arr = new uint256[](2);
    arr[0] = 1;
    arr[1] = 2;
    arr.push(3);
    return arr;
}

05.

Two functions read from data structures using a value that was never written:

solidity
contract A {
    mapping(uint256 => uint256) m;
    uint256[] arr;

    function readMissing() external view returns (uint256, uint256) {
        return (m[999], arr[999]);
    }
}

What happens when readMissing is called on a freshly deployed contract?

06.

A contract starts with a balance of 0. An external caller invokes the following function with 1 ether attached:

solidity
contract Vault {
    function deposit() external payable returns (uint256) {
        return address(this).balance;
    }
}
07.

Two functions both compute "7 percent of x" using integer math:

solidity
function pctA(uint256 x) public pure returns (uint256) {
    return (x * 7) / 100;
}

function pctB(uint256 x) public pure returns (uint256) {
    return (x / 100) * 7;
}

For x = 50, what does each function return?

08.

A contract has this code:

solidity
enum Status { A, B, C }

function castFrom(uint8 value) public pure returns (Status) {
    return Status(value);
}

What happens when this function is called with value = 5?

09.

Two contracts each declare a single state variable:

solidity
contract A { uint8 public count; }
contract B { uint256 public count; }

Compared to writing to count in contract B, writing to count in contract A costs: