Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

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

Practice

(1/5)
1.

What does the return statement do in a blockchain smart contract function?

easy
A. It sends a value back to the caller of the function.
B. It stops the contract from deploying.
C. It creates a new transaction on the blockchain.
D. It changes the contract's address.

Solution

  1. Step 1: Understand the purpose of return

    The return statement is used inside a function to send a value back to whoever called the function.
  2. Step 2: Identify what return does in blockchain functions

    In smart contracts, returning a value allows the caller to receive data from the function execution.
  3. Final Answer:

    It sends a value back to the caller of the function. -> Option A
  4. Quick Check:

    Return sends value back [OK]
Hint: Return sends data back to caller, not change contract [OK]
Common Mistakes:
  • Thinking return creates transactions
  • Confusing return with contract deployment
  • Believing return changes contract address
2.

Which of the following is the correct syntax to specify a function that returns an uint in Solidity?

function getValue() _____ { return 42; }
easy
A. returns uint
B. return uint
C. return (uint)
D. returns (uint)

Solution

  1. Step 1: Recall Solidity function return syntax

    In Solidity, the correct way to specify a return type is returns (type) after the function parentheses.
  2. Step 2: Match the syntax with options

    returns (uint) uses returns (uint), which is the correct syntax.
  3. Final Answer:

    returns (uint) -> Option D
  4. Quick Check:

    Solidity uses returns (type) [OK]
Hint: Remember: returns keyword with parentheses for type [OK]
Common Mistakes:
  • Omitting parentheses around type
  • Using return instead of returns
  • Placing return type inside function body
3.

What will be the output of this Solidity function call?

function multiply(uint a, uint b) public pure returns (uint) { return a * b; }

Calling multiply(3, 4) returns:

medium
A. 7
B. 12
C. 34
D. Error

Solution

  1. Step 1: Understand the function logic

    The function multiplies two unsigned integers a and b and returns the product.
  2. Step 2: Calculate the product of 3 and 4

    3 multiplied by 4 equals 12, so the function returns 12.
  3. Final Answer:

    12 -> Option B
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply inputs and return result [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating numbers as strings
  • Expecting syntax error
4.

Identify the error in this Solidity function that is supposed to return a bool:

function isEven(uint num) public pure returns (bool) { if (num % 2 == 0) { true; } else { false; } }
medium
A. Missing return statements before true and false.
B. The function should return uint instead of bool.
C. The modulus operator % is not allowed in Solidity.
D. The function should be marked as view instead of pure.

Solution

  1. Step 1: Check the return statements

    The function uses true; and false; without return, so it does not actually return any value.
  2. Step 2: Understand Solidity return requirements

    Every return value must be preceded by the return keyword to send the value back.
  3. Final Answer:

    Missing return statements before true and false. -> Option A
  4. Quick Check:

    Return keyword needed before values [OK]
Hint: Always use return before the value to send back [OK]
Common Mistakes:
  • Omitting return keyword
  • Confusing pure and view modifiers
  • Thinking modulus operator is invalid
5.

You want to write a Solidity function that returns the sum of all elements in a uint[] array. Which of the following implementations correctly returns the sum?

function sumArray(uint[] memory arr) public pure returns (uint) { uint total = 0; for (uint i = 0; i < arr.length; i++) { total += arr[i]; } _____ }
hard
A. return arr.length;
B. return i;
C. return total;
D. return arr;

Solution

  1. Step 1: Understand the function logic

    The function sums all elements in the array by adding each element to total.
  2. Step 2: Identify the correct return value

    After the loop, the sum is stored in total, so returning total gives the correct sum.
  3. Final Answer:

    return total; -> Option C
  4. Quick Check:

    Return the sum variable total [OK]
Hint: Return the accumulator variable holding the sum [OK]
Common Mistakes:
  • Returning array length instead of sum
  • Returning loop index variable
  • Returning the whole array instead of a value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard