0
0
Blockchain / Solidityprogramming~20 mins

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

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