Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

View and pure functions in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
View and Pure Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a view function call
Consider this Solidity contract snippet with a view function. What will be the output when calling getBalance() after deployment?
Blockchain / Solidity
contract Wallet {
    uint private balance = 100;
    
    function getBalance() public view returns (uint) {
        return balance;
    }
}
ATransaction rejected
B0
CCompilation error
D100
Attempts:
2 left
💡 Hint
View functions read state without changing it.
❓ Predict Output
intermediate
2:00remaining
Pure function output with parameters
What is the output of calling add(5, 3) in this Solidity contract?
Blockchain / Solidity
contract Calculator {
    function add(uint a, uint b) public pure returns (uint) {
        return a + b;
    }
}
ACompilation error
B8
C0
DTransaction reverted
Attempts:
2 left
💡 Hint
Pure functions return values based only on inputs.
❓ Predict Output
advanced
2:00remaining
Effect of modifying state in a view function
What happens when this contract is compiled and deployed?
Blockchain / Solidity
contract Test {
    uint x = 10;
    
    function wrongView() public view returns (uint) {
        x = 20;
        return x;
    }
}
AReturns 10
BReturns 20
CCompilation error due to state modification in view function
DRuntime error when calling wrongView
Attempts:
2 left
💡 Hint
View functions cannot modify state variables.
❓ Predict Output
advanced
2:00remaining
Pure function calling a view function
What error occurs when compiling this contract?
Blockchain / Solidity
contract Mixed {
    uint y = 5;
    
    function getY() public view returns (uint) {
        return y;
    }
    
    function pureCaller() public pure returns (uint) {
        return getY();
    }
}
ACompilation error: pure function cannot call view function
BRuntime error when calling pureCaller
CReturns 0
DReturns 5
Attempts:
2 left
💡 Hint
Pure functions cannot read or modify state or call view functions.
🧠 Conceptual
expert
2:00remaining
Gas cost difference between view and pure functions
Which statement about gas costs when calling view and pure functions externally (via call) is correct?
ABoth view and pure functions called externally cost no gas
BView functions cost gas, pure functions do not
CBoth view and pure functions always cost gas
DPure functions cost gas, view functions do not
Attempts:
2 left
💡 Hint
External calls to view or pure functions do not change blockchain state.

Practice

(1/5)
1. Which statement best describes a view function in a blockchain smart contract?
easy
A. It reads blockchain data but does not modify it.
B. It modifies blockchain data and stores new values.
C. It neither reads nor writes blockchain data.
D. It only accepts inputs and returns no output.

Solution

  1. Step 1: Understand the role of view functions

    View functions are designed to read data from the blockchain without changing it.
  2. Step 2: Compare options with definition

    It reads blockchain data but does not modify it. matches this definition exactly, while others describe modifying or ignoring blockchain data.
  3. Final Answer:

    It reads blockchain data but does not modify it. -> Option A
  4. Quick Check:

    View function = reads only [OK]
Hint: View functions read data but never change it [OK]
Common Mistakes:
  • Confusing view with functions that modify data
  • Thinking view functions can write data
  • Assuming view functions do not read blockchain data
2. Which of the following is the correct syntax to declare a pure function in Solidity?
easy
A. function calculate() public pure returns (uint) {}
B. function calculate() public view returns (uint) {}
C. function calculate() public returns (uint) {}
D. function calculate() pure returns (uint) {}

Solution

  1. Step 1: Recall Solidity function declaration syntax

    Functions must specify visibility (e.g., public) and state mutability (pure/view) before returns.
  2. Step 2: Identify correct pure function syntax

    function calculate() public pure returns (uint) {} correctly includes visibility and pure keyword before returns. function calculate() pure returns (uint) {} misses visibility, which is required.
  3. Final Answer:

    function calculate() public pure returns (uint) {} -> Option A
  4. Quick Check:

    Pure function syntax = public pure returns [OK]
Hint: Pure functions need visibility and 'pure' keyword before returns [OK]
Common Mistakes:
  • Omitting visibility keyword
  • Using view instead of pure
  • Placing pure after returns
3. What will be the output of this Solidity function call?
function multiply(uint a, uint b) public pure returns (uint) {
    return a * b;
}

// Called with multiply(3, 4)
medium
A. Error: function cannot modify state
B. 7
C. 0
D. 12

Solution

  1. Step 1: Understand the function logic

    The function multiplies two unsigned integers and returns the result.
  2. Step 2: Calculate the output for inputs 3 and 4

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

    12 -> Option D
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply inputs directly in pure function for output [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Expecting state change error
  • Confusing pure with view function behavior
4. Identify the error in this Solidity function declared as view:
uint count = 0;
function increment() public view returns (uint) {
    count += 1;
    return count;
}
medium
A. Missing returns keyword.
B. Cannot modify state variable in a view function.
C. Function should be pure, not view.
D. No error, code is correct.

Solution

  1. Step 1: Understand view function restrictions

    View functions cannot modify state variables; they only read data.
  2. Step 2: Analyze the code behavior

    The function increments a state variable, which is a write operation, causing an error.
  3. Final Answer:

    Cannot modify state variable in a view function. -> Option B
  4. Quick Check:

    View functions = no state changes [OK]
Hint: View functions cannot change state variables [OK]
Common Mistakes:
  • Thinking view allows state changes
  • Confusing pure and view keywords
  • Ignoring state variable modification
5. You want to create a function that calculates the sum of two numbers without reading or modifying any blockchain data. Which function type should you use and how should it be declared in Solidity?
hard
A. Use a view function declared as function sum(uint a, uint b) public view returns (uint).
B. Use a regular function declared as function sum(uint a, uint b) public returns (uint).
C. Use a pure function declared as function sum(uint a, uint b) public pure returns (uint).
D. Use a payable function declared as function sum(uint a, uint b) public payable returns (uint).

Solution

  1. Step 1: Identify function requirements

    The function must not read or modify blockchain data, only use inputs to calculate sum.
  2. Step 2: Choose correct function type and declaration

    Pure functions do not read or write blockchain data, so declaring it as public pure with inputs and returns is correct.
  3. Final Answer:

    Use a pure function declared as function sum(uint a, uint b) public pure returns (uint). -> Option C
  4. Quick Check:

    Pure = no blockchain data access [OK]
Hint: Pure functions only use inputs, no blockchain data access [OK]
Common Mistakes:
  • Using view instead of pure
  • Using payable unnecessarily
  • Not specifying pure keyword
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