0
0
Blockchain / Solidityprogramming~20 mins

Structs in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Struct 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 struct initialization?

Consider the following Solidity code snippet:

pragma solidity ^0.8.0;

contract Test {
    struct Person {
        string name;
        uint age;
    }

    Person public p = Person("Alice", 30);

    function getAge() public view returns (uint) {
        return p.age;
    }
}

What will getAge() return when called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    struct Person {
        string name;
        uint age;
    }

    Person public p = Person("Alice", 30);

    function getAge() public view returns (uint) {
        return p.age;
    }
}
AAlice
B30
CCompilation error
D0
Attempts:
2 left
💡 Hint

Look at how the struct is initialized and what the getAge function returns.

Predict Output
intermediate
2:00remaining
What is the value of the struct field after assignment?

Given this Solidity code:

pragma solidity ^0.8.0;

contract Example {
    struct Data {
        uint x;
        bool flag;
    }

    Data public d;

    function setData() public {
        d.x = 10;
        d.flag = true;
    }

    function getFlag() public view returns (bool) {
        return d.flag;
    }
}

What does getFlag() return after setData() is called?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Example {
    struct Data {
        uint x;
        bool flag;
    }

    Data public d;

    function setData() public {
        d.x = 10;
        d.flag = true;
    }

    function getFlag() public view returns (bool) {
        return d.flag;
    }
}
A0
Bfalse
Ctrue
DCompilation error
Attempts:
2 left
💡 Hint

Check what value is assigned to d.flag in setData().

🔧 Debug
advanced
2:00remaining
Why does this Solidity struct code cause a compilation error?

Examine this Solidity code snippet:

pragma solidity ^0.8.0;

contract DebugTest {
    struct Item {
        string name;
        uint price;
    }

    Item public item = Item("Book", 20);

    function updatePrice(uint newPrice) public {
        item.price = newPrice;
    }

    function getName() public view returns (string memory) {
        return item.name;
    }
}

When compiling, an error occurs related to the struct initialization line. What is the cause?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract DebugTest {
    struct Item {
        string name;
        uint price;
    }

    Item public item = Item("Book", 20);

    function updatePrice(uint newPrice) public {
        item.price = newPrice;
    }

    function getName() public view returns (string memory) {
        return item.name;
    }
}
AThe code is valid and compiles without errors.
BThe string literal "Book" must be declared as bytes32, not string.
CThe compiler requires the constructor to initialize the struct, not inline assignment.
DStructs cannot be initialized inline with parameters in Solidity.
Attempts:
2 left
💡 Hint

Check Solidity version and struct initialization rules.

📝 Syntax
advanced
2:00remaining
Which option causes a syntax error when defining a struct in Solidity?

Which of the following struct definitions will cause a syntax error in Solidity?

Astruct Person { string name; uint age; }
B} ;ega tniu ;eman gnirts { nosreP tcurts
Cstruct Person { string name; uint age; bool isActive; }
Dstruct Person { string name uint age; }
Attempts:
2 left
💡 Hint

Look for missing punctuation in the struct fields.

🚀 Application
expert
2:00remaining
How many items are in the array inside this struct after execution?

Consider this Solidity contract:

pragma solidity ^0.8.0;

contract Inventory {
    struct Product {
        string name;
        uint[] batchNumbers;
    }

    Product public product;

    function addBatch(uint batch) public {
        product.batchNumbers.push(batch);
    }

    function getBatchCount() public view returns (uint) {
        return product.batchNumbers.length;
    }
}

If addBatch(101) is called twice, what will getBatchCount() return?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Inventory {
    struct Product {
        string name;
        uint[] batchNumbers;
    }

    Product public product;

    function addBatch(uint batch) public {
        product.batchNumbers.push(batch);
    }

    function getBatchCount() public view returns (uint) {
        return product.batchNumbers.length;
    }
}
A2
B1
C0
DCompilation error
Attempts:
2 left
💡 Hint

Each call to addBatch adds one element to the array.