Consider the following Solidity contract with overloaded functions named calculate. What will be the output when calling calculate(5) and calculate(5, 10)?
pragma solidity ^0.8.0; contract OverloadExample { function calculate(uint x) public pure returns (uint) { return x * 2; } function calculate(uint x, uint y) public pure returns (uint) { return x + y; } }
Look at how each function processes its parameters: one doubles a single number, the other adds two numbers.
The first calculate function takes one argument and returns it multiplied by 2. So calculate(5) returns 10. The second takes two arguments and returns their sum, so calculate(5, 10) returns 15.
Given this Solidity contract, what is the output of calling getValue(3) and getValue(true)?
pragma solidity ^0.8.0; contract TypeOverload { function getValue(uint x) public pure returns (string memory) { return "Number"; } function getValue(bool b) public pure returns (string memory) { return "Boolean"; } }
Functions are chosen based on the parameter type passed.
The function getValue is overloaded by parameter type. Passing a uint calls the first function returning "Number". Passing a bool calls the second returning "Boolean".
Examine this Solidity contract with overloaded functions. What error will the compiler produce?
pragma solidity ^0.8.0; contract ErrorExample { function foo(uint x) public pure returns (uint) { return x + 1; } function foo(uint y) public pure returns (uint) { return y + 2; } }
Check if the parameter lists differ between the two functions.
Both functions have the same name and identical parameter types (uint). Solidity does not allow overloading functions with the same parameter types, causing a compiler error.
Which option shows the correct syntax for two overloaded functions named transfer in Solidity?
Overloaded functions must differ in parameter types or number of parameters.
Option A shows two functions with the same name but different parameter counts, which is valid overloading. Option A differs only by missing a parameter, which is valid. Option A differs only by parameter order but same types, which is valid in Solidity. Option A duplicates the exact signature, causing an error.
You want to design a Solidity contract with overloaded sendTokens functions:
- One function sends tokens to a single address.
- Another sends tokens to multiple addresses with amounts.
Which option correctly declares these overloaded functions?
Remember to use memory keyword for array parameters in Solidity.
Option A correctly declares two overloaded functions: one for single transfers and one for batch transfers using arrays with memory keyword. Option A misses memory keyword, causing a compiler error. Option A changes parameter types incorrectly. Option A uses a boolean flag instead of arrays, which is not overloading by parameter types but by parameter count.