Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

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

Practice

(1/5)
1. What is a state variable in a blockchain smart contract?
easy
A. A variable that stores data permanently on the blockchain
B. A temporary variable used only inside functions
C. A variable that stores user input off-chain
D. A variable that resets every time a function runs

Solution

  1. Step 1: Understand what state variables do

    State variables store data permanently on the blockchain, unlike local variables which are temporary.
  2. Step 2: Compare options with definition

    Only A variable that stores data permanently on the blockchain describes permanent storage on the blockchain, which matches the definition of state variables.
  3. Final Answer:

    A variable that stores data permanently on the blockchain -> Option A
  4. Quick Check:

    State variable = permanent blockchain storage [OK]
Hint: State variables save data permanently on blockchain [OK]
Common Mistakes:
  • Confusing state variables with local variables
  • Thinking state variables reset after function ends
  • Assuming state variables store data off-chain
2. Which of the following is the correct way to declare a state variable in Solidity?
easy
A. let count = uint256;
B. function uint256 count;
C. uint256 public count;
D. var count = 0;

Solution

  1. Step 1: Recall Solidity syntax for state variables

    State variables are declared outside functions with a type and visibility, e.g., uint256 public count;.
  2. Step 2: Check each option

    uint256 public count; matches correct syntax. Options A, B, and C use incorrect keywords or syntax not valid for state variables.
  3. Final Answer:

    uint256 public count; -> Option C
  4. Quick Check:

    State variable declaration = type + visibility + name [OK]
Hint: State variables need type and visibility outside functions [OK]
Common Mistakes:
  • Using function keyword to declare variables
  • Using var or let which are not Solidity keywords
  • Declaring variables inside functions as state variables
3. Consider this Solidity contract snippet:
contract Counter {
  uint256 public count = 0;
  function increment() public {
    count += 1;
  }
}

What will be the value of count after calling increment() three times?
medium
A. 1
B. 3
C. 0
D. Error: count is not accessible

Solution

  1. Step 1: Understand initial value and increment

    The state variable count starts at 0 and increases by 1 each time increment() is called.
  2. Step 2: Calculate after three calls

    After three calls, count = 0 + 1 + 1 + 1 = 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    0 + 3 increments = 3 [OK]
Hint: Add 1 to count for each increment call [OK]
Common Mistakes:
  • Forgetting count starts at 0
  • Assuming count resets after each call
  • Thinking count is private and inaccessible
4. Identify the error in this Solidity contract:
contract Example {
  uint256 count;
  function setCount(uint256 _count) public {
    uint256 count = _count;
  }
}
medium
A. Local variable shadows state variable inside function
B. State variable count is not declared
C. Function setCount has wrong return type
D. Missing visibility for state variable count

Solution

  1. Step 1: Analyze variable declarations

    The state variable count is declared correctly. Inside setCount, a new local variable count is declared, which hides the state variable.
  2. Step 2: Understand shadowing effect

    Assigning to local count does not change the state variable. This is a common mistake called variable shadowing.
  3. Final Answer:

    Local variable shadows state variable inside function -> Option A
  4. Quick Check:

    Local variable shadows state variable = bug [OK]
Hint: Avoid declaring local variable with same name as state variable [OK]
Common Mistakes:
  • Thinking state variable is missing
  • Ignoring variable shadowing
  • Assuming function needs return type
5. You want to create a smart contract that stores a user's name permanently and allows updating it. Which of these is the best way to declare the state variable and update it?
hard
A. Declare string memory name; as state variable
B. Declare string name; inside the update function and assign new value there
C. Use a local variable string name; in constructor only
D. Declare string public name; as state variable and update it in a public function

Solution

  1. Step 1: Understand state variable declaration for permanent storage

    State variables must be declared outside functions with proper type and visibility, e.g., string public name;.
  2. Step 2: Update state variable in a public function

    To change the stored name, write a public function that assigns a new value to the state variable.
  3. Final Answer:

    Declare string public name; as state variable and update it in a public function -> Option D
  4. Quick Check:

    State variable + public update function = correct pattern [OK]
Hint: Declare state variable outside, update inside public function [OK]
Common Mistakes:
  • Declaring variables inside functions only
  • Using memory keyword for state variables
  • Not providing visibility for state variables
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