0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 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.