0
0
Blockchain / Solidityprogramming~20 mins

Blocks, chains, and hashing in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blockchain Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this simple block hash calculation?
Consider a block with data 'Block1' and previous hash '0000'. The hash function returns the length of the combined string. What is the output of the hash calculation?
Blockchain / Solidity
data = 'Block1'
previous_hash = '0000'
block_string = data + previous_hash
block_hash = len(block_string)
print(block_hash)
A11
B10
C9
D12
Attempts:
2 left
💡 Hint
Count the characters in 'Block1' plus '0000'.
🧠 Conceptual
intermediate
1:30remaining
Which property of a cryptographic hash ensures blockchain security?
Which property of a cryptographic hash function makes it hard to change a block without changing its hash?
AFast computation
BDeterministic output
CCollision resistance
DFixed output length
Attempts:
2 left
💡 Hint
Think about how hard it is to find two inputs with the same hash.
🔧 Debug
advanced
2:30remaining
Why does this blockchain chain break?
This code tries to link blocks by storing previous hashes. Why does the chain verification fail?
Blockchain / Solidity
blocks = [
  {'data': 'Block1', 'prev_hash': '0000', 'hash': 'abc123'},
  {'data': 'Block2', 'prev_hash': 'abc123', 'hash': 'def456'},
  {'data': 'Block3', 'prev_hash': 'xyz789', 'hash': 'ghi012'}
]

for i in range(1, len(blocks)):
    if blocks[i]['prev_hash'] != blocks[i-1]['hash']:
        print(f"Chain broken at block {i+1}")
        break
else:
    print("Chain is valid")
ABecause block 3's prev_hash does not match block 2's hash
BBecause block 2's hash is incorrect
CBecause block 1's prev_hash is not '0000'
DBecause the loop does not check the first block
Attempts:
2 left
💡 Hint
Check the prev_hash of block 3 and the hash of block 2.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates a block hash using SHA-256?
Select the code that correctly computes a SHA-256 hash of a block's data and previous hash in Python.
A
import hashlib
block_string = data + prev_hash
block_hash = hashlib.sha256(block_string.encode()).hexdigest()
B
import hashlib
block_hash = hashlib.sha256(data + prev_hash).hexdigest()
C
import hashlib
block_hash = hashlib.sha256(block_string.encode).hexdigest()
D
import hashlib
block_hash = hashlib.sha256(block_string.encode()).digest()
Attempts:
2 left
💡 Hint
Remember to encode the string before hashing and use hexdigest() for readable output.
🚀 Application
expert
3:00remaining
How many blocks are in the valid chain after this code runs?
Given this blockchain list, how many blocks form a valid chain starting from the first block?
Blockchain / Solidity
blockchain = [
  {'data': 'Genesis', 'prev_hash': '0', 'hash': 'a1'},
  {'data': 'Block1', 'prev_hash': 'a1', 'hash': 'b2'},
  {'data': 'Block2', 'prev_hash': 'b2', 'hash': 'c3'},
  {'data': 'Block3', 'prev_hash': 'x9', 'hash': 'd4'},
  {'data': 'Block4', 'prev_hash': 'd4', 'hash': 'e5'}
]

valid_count = 1
for i in range(1, len(blockchain)):
    if blockchain[i]['prev_hash'] == blockchain[i-1]['hash']:
        valid_count += 1
    else:
        break
print(valid_count)
A2
B4
C5
D3
Attempts:
2 left
💡 Hint
Check where the prev_hash does not match the previous block's hash.