Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Structs in Blockchain / Solidity - Practice Problems & Coding Challenges

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

Practice

(1/5)
1.

What is the main purpose of using structs in blockchain programming?

easy
A. To perform mathematical calculations faster
B. To group related data together under one name
C. To create loops for repeating tasks
D. To store only single values like integers

Solution

  1. Step 1: Understand what structs do

    Structs are used to group different pieces of related data into one unit.
  2. Step 2: Compare options

    Only To group related data together under one name correctly describes grouping related data. Other options describe unrelated tasks.
  3. Final Answer:

    To group related data together under one name -> Option B
  4. Quick Check:

    Structs = Group related data [OK]
Hint: Think of structs as folders holding related info [OK]
Common Mistakes:
  • Confusing structs with loops or calculations
  • Thinking structs store only single values
  • Assuming structs speed up math operations
2.

Which of the following is the correct syntax to define a struct named Person with fields name (string) and age (uint)?

?
easy
A. struct Person(string name, uint age);
B. struct Person = (string name, uint age);
C. Person struct { string name; uint age; }
D. struct Person { string name; uint age; }

Solution

  1. Step 1: Recall struct syntax in blockchain languages

    Structs are defined with the keyword struct, followed by the name and curly braces containing fields with types and names.
  2. Step 2: Check each option

    struct Person { string name; uint age; } matches the correct syntax. Options B, C, and D use incorrect punctuation or order.
  3. Final Answer:

    struct Person { string name; uint age; } -> Option D
  4. Quick Check:

    Correct struct syntax = struct Person { string name; uint age; } [OK]
Hint: Remember: struct Name { type field; } [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Assigning struct like a variable
  • Omitting semicolons after fields
3.

Given the struct and code below, what will be the output?

struct Car {
  string brand;
  uint year;
}

Car myCar = Car("Tesla", 2020);

print(myCar.brand);
medium
A. Tesla
B. 2020
C. Car
D. Error: Cannot print struct field

Solution

  1. Step 1: Understand struct initialization

    The struct Car is created with brand "Tesla" and year 2020.
  2. Step 2: Identify printed field

    The code prints myCar.brand, which is "Tesla".
  3. Final Answer:

    Tesla -> Option A
  4. Quick Check:

    Print myCar.brand = Tesla [OK]
Hint: Print the field name, not the whole struct [OK]
Common Mistakes:
  • Printing the whole struct instead of a field
  • Confusing brand with year
  • Expecting an error when printing a field
4.

What is wrong with the following struct usage?

struct Book {
  string title;
  uint pages;
}

Book myBook;
myBook.title = "Learn Blockchain";
myBook.pages = "300";
medium
A. Struct name should be lowercase
B. Missing semicolon after struct definition
C. Cannot assign string to pages which is uint
D. Cannot assign values after declaration

Solution

  1. Step 1: Check field types and assignments

    The field pages is of type uint, but assigned a string "300".
  2. Step 2: Identify type mismatch error

    Assigning a string to a number type causes a type error.
  3. Final Answer:

    Cannot assign string to pages which is uint -> Option C
  4. Quick Check:

    Type mismatch in assignment = Cannot assign string to pages which is uint [OK]
Hint: Match field types exactly when assigning values [OK]
Common Mistakes:
  • Assigning string to a number field
  • Ignoring type errors
  • Thinking struct names must be lowercase
5.

You want to store multiple Employee structs in a list and then find the average age. Which approach correctly uses structs and arrays?

struct Employee {
  string name;
  uint age;
}

Employee[] employees;
employees.push(Employee("Alice", 30));
employees.push(Employee("Bob", 40));

uint totalAge = 0;
for (uint i = 0; i < employees.length; i++) {
  totalAge += employees[i].age;
}
uint averageAge = totalAge / employees.length;
print(averageAge);
hard
A. This code correctly calculates and prints average age
B. Cannot push structs into arrays
C. Loop should use i <= employees.length
D. Cannot access struct fields inside arrays

Solution

  1. Step 1: Understand struct array usage

    The code creates an array of Employee structs and adds two employees.
  2. Step 2: Check loop and calculation

    The loop sums ages correctly using employees[i].age and divides by length to get average.
  3. Final Answer:

    This code correctly calculates and prints average age -> Option A
  4. Quick Check:

    Struct arrays and loops = correct [OK]
Hint: Use array indexing and dot to access struct fields [OK]
Common Mistakes:
  • Using <= instead of < in loop causing out-of-bounds
  • Thinking structs can't be stored in arrays
  • Trying to access struct fields without indexing
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