Consider the following Solidity contract. What will be the value of count after calling increment() twice?
pragma solidity ^0.8.0; contract Counter { uint public count = 0; function increment() public { count += 1; } }
State variables keep their value between function calls.
The count variable starts at 0 and increases by 1 each time increment() is called. After two calls, it becomes 2.
Choose the correct statement about state variables in Solidity.
Think about where data is saved permanently in a smart contract.
State variables are stored on the blockchain and keep their values between transactions.
Examine the contract below. Why does calling setValue(10) not change the value state variable?
pragma solidity ^0.8.0; contract Test { uint value; function setValue(uint value) public { value = value; } function getValue() public view returns (uint) { return value; } }
Look carefully at the variable names inside the function.
The function parameter value has the same name as the state variable, so value = value; assigns the parameter to itself, leaving the state variable unchanged.
Choose the correct Solidity code snippet that declares a public uint state variable named totalSupply initialized to 1000.
Remember the order of visibility and type in Solidity variable declarations.
The correct syntax is uint public totalSupply = 1000;. Visibility comes after the type.
Given the contract below, what will be the value of balance after the following sequence of calls?
- Call
deposit(50) - Call
withdraw(20) - Call
deposit(30)
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; } } }
Add deposits and subtract withdrawals carefully.
Starting from 0, deposit 50 → 50, withdraw 20 → 30, deposit 30 → 60.