Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Gas and transaction fees in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Gas and Fees Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Calculate total transaction cost in Ethereum

Given the following transaction parameters, what is the total cost in Ether for this transaction?

Gas limit: 21000
Gas price: 50 Gwei (1 Gwei = 10-9 Ether)

Blockchain / Solidity
gas_limit = 21000
gas_price_gwei = 50
gas_price_ether = gas_price_gwei * 1e-9
total_cost = gas_limit * gas_price_ether
print(total_cost)
A0.0000105
B0.00105
C0.000105
D0.0105
Attempts:
2 left
๐Ÿ’ก Hint

Multiply gas limit by gas price converted to Ether.

๐Ÿง  Conceptual
intermediate
1:30remaining
Understanding Gas Limit and Gas Price

Which statement correctly describes the relationship between gas limit and gas price in a blockchain transaction?

AGas limit and gas price are the same and used interchangeably.
BGas price is the maximum gas a transaction can use; gas limit is the amount paid per unit of gas.
CGas limit is the maximum amount of gas a transaction can use; gas price is the amount paid per unit of gas.
DGas limit is the total fee paid; gas price is the transaction size.
Attempts:
2 left
๐Ÿ’ก Hint

Think about how much gas you allow and how much you pay per gas unit.

๐Ÿ”ง Debug
advanced
2:00remaining
Identify the error in gas fee calculation code

What error does the following code produce when calculating transaction fees?

gas_limit = 30000
gas_price = '20'
total_fee = gas_limit * gas_price
print(total_fee)
ANo error, outputs '20' repeated 30000 times
BTypeError: can't multiply sequence by non-int of type 'str'
CTypeError: unsupported operand type(s) for *: 'int' and 'str'
DTypeError: can't multiply sequence by non-int of type 'int'
Attempts:
2 left
๐Ÿ’ก Hint

Check the data types of variables before multiplication.

โ“ Predict Output
advanced
2:00remaining
Calculate transaction fee with dynamic gas price

What is the output of this code snippet?

def calculate_fee(gas_limit, gas_price_gwei):
    gas_price_ether = gas_price_gwei * 1e-9
    return gas_limit * gas_price_ether

fee = calculate_fee(50000, 100)
print(f"{fee:.6f}")
A0.005000
B0.000500
C0.050000
D0.000050
Attempts:
2 left
๐Ÿ’ก Hint

Multiply gas limit by gas price converted to Ether.

๐Ÿง  Conceptual
expert
2:30remaining
Why do blockchain transactions require gas fees?

Which is the main reason blockchain networks require gas fees for transactions?

ATo increase the value of the cryptocurrency by burning tokens.
BTo limit the number of transactions by setting a fixed fee per transaction.
CTo pay miners directly for storing user data permanently.
DTo prevent spam and allocate network resources fairly by charging for computation.
Attempts:
2 left
๐Ÿ’ก Hint

Think about why networks charge fees for running code.

Practice

(1/5)
1. What does gas represent in a blockchain transaction?
easy
A. The amount of computational work required to process the transaction
B. The total amount of cryptocurrency sent in the transaction
C. The number of transactions in a block
D. The time it takes to confirm a transaction

Solution

  1. Step 1: Understand the role of gas in blockchain

    Gas is a unit that measures how much computational effort is needed to execute operations like transactions or smart contracts.
  2. Step 2: Differentiate gas from other transaction elements

    Gas is not the amount of cryptocurrency sent, nor the number of transactions or confirmation time.
  3. Final Answer:

    The amount of computational work required to process the transaction -> Option A
  4. Quick Check:

    Gas = computational work [OK]
Hint: Gas measures work, not money or time [OK]
Common Mistakes:
  • Confusing gas with transaction value
  • Thinking gas is transaction speed
  • Mixing gas with block size
2. Which of the following is the correct formula to calculate transaction fees?
easy
A. Transaction fees = gas price รท gas used
B. Transaction fees = gas price - gas used
C. Transaction fees = gas used + gas price
D. Transaction fees = gas used x gas price

Solution

  1. Step 1: Recall the transaction fee formula

    Transaction fees are calculated by multiplying the gas used by the gas price.
  2. Step 2: Verify the formula correctness

    Dividing, adding, or subtracting gas used and gas price does not represent the fee calculation.
  3. Final Answer:

    Transaction fees = gas used x gas price -> Option D
  4. Quick Check:

    Fees = gas used x gas price [OK]
Hint: Multiply gas used by gas price for fees [OK]
Common Mistakes:
  • Using division instead of multiplication
  • Adding gas used and gas price
  • Subtracting gas values
3. Given a transaction that used 21000 gas units and a gas price of 50 gwei, what is the total transaction fee in gwei?
medium
A. 21000 gwei
B. 1,050,000 gwei
C. 420 gwei
D. 50 gwei

Solution

  1. Step 1: Identify given values

    Gas used = 21000 units, Gas price = 50 gwei per unit.
  2. Step 2: Calculate total fee

    Total fee = gas used x gas price = 21000 x 50 = 1,050,000 gwei.
  3. Final Answer:

    1,050,000 gwei -> Option B
  4. Quick Check:

    21000 x 50 = 1,050,000 [OK]
Hint: Multiply gas used by gas price directly [OK]
Common Mistakes:
  • Multiplying incorrectly
  • Confusing gas units with gwei
  • Using gas price as total fee
4. Identify the error in this transaction fee calculation code snippet:
gas_used = 30000
gas_price = 20
transaction_fee = gas_used + gas_price
print(transaction_fee)
medium
A. Using addition instead of multiplication for fee calculation
B. Incorrect variable names
C. Missing print statement
D. Gas price should be divided by gas used

Solution

  1. Step 1: Review the code logic

    The code adds gas_used and gas_price instead of multiplying them.
  2. Step 2: Understand correct fee calculation

    Transaction fee should be gas_used x gas_price, not addition.
  3. Final Answer:

    Using addition instead of multiplication for fee calculation -> Option A
  4. Quick Check:

    Fee = gas_used x gas_price, not + [OK]
Hint: Fees multiply gas used by gas price, not add [OK]
Common Mistakes:
  • Adding gas values instead of multiplying
  • Confusing variable names
  • Ignoring correct formula
5. A user wants to speed up their transaction by increasing the gas price from 40 gwei to 60 gwei. If the gas used remains 21000, how much more will the user pay in transaction fees compared to before?
hard
A. 600,000 gwei more
B. 1,260,000 gwei more
C. 420,000 gwei more
D. 840,000 gwei more

Solution

  1. Step 1: Calculate original fee

    Original fee = 21000 x 40 = 840,000 gwei.
  2. Step 2: Calculate new fee

    New fee = 21000 x 60 = 1,260,000 gwei.
  3. Step 3: Find difference

    Difference = 1,260,000 - 840,000 = 420,000 gwei more.
  4. Final Answer:

    420,000 gwei more -> Option C
  5. Quick Check:

    Increase = (60-40)x21000 = 420,000 [OK]
Hint: Subtract old fee from new fee to find extra cost [OK]
Common Mistakes:
  • Calculating total fee instead of difference
  • Mixing gas price and gas used
  • Incorrect multiplication
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