0
0
Blockchain / Solidityprogramming~20 mins

State variables in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Variable 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 contract's state variable?

Consider the following Solidity contract. What will be the value of count after calling increment() twice?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Counter {
    uint public count = 0;

    function increment() public {
        count += 1;
    }
}
A0
B2
C1
DCompilation error
Attempts:
2 left
💡 Hint

State variables keep their value between function calls.

🧠 Conceptual
intermediate
1:30remaining
Which statement about state variables in Solidity is true?

Choose the correct statement about state variables in Solidity.

AState variables are stored in memory and lost after function execution.
BState variables are only accessible inside the constructor.
CState variables are stored on the blockchain and persist between transactions.
DState variables cannot be public.
Attempts:
2 left
💡 Hint

Think about where data is saved permanently in a smart contract.

🔧 Debug
advanced
2:30remaining
Why does this contract fail to update the state variable?

Examine the contract below. Why does calling setValue(10) not change the value state variable?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint value;

    function setValue(uint value) public {
        value = value;
    }

    function getValue() public view returns (uint) {
        return value;
    }
}
AThe function should be marked as view to modify state variables.
BThe state variable is private and cannot be changed.
CThe contract is missing a constructor to initialize the variable.
DThe function parameter shadows the state variable, so assignment does nothing.
Attempts:
2 left
💡 Hint

Look carefully at the variable names inside the function.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a public state variable with an initial value?

Choose the correct Solidity code snippet that declares a public uint state variable named totalSupply initialized to 1000.

Auint public totalSupply = 1000;
Bpublic uint totalSupply = 1000;
Cuint totalSupply public = 1000;
Duint totalSupply = 1000 public;
Attempts:
2 left
💡 Hint

Remember the order of visibility and type in Solidity variable declarations.

🚀 Application
expert
3:00remaining
What is the value of the state variable after multiple transactions?

Given the contract below, what will be the value of balance after the following sequence of calls?

  1. Call deposit(50)
  2. Call withdraw(20)
  3. Call deposit(30)
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Wallet {
    uint public balance;

    function deposit(uint amount) public {
        balance += amount;
    }

    function withdraw(uint amount) public {
        if (amount <= balance) {
            balance -= amount;
        }
    }
}
A60
B100
C80
D50
Attempts:
2 left
💡 Hint

Add deposits and subtract withdrawals carefully.