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?
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; } }
Look at how the struct is initialized and what the getAge function returns.
The struct Person is initialized with name = "Alice" and age = 30. The function getAge() returns the age field, so it returns 30.
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?
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; } }
Check what value is assigned to d.flag in setData().
The function setData() sets d.flag to true. So, getFlag() returns true.
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?
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; } }
Check Solidity version and struct initialization rules.
Since Solidity 0.8.0, structs can be initialized inline with parameters as shown. The code is valid and compiles without errors.
Which of the following struct definitions will cause a syntax error in Solidity?
Look for missing punctuation in the struct fields.
Option D is missing a semicolon between string name and uint age, causing a syntax error.
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?
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; } }
Each call to addBatch adds one element to the array.
Calling addBatch(101) twice pushes two elements into batchNumbers. So, getBatchCount() returns 2.