Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Contract structure and syntax in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Contract Structure and Syntax
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 contract's function call?

Consider the following Solidity contract snippet. What will be the output when getValue() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Simple {
    uint private value = 10;

    function getValue() public view returns (uint) {
        return value + 5;
    }
}
ACompilation error
B10
C15
D5
Attempts:
2 left
💡 Hint

Look at the value of value and what getValue() returns.

❓ Predict Output
intermediate
2:00remaining
What error does this Solidity contract produce?

What error will occur when compiling this Solidity contract?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint public x;

    function setX(uint _x) public {
        x = _x;
    }

    function setX(uint _x) public {
        x = _x * 2;
    }
}
ARuntime error: Duplicate function call
BSyntaxError: Missing semicolon
CNo error, compiles successfully
DTypeError: Function overloading not allowed with same parameter types
Attempts:
2 left
💡 Hint

Check if Solidity allows two functions with the same name and parameters.

🔧 Debug
advanced
2:00remaining
Why does this contract fail to compile?

Identify the reason this Solidity contract fails to compile.

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Counter {
    uint count;

    function increment() public {
        count += 1;
    }

    function getCount() public view returns (uint) {
        return count;
    }
}
AVariable <code>count</code> is not initialized
BMissing semicolon after <code>count += 1</code>
CFunction <code>increment</code> must be declared <code>view</code>
DReturn type of <code>getCount</code> is incorrect
Attempts:
2 left
💡 Hint

Look carefully at the line inside increment().

🧠 Conceptual
advanced
2:00remaining
What is the visibility of the variable data in this contract?

Given this Solidity contract, what is the visibility of the variable data?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Storage {
    string data;

    function setData(string memory _data) public {
        data = _data;
    }

    function getData() public view returns (string memory) {
        return data;
    }
}
AInternal (accessible in this contract and derived contracts)
BPublic (accessible from outside the contract)
CExternal (only callable from outside)
DPrivate (only accessible inside the contract)
Attempts:
2 left
💡 Hint

State variables without explicit visibility are internal by default in Solidity.

❓ Predict Output
expert
2:00remaining
What is the output of this contract's function call with inheritance?

Consider these two contracts. What will be the output of getName() when called on Child?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Parent {
    function getName() public pure virtual returns (string memory) {
        return "Parent";
    }
}

contract Child is Parent {
    function getName() public pure override returns (string memory) {
        return "Child";
    }
}
A"Child"
B"Parent"
CCompilation error due to missing override keyword
DRuntime error: Function not found
Attempts:
2 left
💡 Hint

Look at how getName() is overridden in Child.

Practice

(1/5)
1.

What is the correct way to start defining a contract named SimpleStorage in Solidity?

easy
A. contract SimpleStorage { }
B. class SimpleStorage { }
C. function SimpleStorage() { }
D. module SimpleStorage { }

Solution

  1. Step 1: Understand Solidity contract declaration

    In Solidity, contracts are declared using the keyword contract followed by the contract name and curly braces.
  2. Step 2: Match the syntax with the options

    Only contract SimpleStorage { } uses contract SimpleStorage { }, which is the correct syntax to start a contract.
  3. Final Answer:

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

    Contract declaration = contract SimpleStorage { } [OK]
Hint: Look for 'contract' keyword to start a contract [OK]
Common Mistakes:
  • Using 'class' instead of 'contract'
  • Using 'function' to define contract
  • Using 'module' which is not valid in Solidity
2.

Which of the following is the correct syntax to declare a state variable count of type uint inside a contract?

easy
A. uint count = 0
B. var count = uint;
C. uint count;
D. count uint;

Solution

  1. Step 1: Recall Solidity variable declaration syntax

    State variables are declared by specifying the type followed by the variable name and a semicolon.
  2. Step 2: Check each option

    uint count; correctly declares uint count;. uint count = 0 misses the semicolon. Options B and D use incorrect syntax.
  3. Final Answer:

    uint count; -> Option C
  4. Quick Check:

    Variable declaration = type name; [OK]
Hint: Remember: type then variable name, end with semicolon [OK]
Common Mistakes:
  • Omitting semicolon at the end
  • Using 'var' keyword which is not valid in Solidity
  • Reversing type and variable name
3.

What will be the output of the following Solidity function when called?

contract Test {
    uint number = 5;
    function getNumber() public view returns (uint) {
        return number + 3;
    }
}

medium
A. 5
B. 8
C. 3
D. Compilation error

Solution

  1. Step 1: Understand the variable and function

    The state variable number is set to 5. The function returns number + 3.
  2. Step 2: Calculate the return value

    5 + 3 equals 8, so the function returns 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    5 + 3 = 8 [OK]
Hint: Add variable value and constant inside return [OK]
Common Mistakes:
  • Confusing variable value with function output
  • Assuming function returns variable directly without addition
  • Thinking there is a syntax error
4.

Identify the error in the following Solidity contract snippet:

contract Counter {
    uint count
    function increment() public {
        count += 1;
    }
}

medium
A. Contract name should be lowercase
B. Function increment should be private
C. Variable count should be declared inside the function
D. Missing semicolon after uint count

Solution

  1. Step 1: Check variable declaration syntax

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

    Function visibility as public is valid. Variables are correctly declared outside functions. Contract names can be uppercase.
  3. Final Answer:

    Missing semicolon after uint count -> Option D
  4. Quick Check:

    Semicolon missing = syntax error [OK]
Hint: Check for missing semicolons after variable declarations [OK]
Common Mistakes:
  • Forgetting semicolon after variable declaration
  • Thinking function visibility must be private
  • Assuming contract names must be lowercase
5.

You want to create a contract that stores a person's name and age, and has a function to update the age. Which of the following contract structures correctly implements this?

contract Person {
    string name;
    uint age;

    function setAge(uint newAge) public {
        age = newAge;
    }
}
hard
A. Correct: variables declared and function updates age properly
B. Incorrect: missing visibility for variables
C. Incorrect: function should return the new age
D. Incorrect: variables should be declared inside the function

Solution

  1. Step 1: Check variable declarations

    Variables name and age are declared at contract level, which is correct for storing data.
  2. Step 2: Check function to update age

    The function setAge takes a parameter and assigns it to age, correctly updating the state variable.
  3. Final Answer:

    Correct: variables declared and function updates age properly -> Option A
  4. Quick Check:

    State variables + update function = correct contract [OK]
Hint: State variables outside, update functions inside contract [OK]
Common Mistakes:
  • Declaring variables inside functions instead of contract
  • Expecting update functions to return values unnecessarily
  • Forgetting to declare variables at contract level
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