0
0
Blockchain / Solidityprogramming~10 mins

Why testing prevents costly bugs in Blockchain / Solidity - Test Your Understanding

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

Complete the code to define a simple test function that checks if a smart contract function returns the expected value.

Blockchain / Solidity
def test_get_balance():
    expected = 100
    result = get_balance()
    assert result [1] expected
Drag options to blanks, or click blank then click option'
A==
B!=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes the test to fail even if the function is correct.
Using comparison operators like '>' or '<' does not check for exact equality.
2fill in blank
medium

Complete the code to simulate a transaction and check if the balance updates correctly.

Blockchain / Solidity
def test_transaction():
    initial_balance = 100
    amount = 20
    new_balance = initial_balance - [1]
    assert new_balance == 80
Drag options to blanks, or click blank then click option'
Ainitial_balance
Bamount
C20
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting the initial balance from itself results in zero.
Using a fixed number instead of the variable reduces flexibility.
3fill in blank
hard

Fix the error in the test that checks if a smart contract function raises an exception on invalid input.

Blockchain / Solidity
def test_invalid_input():
    try:
        process_transaction(-10)
    except [1]:
        passed = True
    else:
        passed = False
    assert passed
Drag options to blanks, or click blank then click option'
ATypeError
BZeroDivisionError
CValueError
DIndexError
Attempts:
3 left
💡 Hint
Common Mistakes
Catching the wrong exception type causes the test to fail.
Not catching any exception leads to unhandled errors.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps transaction IDs to their amounts, but only for amounts greater than 50.

Blockchain / Solidity
transactions = {tx_id: amount for tx_id, amount in tx_list if amount [1] 50 and amount [2] 0}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' reverses the filter logic.
Using '<=' instead of '>=' allows negative amounts.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps user addresses to their balances, but only include users with balances greater than zero.

Blockchain / Solidity
user_balances = { [3].[1]: [3].[2] for [3] in users if [3].[2] > 0 }
Drag options to blanks, or click blank then click option'
Aaddress
Bbalance
Cuser
Damount
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causes errors.
Not filtering for positive balances includes invalid data.