0
0
Blockchain / Solidityprogramming~20 mins

Return values in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Return Values 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 function call?

Consider the following Solidity function that returns a value. What will be the output when getValue() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    function getValue() public pure returns (uint) {
        uint a = 5;
        uint b = 10;
        return a + b;
    }
}
A10
B5
C15
DCompilation error
Attempts:
2 left
💡 Hint

Look at the return statement carefully. It adds two numbers.

Predict Output
intermediate
2:00remaining
What does this smart contract function return?

Given the following Solidity function, what is the output when getGreeting() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Greeter {
    function getGreeting() public pure returns (string memory) {
        return "Hello, Blockchain!";
    }
}
A"Hello, Blockchain!"
B"Hello, World!"
CCompilation error due to missing memory keyword
DEmpty string
Attempts:
2 left
💡 Hint

Check the string returned exactly as written.

Predict Output
advanced
2:00remaining
What is the output of this Solidity function with multiple returns?

What will be the output of calling getData() in the following contract?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Data {
    function getData() public pure returns (uint, bool) {
        uint x = 42;
        bool y = true;
        return (x, y);
    }
}
ACompilation error due to missing tuple return
B(42, true)
C42
D(true, 42)
Attempts:
2 left
💡 Hint

Look at the order of the returned values in the function signature.

Predict Output
advanced
2:00remaining
What error does this Solidity function produce?

What error will occur when calling brokenFunction() in this contract?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Broken {
    function brokenFunction() public pure returns (uint) {
        uint a = 10;
        // Missing return statement
    }
}
ANo error, returns 10
BRuntime error: Division by zero
CReturns 0 by default
DCompilation error: Missing return statement
Attempts:
2 left
💡 Hint

Functions with a return type must have a return statement.

🧠 Conceptual
expert
3:00remaining
Which option correctly describes the behavior of return values in Solidity functions?

Choose the statement that best describes how return values work in Solidity functions.

AFunctions must always explicitly return a value if a return type is declared; otherwise, a compilation error occurs.
BFunctions can omit return statements even if a return type is declared; the default zero value is returned automatically.
CFunctions can return multiple values only if they are declared as external functions.
DFunctions without a return type can still return values without errors.
Attempts:
2 left
💡 Hint

Think about Solidity's strict typing and return requirements.