0
0
Blockchain / Solidityprogramming~20 mins

Proof of Work vs Proof of Stake in Blockchain / Solidity - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Consensus 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 Proof of Work simulation code?

Consider this simplified Python code simulating a Proof of Work mining attempt. What will it print?

Blockchain / Solidity
import hashlib

def proof_of_work(data, difficulty):
    nonce = 0
    prefix = '0' * difficulty
    while True:
        text = f'{data}{nonce}'
        hash_result = hashlib.sha256(text.encode()).hexdigest()
        if hash_result.startswith(prefix):
            return nonce, hash_result
        nonce += 1

nonce, hash_val = proof_of_work('block', 2)
print(nonce, hash_val[:4])
AIt depends on the machine speed and may vary
B5 00c1
C0 00ab
D12 00f3
Attempts:
2 left
💡 Hint

The nonce is found by trial and error to get a hash starting with two zeros. This depends on the hash function output and cannot be predicted exactly.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes Proof of Stake?

Choose the correct description of how Proof of Stake (PoS) consensus works in blockchain.

ATransactions are validated by solving cryptographic puzzles faster than others.
BBlocks are created randomly without any stake or resource requirement.
CMiners solve complex puzzles to validate transactions and create blocks.
DValidators are chosen based on the amount of cryptocurrency they hold and lock up.
Attempts:
2 left
💡 Hint

Think about how PoS selects who creates the next block based on ownership rather than computational work.

🔧 Debug
advanced
2:00remaining
Why does this Proof of Work code raise an error?

Identify the error in this Proof of Work Python code snippet and what error it raises.

Blockchain / Solidity
import hashlib

def pow(data, difficulty):
    nonce = 0
    prefix = '0' * difficulty
    while True:
        text = f'{data}{nonce}'
        hash_val = hashlib.sha256(text.encode()).hexdigest()
        if hash_val.startswith(prefix):
            return nonce
        nonce += 1

pow('block', 3)
ASyntaxError due to missing colon after while True
BTypeError because prefix is not a string
CNameError because hashlib is not imported
DRuntimeError due to infinite loop
Attempts:
2 left
💡 Hint

Check the syntax of the while loop line carefully.

Predict Output
advanced
1:30remaining
What is the output of this Proof of Stake validator selection code?

This Python code simulates selecting a validator based on stake weights. What does it print?

Blockchain / Solidity
import random

validators = {'Alice': 50, 'Bob': 30, 'Carol': 20}

random.seed(1)
choices = list(validators.keys())
weights = list(validators.values())
selected = random.choices(choices, weights=weights, k=1)[0]
print(selected)
ABob
BAlice
CCarol
DRandom selection with equal chance for all
Attempts:
2 left
💡 Hint

random.choices with weights picks based on stake amounts.

🧠 Conceptual
expert
1:30remaining
Which is a key difference between Proof of Work and Proof of Stake?

Select the statement that best highlights a fundamental difference between Proof of Work (PoW) and Proof of Stake (PoS) consensus mechanisms.

APoW is energy efficient; PoS consumes large amounts of electricity.
BPoW uses stake to select validators; PoS uses mining hardware to solve puzzles.
CPoW requires validators to solve puzzles using computational power; PoS selects validators based on their cryptocurrency holdings.
DPoW validators are chosen randomly; PoS validators compete to solve cryptographic puzzles.
Attempts:
2 left
💡 Hint

Think about what resource each consensus mechanism uses to secure the network.