0
0
Blockchain / Solidityprogramming~20 mins

Sidechains in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sidechain 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 sidechain token transfer simulation?

Consider a simple Python simulation of a sidechain token transfer. What will be printed after running the code?

Blockchain / Solidity
class Sidechain:
    def __init__(self):
        self.balances = {"main": 1000, "side": 0}

    def lock_tokens(self, amount):
        if self.balances["main"] >= amount:
            self.balances["main"] -= amount
            self.balances["side"] += amount
            print(f"Locked {amount} tokens to sidechain")
        else:
            print("Insufficient tokens on main chain")

    def unlock_tokens(self, amount):
        if self.balances["side"] >= amount:
            self.balances["side"] -= amount
            self.balances["main"] += amount
            print(f"Unlocked {amount} tokens to main chain")
        else:
            print("Insufficient tokens on sidechain")

sc = Sidechain()
sc.lock_tokens(300)
sc.unlock_tokens(100)
print(sc.balances)
A{'main': 800, 'side': 200}
B{'main': 700, 'side': 300}
C{'main': 900, 'side': 100}
D{'main': 1000, 'side': 0}
Attempts:
2 left
💡 Hint

Think about how tokens move between main and sidechain balances after locking and unlocking.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a sidechain's purpose?

Choose the option that correctly explains why sidechains are used in blockchain technology.

ASidechains are used to permanently store all transaction data from the main blockchain to increase security.
BSidechains allow assets to move between different blockchains to improve scalability and add features without changing the main chain.
CSidechains replace the main blockchain entirely to provide a faster consensus mechanism.
DSidechains are private blockchains that cannot interact with the main blockchain.
Attempts:
2 left
💡 Hint

Think about how sidechains help with scalability and flexibility.

🔧 Debug
advanced
2:00remaining
Identify the error in this sidechain asset locking code

The following Python code simulates locking assets to a sidechain. It raises an error when run. What is the cause?

Blockchain / Solidity
class Sidechain:
    def __init__(self):
        self.balances = {"main": 500, "side": 0}

    def lock(self, amount):
        if self.balances["main"] >= amount:
            self.balances["main"] -= amount
            self.balances["side"] += amount
        else:
            print("Not enough tokens")

sc = Sidechain()
sc.lock(600)
ASyntaxError due to missing colon after else statement
BKeyError because 'side' key does not exist in balances
CTypeError because balances is not a dictionary
DRuntimeError due to negative balance
Attempts:
2 left
💡 Hint

Check the syntax of the if-else block carefully.

📝 Syntax
advanced
2:00remaining
Which option correctly implements a sidechain asset withdrawal function?

Choose the code snippet that correctly defines a method to withdraw tokens from a sidechain back to the main chain.

A
def withdraw(self, amount):
    if self.balances['side'] >= amount:
        self.balances['side'] -= amount
        self.balances['main'] =+ amount
    else:
        print('Insufficient sidechain balance')
B
def withdraw(self, amount):
    if self.balances['side'] > amount:
        self.balances['side'] -= amount
        self.balances['main'] += amount
    else
        print('Insufficient sidechain balance')
C
def withdraw(self, amount):
    if self.balances['side'] >= amount
        self.balances['side'] -= amount
        self.balances['main'] += amount
    else:
        print('Insufficient sidechain balance')
D
def withdraw(self, amount):
    if self.balances['side'] >= amount:
        self.balances['side'] -= amount
        self.balances['main'] += amount
    else:
        print('Insufficient sidechain balance')
Attempts:
2 left
💡 Hint

Look for correct syntax and correct operators.

🚀 Application
expert
2:30remaining
How many tokens remain on the sidechain after these operations?

Given the following sequence of operations on a sidechain, how many tokens remain on the sidechain?

sc = Sidechain()
sc.lock_tokens(400)
sc.unlock_tokens(150)
sc.lock_tokens(100)
sc.unlock_tokens(200)
print(sc.balances)
Blockchain / Solidity
class Sidechain:
    def __init__(self):
        self.balances = {"main": 1000, "side": 0}

    def lock_tokens(self, amount):
        if self.balances["main"] >= amount:
            self.balances["main"] -= amount
            self.balances["side"] += amount

    def unlock_tokens(self, amount):
        if self.balances["side"] >= amount:
            self.balances["side"] -= amount
            self.balances["main"] += amount
A350
B100
C150
D250
Attempts:
2 left
💡 Hint

Track each lock and unlock step carefully to update balances.