Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Why logic controls execution in Blockchain / Solidity - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Logic Mastery in Blockchain
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 smart contract function?

Consider this simplified smart contract function that controls token transfer based on a condition.

function transfer(uint amount) public returns (string memory) {
    if (amount > 100) {
        return "Transfer denied: amount too large";
    } else {
        return "Transfer successful";
    }
}

What will be the output when transfer(150) is called?

Blockchain / Solidity
function transfer(uint amount) public returns (string memory) {
    if (amount > 100) {
        return "Transfer denied: amount too large";
    } else {
        return "Transfer successful";
    }
}
ARuntime error
B"Transfer successful"
CCompilation error
D"Transfer denied: amount too large"
Attempts:
2 left
💡 Hint

Think about what the if condition checks and what happens when the amount is 150.

❓ Predict Output
intermediate
2:00remaining
What does this Solidity code output?

Look at this Solidity function that uses logic to decide the fee.

function calculateFee(uint amount) public pure returns (uint) {
    if (amount <= 50) {
        return 1;
    } else if (amount <= 100) {
        return 2;
    } else {
        return 5;
    }
}

What is the output of calculateFee(75)?

Blockchain / Solidity
function calculateFee(uint amount) public pure returns (uint) {
    if (amount <= 50) {
        return 1;
    } else if (amount <= 100) {
        return 2;
    } else {
        return 5;
    }
}
A2
B5
C1
D0
Attempts:
2 left
💡 Hint

Check which condition 75 satisfies.

🔧 Debug
advanced
2:00remaining
Why does this smart contract function always revert?

Here is a Solidity function meant to approve spending only if the amount is positive.

function approve(uint amount) public returns (bool) {
    require(amount < 0, "Amount must be positive");
    return true;
}

Why does this function always revert?

Blockchain / Solidity
function approve(uint amount) public returns (bool) {
    require(amount < 0, "Amount must be positive");
    return true;
}
ABecause amount can never be less than 0 since it's unsigned, so require always fails
BBecause the function should use if instead of require
CBecause the function is missing a return statement
DBecause require needs a semicolon after the message string
Attempts:
2 left
💡 Hint

Think about the type of amount and what the condition checks.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this Solidity function?

Look at this function that uses logic to check ownership.

function checkOwner(address user) public view returns (bool) {
    if user == owner {
        return true;
    } else {
        return false;
    }
}

Which option shows the code with a syntax error?

Blockchain / Solidity
function checkOwner(address user) public view returns (bool) {
    if user == owner {
        return true;
    } else {
        return false;
    }
}
Aif (user == owner) { return true; } else { return false; }
Breturn user == owner;
Cif user == owner { return true; } else { return false; }
Dif (user == owner) return true; else return false;
Attempts:
2 left
💡 Hint

Remember how conditions are written in Solidity.

🚀 Application
expert
3:00remaining
How many times will the event be emitted in this Solidity loop?

Consider this Solidity function that emits an event based on a logic condition inside a loop.

event LogNumber(uint number);

function emitEvenNumbers() public {
    for (uint i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            emit LogNumber(i);
        }
    }
}

How many times will LogNumber be emitted when emitEvenNumbers() is called?

Blockchain / Solidity
event LogNumber(uint number);

function emitEvenNumbers() public {
    for (uint i = 0; i < 5; i++) {
        if (i % 2 == 0) {
            emit LogNumber(i);
        }
    }
}
A4
B3
C2
D5
Attempts:
2 left
💡 Hint

Count how many numbers from 0 to 4 are even.

Practice

(1/5)
1. Why is logic important in blockchain programming?
easy
A. It creates new cryptocurrencies automatically
B. It stores data permanently on the blockchain
C. It controls which parts of the code run based on conditions
D. It speeds up the blockchain network

Solution

  1. Step 1: Understand the role of logic in code

    Logic decides which code runs by checking conditions, like a traffic light controlling cars.
  2. Step 2: Apply this to blockchain context

    In blockchain, logic ensures only allowed actions happen, protecting assets and data.
  3. Final Answer:

    It controls which parts of the code run based on conditions -> Option C
  4. Quick Check:

    Logic controls execution = D [OK]
Hint: Logic decides code flow by conditions, like a gatekeeper [OK]
Common Mistakes:
  • Confusing logic with data storage
  • Thinking logic speeds network
  • Assuming logic creates cryptocurrencies
2. Which of the following is the correct syntax for an if statement in Solidity (blockchain language)?
easy
A. if (balance > 0) { sendFunds(); }
B. if balance > 0 then sendFunds()
C. if balance > 0 { sendFunds(); }
D. if (balance > 0) sendFunds;

Solution

  1. Step 1: Recall Solidity if statement syntax

    Solidity uses C-style syntax: condition in parentheses, code block in braces.
  2. Step 2: Check each option

    if (balance > 0) { sendFunds(); } matches correct syntax with parentheses and braces; others miss parentheses, braces, or semicolon.
  3. Final Answer:

    if (balance > 0) { sendFunds(); } -> Option A
  4. Quick Check:

    Solidity if syntax = C [OK]
Hint: If statements need parentheses and braces in Solidity [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Missing braces for code block
  • Leaving out semicolons
3. What will be the output of this Solidity function when amount = 50?
function checkAmount(uint amount) public pure returns (string memory) {
  if (amount > 100) {
    return "High";
  } else if (amount > 20) {
    return "Medium";
  } else {
    return "Low";
  }
}
medium
A. "High"
B. Compilation error
C. "Low"
D. "Medium"

Solution

  1. Step 1: Evaluate the if conditions with amount = 50

    50 > 100 is false, so skip first if; 50 > 20 is true, so return "Medium".
  2. Step 2: Confirm no errors in code

    Code syntax is correct, so no compilation error.
  3. Final Answer:

    "Medium" -> Option D
  4. Quick Check:

    50 > 20 = true, output = "Medium" [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
  • Choosing "High" because 50 is large
  • Ignoring else if condition order
  • Thinking code has syntax errors
4. Identify the error in this Solidity code snippet:
function transfer(uint amount) public {
  if amount > balance {
    revert("Insufficient balance");
  }
  balance -= amount;
}
medium
A. Missing parentheses around the if condition
B. Using revert instead of throw
C. balance should be a local variable
D. Missing semicolon after revert statement

Solution

  1. Step 1: Check if statement syntax

    In Solidity, conditions must be inside parentheses: if (condition) {...}
  2. Step 2: Verify other parts

    Using revert is correct for errors; semicolon is present; balance can be state variable.
  3. Final Answer:

    Missing parentheses around the if condition -> Option A
  4. Quick Check:

    If condition needs parentheses = A [OK]
Hint: If conditions always need parentheses in Solidity [OK]
Common Mistakes:
  • Thinking revert is wrong
  • Assuming balance must be local
  • Missing that parentheses are required
5. You want to write a Solidity function that only allows transfers if the sender is the owner and the amount is less than the balance. Which logic correctly controls execution?
hard
A. if (msg.sender == owner || amount < balance) { transfer(amount); } else { revert("Not allowed"); }
B. if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); }
C. if (msg.sender != owner && amount > balance) { transfer(amount); } else { revert("Not allowed"); }
D. if (msg.sender != owner || amount > balance) { transfer(amount); } else { revert("Not allowed"); }

Solution

  1. Step 1: Understand the required conditions

    Both conditions must be true: sender is owner AND amount less than balance.
  2. Step 2: Check each option's logic

    if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } uses && (AND) correctly; B uses || (OR) which allows wrong cases; C and D use negations incorrectly.
  3. Final Answer:

    if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } -> Option B
  4. Quick Check:

    Use AND for both conditions true = A [OK]
Hint: Use AND (&&) to require all conditions true [OK]
Common Mistakes:
  • Using OR (||) instead of AND (&&)
  • Negating conditions incorrectly
  • Allowing transfers when conditions fail
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard