Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Smart contract concept in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Smart Contract 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 Solidity function call?

Consider this Solidity smart contract snippet:

pragma solidity ^0.8.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

If we call set(42) and then get(), what will get() return?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}
ATransaction revert error
B42
CCompilation error
D0
Attempts:
2 left
💡 Hint

Think about what the set function does before calling get.

🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a smart contract?

Choose the best description of a smart contract in blockchain technology.

AA centralized database storing user information securely.
BA legal contract signed digitally between two parties.
CA self-executing program that runs on a blockchain and enforces rules automatically.
DA cryptocurrency wallet used to store tokens.
Attempts:
2 left
💡 Hint

Think about what makes smart contracts different from traditional contracts.

🔧 Debug
advanced
2:00remaining
What error does this Solidity code produce?

Examine this Solidity function:

function divide(uint a, uint b) public pure returns (uint) {
    return a / b;
}

What happens if b is zero when calling divide(10, 0)?

Blockchain / Solidity
function divide(uint a, uint b) public pure returns (uint) {
    return a / b;
}
AThrows a division by zero runtime error and reverts the transaction
BCompiles with a warning but runs normally
CReturns 0
DReturns a large number due to overflow
Attempts:
2 left
💡 Hint

What happens in programming when you divide by zero?

📝 Syntax
advanced
1:30remaining
Which option correctly declares a payable function in Solidity?

In Solidity, which function declaration allows the function to receive Ether?

Afunction deposit() public payable {}
Bfunction deposit() public pay {}
Cfunction deposit() public payable returns (uint) {}
Dfunction deposit() public payable() {}
Attempts:
2 left
💡 Hint

Look for the correct keyword and syntax for payable functions.

🚀 Application
expert
2:30remaining
How many items are in the mapping after this transaction?

Given this Solidity contract snippet:

contract Registry {
    mapping(address => bool) public registered;

    function register() public {
        registered[msg.sender] = true;
    }
}

If three different users call register() once each, how many entries will registered contain?

A1
BCannot be determined
C0
D3
Attempts:
2 left
💡 Hint

Think about how mappings store data for unique keys.

Practice

(1/5)
1. What is a smart contract in blockchain technology?
easy
A. A type of cryptocurrency wallet
B. A program that runs on a blockchain to automate agreements
C. A physical contract signed by two parties
D. A blockchain mining algorithm

Solution

  1. Step 1: Understand the definition of smart contracts

    Smart contracts are computer programs that run on blockchains to automate and enforce agreements without middlemen.
  2. Step 2: Compare options with the definition

    Only A program that runs on a blockchain to automate agreements describes a program on blockchain automating agreements. Other options describe unrelated concepts.
  3. Final Answer:

    A program that runs on a blockchain to automate agreements -> Option B
  4. Quick Check:

    Smart contract = automated blockchain program [OK]
Hint: Smart contracts are code on blockchain, not physical papers [OK]
Common Mistakes:
  • Confusing smart contracts with physical contracts
  • Thinking smart contracts are wallets or mining tools
  • Assuming smart contracts require middlemen
2. Which of the following is the correct way to start a simple smart contract in Solidity?
easy
A. contract MyContract { }
B. class MyContract { }
C. function MyContract() { }
D. module MyContract { }

Solution

  1. Step 1: Recall Solidity syntax for defining contracts

    In Solidity, smart contracts start with the keyword contract followed by the contract name and braces.
  2. Step 2: Check each option's syntax

    contract MyContract { } uses contract keyword correctly. Options A, B, and C use keywords from other languages or incorrect syntax.
  3. Final Answer:

    contract MyContract { } -> Option A
  4. Quick Check:

    Solidity contracts start with 'contract' keyword [OK]
Hint: Solidity contracts start with 'contract' keyword, not class or function [OK]
Common Mistakes:
  • Using 'class' instead of 'contract' keyword
  • Confusing functions with contract declarations
  • Using module or other unrelated keywords
3. What will be the output of this Solidity function when called?
function getValue() public pure returns (uint) { return 5 + 3; }
medium
A. Error
B. 53
C. 8
D. 0

Solution

  1. Step 1: Analyze the function logic

    The function returns the sum of 5 and 3, which is 8.
  2. Step 2: Understand Solidity return behavior

    The function is marked pure and returns a uint, so it will output the calculated value 8 without errors.
  3. Final Answer:

    8 -> Option C
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Add numbers inside function to get output quickly [OK]
Common Mistakes:
  • Thinking '5 + 3' concatenates as '53'
  • Expecting runtime error due to missing state
  • Assuming default return is zero
4. Identify the error in this Solidity smart contract snippet:
contract Test {
  uint value;
  function setValue(uint _value) public {
    value = _value
  }
}
medium
A. Missing semicolon after assignment
B. Incorrect function visibility
C. Variable 'value' should be public
D. Function name should be 'set_value'

Solution

  1. Step 1: Check syntax line by line

    The line value = _value is missing a semicolon at the end, which is required in Solidity.
  2. Step 2: Verify other parts

    Function visibility 'public' is correct, variable can be private, and function naming is valid. So only missing semicolon is error.
  3. Final Answer:

    Missing semicolon after assignment -> Option A
  4. Quick Check:

    Semicolons end statements in Solidity [OK]
Hint: Check for missing semicolons after statements [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking function visibility is wrong
  • Assuming variable must be public
5. You want to create a smart contract that stores user balances and allows deposits only if the amount is positive. Which Solidity code snippet correctly enforces this?
hard
A. function deposit(uint amount) public { if(amount >= 0) { balances[msg.sender] = amount; } }
B. function deposit(uint amount) public { balances[msg.sender] += amount; }
C. function deposit(int amount) public { balances[msg.sender] += amount; }
D. function deposit(uint amount) public { require(amount > 0, "Amount must be positive"); balances[msg.sender] += amount; }

Solution

  1. Step 1: Check input validation for positive amount

    function deposit(uint amount) public { require(amount > 0, "Amount must be positive"); balances[msg.sender] += amount; } uses require(amount > 0) to ensure only positive deposits are accepted.
  2. Step 2: Compare other options for correctness

    function deposit(uint amount) public { if(amount >= 0) { balances[msg.sender] = amount; } } allows zero and overwrites balance instead of adding. function deposit(int amount) public { balances[msg.sender] += amount; } uses signed int allowing negative deposits. function deposit(uint amount) public { balances[msg.sender] += amount; } lacks any check.
  3. Final Answer:

    function deposit(uint amount) public { require(amount > 0, "Amount must be positive"); balances[msg.sender] += amount; } -> Option D
  4. Quick Check:

    Use require() to enforce positive deposits [OK]
Hint: Use require() to check conditions before updating state [OK]
Common Mistakes:
  • Not validating input amounts
  • Using signed integers for balances
  • Overwriting balances instead of adding
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