Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

State variables in Blockchain / Solidity - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a state variable named count of type uint.

Blockchain / Solidity
contract Counter {
    [1] count;
}
Drag options to blanks, or click blank then click option'
Afunction
Buint
Cevent
Dmapping
Attempts:
3 left
💡 Hint
Common Mistakes
Using function instead of a data type.
Trying to declare state variables without a type.
2fill in blank
medium

Complete the code to initialize the state variable owner with the address of the contract creator.

Blockchain / Solidity
contract Ownership {
    address public owner = [1];
}
Drag options to blanks, or click blank then click option'
Athis
Bowner
Cmsg.sender
Daddress(0)
Attempts:
3 left
💡 Hint
Common Mistakes
Using this which refers to the contract itself, not the creator.
Using address(0) which is the zero address.
3fill in blank
hard

Fix the error in the code by completing the declaration of the state variable balance as a private unsigned integer.

Blockchain / Solidity
contract Wallet {
    [1] uint balance;
}
Drag options to blanks, or click blank then click option'
Apublic
Bexternal
Cinternal
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using external which is invalid for state variables.
Using public when privacy is needed.
4fill in blank
hard

Fill both blanks to declare a state variable data as a public string and initialize it with "Hello".

Blockchain / Solidity
contract Example {
    [1] public data = [2];
}
Drag options to blanks, or click blank then click option'
Astring
B"Hello"
C"World"
Dbytes
Attempts:
3 left
💡 Hint
Common Mistakes
Using bytes instead of string for text.
Forgetting quotes around the text.
5fill in blank
hard

Fill all three blanks to declare a private unsigned integer totalSupply, initialize it to 1000, and make it accessible via a public function getSupply.

Blockchain / Solidity
contract Token {
    [1] uint totalSupply = [2];
    function getSupply() public view returns (uint) {
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
Aprivate
B1000
CtotalSupply
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using public instead of private for the variable.
Returning a wrong variable or literal in the function.

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