Imagine a blockchain network where transactions are recorded. Why does strong security in this network help prevent financial loss?
Think about what happens if someone can change transaction records without permission.
Strong security prevents unauthorized changes to transaction data. This keeps funds safe by ensuring only valid transactions happen.
Given this simplified smart contract code snippet, what will be the output after running it?
balances = {'Alice': 100, 'Bob': 50}
# Alice tries to send 30 to Bob
amount = 30
sender = 'Alice'
receiver = 'Bob'
if balances.get(sender, 0) >= amount:
balances[sender] -= amount
balances[receiver] += amount
print(f"Transfer successful: {sender} -> {receiver} : {amount}")
else:
print("Transfer failed: insufficient funds")Check if Alice has enough balance before sending.
Alice has 100, which is enough to send 30 to Bob. So the transfer succeeds and prints the success message.
Look at this code that handles a transaction. Which option correctly identifies the error that could cause financial loss?
def transfer_funds(balances, sender, receiver, amount): if balances[sender] > amount: balances[sender] -= amount balances[receiver] += amount return "Transfer complete" else: return "Insufficient funds"
Think about what happens if sender tries to send all their money.
The condition uses > which blocks transfers equal to the sender's full balance. This can cause confusion or loss of funds if exact transfers are needed.
What error will this code produce when executed?
transactions = [{'from': 'Alice', 'to': 'Bob', 'amount': 50}]
for tx in transactions:
print(f"{tx['from']} sends {tx['amount']} to {tx['to']}")Check the for loop syntax carefully.
The for loop is missing a colon at the end of the line, causing a SyntaxError.
Consider this code that records transactions in a blockchain ledger. How many unique transactions will the ledger contain after execution?
ledger = set() transactions = [ ('Alice', 'Bob', 20), ('Bob', 'Charlie', 15), ('Alice', 'Bob', 20), ('Charlie', 'Alice', 5) ] for tx in transactions: ledger.add(tx) print(len(ledger))
Sets do not allow duplicate entries. Count unique tuples.
The transaction ('Alice', 'Bob', 20) appears twice but is stored once in the set. So total unique transactions are 3.