Challenge - 5 Problems
View and Pure Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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;
}
}Attempts:
2 left
💡 Hint
View functions read state without changing it.
✗ Incorrect
The
getBalance() function is a view function that returns the stored balance which is initialized to 100. Calling it does not change state and returns 100.❓ Predict Output
intermediate2: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;
}
}Attempts:
2 left
💡 Hint
Pure functions return values based only on inputs.
✗ Incorrect
The
add function is pure and returns the sum of a and b. Calling add(5, 3) returns 8.❓ Predict Output
advanced2: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;
}
}Attempts:
2 left
💡 Hint
View functions cannot modify state variables.
✗ Incorrect
The
wrongView function tries to modify the state variable x inside a view function, which is not allowed and causes a compilation error.❓ Predict Output
advanced2: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();
}
}Attempts:
2 left
💡 Hint
Pure functions cannot read or modify state or call view functions.
✗ Incorrect
The
pureCaller function is declared pure but calls getY, a view function that reads state. This is disallowed and causes a compilation error.🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
External calls to view or pure functions do not change blockchain state.
✗ Incorrect
When called externally (not in a transaction), both view and pure functions do not consume gas because they do not modify state and run locally on the node.