Consider the following Solidity function that returns a boolean and an integer:
function test() public pure returns (bool, int) {
int a = -10;
bool b = a < 0;
return (b, a * 2);
}What will be the returned values?
function test() public pure returns (bool, int) { int a = -10; bool b = a < 0; return (b, a * 2); }
Remember that int can hold negative values and the comparison a < 0 checks if a is negative.
The variable a is -10, which is less than 0, so b is true. Multiplying -10 by 2 gives -20.
Given this Solidity code snippet:
address addr = 0x1234567890123456789012345678901234567890; address zeroAddr = address(0); bool isZero = (addr == zeroAddr);
What is the value of isZero?
address addr = 0x1234567890123456789012345678901234567890; address zeroAddr = address(0); bool isZero = (addr == zeroAddr);
The zero address is a special address with all zeros. Check if addr matches it.
The addr is a non-zero address, so comparing it to address(0) results in false.
Consider this Solidity function:
function compareStrings() public pure returns (bool) {
string memory a = "hello";
string memory b = "hello";
return keccak256(bytes(a)) == keccak256(bytes(b));
}What does this function return?
function compareStrings() public pure returns (bool) { string memory a = "hello"; string memory b = "hello"; return keccak256(bytes(a)) == keccak256(bytes(b)); }
Strings in Solidity cannot be compared directly. Instead, their hashes are compared.
Both strings are "hello", so their keccak256 hashes are equal, returning true.
Analyze this Solidity snippet:
uint8 x = 255; x = x + 1;
What happens when this code runs?
uint8 x = 255; x = x + 1;
Since Solidity 0.8, arithmetic overflow causes a runtime error.
Adding 1 to 255 (max for uint8) causes overflow, triggering a runtime revert error.
In Solidity, which data type should you use to store Ethereum wallet addresses securely and efficiently?
Ethereum addresses have a special data type designed for them.
The address type is specifically designed to store Ethereum wallet addresses, providing built-in safety and utility functions.